简体   繁体   中英

php form does not save date into database

my problem is that i have a form that was supposed to save information about a comic book into a database, the information it is saving is title, description etc, also it is uploading an image of the comic to my server.

right now it does upload the image to the server, but it does not put any information into my table, and simple don't know why?

Im pretty new php and mysql so maybe it is an easy problem but i can't figure this out, and i haven't been able to find the answer online.

my table structure:

  1. id - int(11)
  2. title - varchar(50)
  3. description - text
  4. publicer - varchar(50)
  5. image - varchar(30)
  6. price - int(10)
  7. status - tinyint(1)

my form is on my index.php and looks like this:

<form method="post" action="newcomic.php" enctype="multipart/form-data">
<p>Comic name:
<br><input type="text" name="title"/></p>

<p>Description of the comic:
<br><textarea name="description"></textarea></p>

<p>Publicer:
<br><input type="text" name="publicer" /></p>

<p>Image:
<br><input type="file" name="image" /></p> 

<p>Price:
<br><input type="text" name="price" /></p>

<p><input type="submit" name="add" title="Add new comic to database" value="Add Comic"/></p>
</form>

And my my newcomic.php file is looking like this

<?php

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['image']['name']);

//This gets all the other information from the form
$title = $_POST['title'];
$description = $_POST['description'];
$publicer = $_POST['publicer'];
$image = ($_FILES['image']['name']);
$price = $_POST['price'];


// Connects to your Database
mysql_connect("localhost", "root", "root") or die(mysql_error()) ;
mysql_select_db("comic_express") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO products (id, title, description, publicer, image, price, status)
VALUES ('', '$title', '$description', '$publicer', '$image', '$price', '1')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

Hope that anyone can help me :)

Wrong sql syntax. You are trying to put empty string in id.

You should add some error handling to your query execution to help find what's happening.

Basic mysql error handing in php would be something like:

<?php
$link = mysql_connet(CREDS HERE);
$query = "YOUR QUERY HERE";
$result = mysql_query($query, $link);
if(!$result)
    echo mysql_error();
else
   //Query was successful, do whatever here
?>

You always want to make sure you that the query was successful, even if you're confident that it will.

I believe Guarana is right on this one as well, just take the id out (if you set up the table properly the id will be auto generated) or you will need actually insert the id instead of empty string.

hope this helps!

you must use mysql_error() function along with your query. so that you can found the eject problem in your sql query string.

use given edited code and try.

 <?php

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['image']['name']);

//This gets all the other information from the form
$title = $_POST['title'];
$description = $_POST['description'];
$publicer = $_POST['publicer'];
$image = ($_FILES['image']['name']);
$price = $_POST['price'];


// Connects to your Database
mysql_connect("localhost", "root", "root") or die(mysql_error()) ;
mysql_select_db("comic_express") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO products (title, description, publicer, image, price, status)
VALUES ('$title', '$description', '$publicer', '$image', '$price', '1')") or die(mysql_error()) ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

oh yes you are inserting blank value in integer type field. check that it is primary key with auto increment. if yes leave this column means you have no need to insert it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM