klainis Posted June 8, 2006 Share Posted June 8, 2006 I can't seem to get variables to work with my query statement. Everything works fine if I manually input what I want in the PHP code, but not from the variables. I have added various echo statements just to make sure that all the variables actually hold something, and they do. I have also run some select statements to make sure that the database is actually connected and it is. I get no error message from this, it just leaves a blank screen. Here is the code.[code]<html><head> <title>Book Entry Results</title></head><body><h1>Book Entry Results</h1><?php //create short variable names $isbn=$_POST['isbn']; $author=$_POST['author']; $title=$_POST['title']; $price=$_POST['price']; if(!$isbn || !$author || !$title || !$price) { echo "You have not entered all the required details.<br />" ."Please go back and try again."; exit(); } if(!get_magic_quotes_gpc()) { $isbn=addslashes($isbn); $author=addslashes($author); $title=addslashes($title); $price=doubleval($price); } $db = new mysqli('localhost', 'username', 'password', 'books'); if (mysqli_connect_errno()) { echo "Error: Could not connect to database. Please try again later."; exit(); } $query = "INSERT INTO books VALUES ('$isbn', '$author', '$title', '$price')"; //echo $query; //$result = $db->query($query); $result = mysqli_query($db, $query); //echo $result; if($result) echo $db->affected_rows.' book inserted into database.'; else echo "This sucks"; $db->close(); ?></body></html>[/code]Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/11519-variables-and-mysql-insert/ Share on other sites More sharing options...
Buyocat Posted June 8, 2006 Share Posted June 8, 2006 The problem I see with your insert statement is the following, it specifies the values to be inserted but not the columns to insert the values to. You need to construct the statement like this:INSERT INTO tablename (col1, col2, col3) VALUES ('val1', 'val2', 'val3')hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/11519-variables-and-mysql-insert/#findComment-43360 Share on other sites More sharing options...
klainis Posted June 8, 2006 Author Share Posted June 8, 2006 [!--quoteo(post=381570:date=Jun 8 2006, 01:05 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 8 2006, 01:05 PM) [snapback]381570[/snapback][/div][div class=\'quotemain\'][!--quotec--]The problem I see with your insert statement is the following, it specifies the values to be inserted but not the columns to insert the values to. You need to construct the statement like this:INSERT INTO tablename (col1, col2, col3) VALUES ('val1', 'val2', 'val3')hope that helps[/quote]I have actually tried it that way. I have also tried [code]$query = "INSERT INTO books set isbn = '$isbn', author = '$author', title = '$title', price = '$price'";[/code]I have even tried not bothering with the query variable and just plugging the full query into [code]$result = mysqli_query($db, "");[/code]And no matter how I form it, I still get the blank page, but if I replace the variables with words/numbers the database is updated and the results are shown.Thanks,Robert Quote Link to comment https://forums.phpfreaks.com/topic/11519-variables-and-mysql-insert/#findComment-43373 Share on other sites More sharing options...
.josh Posted June 8, 2006 Share Posted June 8, 2006 so you are telling us that [code]$query = "INSERT INTO books (isbn,author,title,price) VALUES ('$isbn','$author','$title','$price')";$result = mysql_query($query); [/code]does not work? Quote Link to comment https://forums.phpfreaks.com/topic/11519-variables-and-mysql-insert/#findComment-43374 Share on other sites More sharing options...
nogray Posted June 8, 2006 Share Posted June 8, 2006 use the code Crayon Violent suggested and print out any errors, like this[code]$query = "INSERT INTO books (isbn,author,title,price) VALUES ('$isbn','$author','$title','$price')";$result = mysql_query($query) or die(mysql_error()); [/code]The error message should tell you what's wrong, if you need more help, you can post it here. Quote Link to comment https://forums.phpfreaks.com/topic/11519-variables-and-mysql-insert/#findComment-43381 Share on other sites More sharing options...
klainis Posted June 8, 2006 Author Share Posted June 8, 2006 It was the mysqli_error($db) function that made me realize I was sending an invalid character. I feel a bit dumb since it was such a small mistake, but I guess it's useful for learning. Thank you all for the help, I spent 3 hours trying to find the answer to this question with Google searches, and all I really had to do was post here.Thanks,Robert Quote Link to comment https://forums.phpfreaks.com/topic/11519-variables-and-mysql-insert/#findComment-43388 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.