0

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 :)

3
  • 2
    You might try outputting mysql errors like this: mysql_query(...) or die(mysql_error());. One thing I notice is that you're trying to insert a string into id, which is set as int(11). If id is an auto-incremented field, leave it out of your INSERT statement. Commented Nov 27, 2013 at 22:29
  • If your id column is set to AUTO_INCREMENT remove the first entries in your query. (EDIT) Seems like we entered the same comment at the same time lol @showdev Commented Nov 27, 2013 at 22:31
  • So I'm feeling a little stupid right now, i did what you guys said an removed the id, still didn't work, tried the mqsql_error an it turns out that the image name i was trying to upload was longer the 30 characters, it was 31 -.- but anyways. thanks to all of you for your help. Commented Nov 28, 2013 at 8:39

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.