Question
I need help uploading an image, title, description and keywords(text) to the database,A session is required to access this page, when I click on the
I need help uploading an image, title, description and keywords(text) to the database,A session is required to access this page, when I click on the input button though, I get redirected to a blank page, with the PHP page that is supposed to upload the data in the URL and the DB remains empty. I understand that this may be hard to correct as I won't be able to post all my code but a working example would be appreciated either way.
Here is the DB Schema
DROP TABLE IF EXISTS `members`;
CREATE TABLE IF NOT EXISTS `members` (
`username` varchar(40) NOT NULL,
`email` varchar(25) NOT NULL,
`dateOfBirth` date NOT NULL,
`gender` varchar(20) NOT NULL,
`photography` varchar(20) NOT NULL,
`landmark` varchar(40) NOT NULL,
`pWord` varchar(40) NOT NULL,
PRIMARY KEY (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `members` (`username`, `email`, `dateOfBirth`, `gender`, `photography`, `landmark`, `pWord`) VALUES
('kevinMckevin', 'kmck@Gmal.com', '2002-06-05', 'Male', 'Photo Journalism', 'Maracas Beach', 'fde290ea8d375a112998beacd5f4cff5'); pWord is md5(kenny)
it's g - m -a -i - l .com
DROP TABLE IF EXISTS `posts`;
CREATE TABLE IF NOT EXISTS `posts` (
`image` blob,
`description` text,
`emailFK` varchar(25) DEFAULT NULL,
`title` text,
`keywords` text NOT NULL,
KEY `emailFK` (`emailFK`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
COMMIT;
I could not place the login form due to the question being too long
Here is the php code to login
if (isset($_POST['login'])) {
$email = $_POST['email'];
$password = $_POST['password'];
include 'db_conn.php';
$sql = "SELECT * FROM members WHERE email='$email'";
$result = mysqli_query($con, $sql) or die("Error:".mysqli_error($con));
$rowcount = mysqli_num_rows($result);
if ($rowcount == 1) {
session_start();
$_SESSION['user'] = $email;
header('location: welcome.php');
} else {
echo "
alert('Email or Password does not exist');
window.location=\"../html/loginForm.html\";
";
}
mysqli_close($con);
}
?>
Here is the form to upload the image
session_start();
if (!isset($_SESSION['email'])) {
header("location:../html/registerPHP.html");
}
?>
Upload images
And here is the php code to upload it to the database
if(isset($_POST["upload"])){
session_start();
//Capture values from form and store in php variables
$image_desc=$_POST['description'];
$uploader=$_SESSION['user']; //$_SESSION not $_POST
$target_dir="../img/";
$file_name=$_FILES["image"]["name"];
$file_size=$_FILES["image"]["size"];
$file_tmp_name=$_FILES["image"]["tmp_name"];
$target_file=$target_dir.$file_name;
$file_ext=strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$error = false;
$field_array=array($file_name, $image_desc, $uploader);
$valid = true;
//The foreach loop works only on arrays and is used to loop through each key/value pair in an array.
foreach($field_array as $field){
if(empty($field)){
$valid = false;
}
}
if (!$valid){
header('location: uploadImageForm.php');
}
//Check if file already exists
if (file_exists($target_file)){
echo "
alert('File already exists.');
window.location=\"uploadImageForm.php\";
";
$error = true; //line was not included
}
//Check file size
if ($file_size > 2000000){
echo "
alert('File limit of 2MB exceeded.');
window.location=\"uploadImageForm.php\";
";
$error = true;
}
//Allow certain file formats
if($file_ext!="jpg" && $file_ext!="png" && $file_ext!="jpeg" && $file_ext!="gif"){
echo "
alert('Only JPG, JPEG, PNG & GIF files allowed');
window.location=\"uploadImageForm.php\";
";
$error = true;
}
//Check if image file is authentic
$check=getimagesize($file_tmp_name);
if($check==false){
echo "
alert('File is not a real image.');
window.location=\"uploadImageForm.php\";
";
$error=true;
}
//If everything is ok, try to upload file and insert data into client_pic table
if($error==false){
if (move_uploaded_file($file_tmp_name, $target_file)){
include 'con_server_db.php';
$sql="INSERT INTO client_pic (clientPic, description, clientFK )
VALUES ('$file_name','$image_desc','$uploader')"; //change from client_pict to client_pic
if (mysqli_query($con, $sql)){
echo "
window.location=\"viewImage.php\";
";
}
else {
echo "
alert('Error inserting data into client_pict table');
window.location=\"uploadImageForm.php\";
";
}
mysqli_close($con);
}
else {
echo "
alert('There was an error uploading your file');
window.location=\"uploadImageForm.php\";
";
}
}
}
?>
Even is you use your own example I'd still appreciate it
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started