Jump to content

error putting form in database


BartWilke

Recommended Posts

Hi all,

 

I got a error when trying to upload to database. Perhaps someone can help me ?

 

 

Error:

 

ERROR: Could not able to execute INSERT INTO `Puzzle` (`NamePuzzle`, `image`) VALUES ('', '').

 

HTML Code:

 

<form action="..upload.php" method="post" enctype="multipart/form-data">

Image: <input type="file" name="image" id="take-picture"><br>
Name Puzzle: <input type="text" name="NamePuzzle"><br>
<br>
<input type="submit" value="Upload Puzzle" name="submit">
</form>
PHP Code:
<?php
include "conn.php";
$target_dir = "../uploads/";
$target_file = $target_dir . basename($_FILES["picture"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["picture"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["picture"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["picture"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["picture"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
// Escape user inputs for security
$image = mysqli_real_escape_string($link, $_POST['image']);
$NamePuzzle = mysqli_real_escape_string($link, $_POST['NamePuzzle']);
// attempt insert query execution
$sql = "INSERT INTO `Puzzle` (`NamePuzzle`, `image`) VALUES ('$image', '$NamePuzzle')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
Link to comment
Share on other sites

<input type="file" name="image" id="take-picture">

you called your file post name "image" .

and you are calling it from php with a different name " picture" .

$target_file = $target_dir . basename($_FILES["picture"]["name"]);

here again you are calling it "image"

$image = mysqli_real_escape_string($link, $_POST['image']);

it's supposed to be

$_FILES["picture"]["name"] instead of $_post['image'];

 

Good luck

 

Edit : call you input picture and chage the calls from $_post['image'] to $_FILES["picture"]["name"]

Edited by john_jack
Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...