androidd Posted April 23, 2013 Share Posted April 23, 2013 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"); if(!$comic_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. 1 Marvel Comics The Superior Spider-Man 1 original 3.99 1 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 Quote Link to comment Share on other sites More sharing options...
davidannis Posted April 23, 2013 Share Posted April 23, 2013 Try single quotes around the field names (not backticks) ' not ` Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 23, 2013 Share Posted April 23, 2013 Try single quotes around the field names (not backticks) ' not ` No, don't. Backticks are correct. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 23, 2013 Share Posted April 23, 2013 (edited) 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 April 23, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 23, 2013 Share Posted April 23, 2013 Try single quotes around the field names (not backticks) ' not ` 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! Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 23, 2013 Share Posted April 23, 2013 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. End. Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 23, 2013 Share Posted April 23, 2013 No. Seriously you guys stop that. Lol. Ok got it Quote Link to comment Share on other sites More sharing options...
davidannis Posted April 23, 2013 Share Posted April 23, 2013 In line 24 there are no quotes of any kind around the field names which is what puzzles me. Quote Link to comment Share on other sites More sharing options...
androidd Posted April 23, 2013 Author Share Posted April 23, 2013 I've tried both right now It's set with ` instead of ' s around the column names but none of em seem to do it yet... Quote Link to comment Share on other sites More sharing options...
davidannis Posted April 23, 2013 Share Posted April 23, 2013 I think the problem is here $db_con = mysqli_query($sql_insert, $comic_db); link should be before query not after. Quote Link to comment Share on other sites More sharing options...
Solution davidannis Posted April 23, 2013 Solution Share Posted April 23, 2013 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); Quote Link to comment Share on other sites More sharing options...
androidd Posted April 23, 2013 Author Share Posted April 23, 2013 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); IT WORKED!!! XD soon as I swaped those around BAM data was entered. Thanks everyone for helping me with this. Quote Link to comment Share on other sites More sharing options...
androidd Posted April 23, 2013 Author Share Posted April 23, 2013 TY everyone! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.