Jump to content

What Am I doing wrong with my INSERT statement?

Go to solution Solved by davidannis,

Recommended Posts

Hey all this is my first time posting and was wondering if I could get some help and see what I am doing wrong.


// I've tried my best to find my answer through google searches and after days of working on this I finally decided to ask for some guidence.  //


Project: I am trying to make a web based form that will take information about my comic books and input them into a mysql database.


Info: I have got the code for the html input page all done and have it sending the $_POST[''] variables to another script that will actually input the data into the database.  / In addition this app is only available to me and won't be facing the public world so I have left out the checks on the data since most of it is controled variables from the form.


Problem: After I input the 7 values into the html form and submit them the script never inputs the data. I have echoed out the $_POST[''] variables to make sure they were being passed correctly and well they are.


Question: It's a multi part question / a.) Can anyone see what I am doing wrong in my INSERT statement and mysqli_query  b.) Is there a better way? I saw PDO but couldn't really wrap my head around it. c.) what could or should I be using to see what mysql is telling me when the INSERT statement runs to see if Its a problem on the other side.


What I've done/tried:  Seems like too much too say... lol but I've verified the user permissions for the DB and the user I'm connecting with and it has full rights on DB / Multiple INSERT statements but none of em work. / I've tried moving the table into a separate database scheme (still no go) / I've tried different ways of selecting the DB outside of the connection params as well as in the connection params with no favorable result. / I tried using the string that mysql workbench creates to input values the same as the php code to input values and that didn't work.


Code: [comic_form.php]

$comic_db = mysqli_connect("foo", "bar", "foobar", "comic_info_db");

	echo "Connection to DB Failed";
else {
	echo "Connect to DB Established";

// Variables From Web Form

$idcomic_db = $_POST['idcomic_db'];
$publisher = $_POST['publisher'];
$comic_name = $_POST['comic_name'];
$comic_num = $_POST['comic_num'];
$comic_cover = $_POST['comic_cover'];
$price_paid = $_POST['price_paid'];
$quantity = $_POST['quantity'];

$sql_insert = "INSERT INTO `comic_db` (idcomic_db, publisher, comic_name, comic_num, comic_cover, price_paid, quantity) VALUES ('$idcomic_db', '$publisher', '$comic_name', '$comic_num', '$comic_cover', '$price_paid', '$quantity')";
$db_con = mysqli_query($sql_insert, $comic_db);
$error = mysqli_error($db_con);

In that same script I also have echoed the variables as well as the sql_insert string and this is what I get. I did this just to see what variables were being passed as well as the string that was being created. Dunno if there is a better way to do it.

Marvel Comics
The Superior Spider-Man

INSERT INTO `comic_db`.`comic_db` (`idcomic_db`, `publisher`, `comic_name`, `comic_number`, `comic_cover`, `price_paid`, `quantity`) VALUES ('0', 'Marvel Comics', 'The Superior Spider-Man', '1', 'original', '3.99', '1')

I've also included a screen shot of the table params from mysql.

if it helps I also am using PHP Version 5.3.10-1ubuntu3.6



Sorry if this is lengthy thought more info would be better

Thanks for the help




Link to comment
Share on other sites

OP: you need to echo the error you get from MySQL.

It's probably having to do with trying to put a value into your primary key. Make that field auto increment and leave it out of your query.

Edited by Jessica
Link to comment
Share on other sites

I Googled it and read some answers from other forums and they said the exact opposite, use `` not '' around everything that needs them.


Try both!

No. Seriously you guys stop that.


Backticks are for columns and tables.

Quotes are for strings.

Nothing goes around numbers and functions.


Link to comment
Share on other sites


To clarify the correct syntax is

mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

so you need

$db_con = mysqli_query( $comic_db, $sql_insert);


soon as I swaped those around BAM data was entered. Thanks everyone for helping me with this.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.