NeilB Posted January 5, 2007 Share Posted January 5, 2007 I've got a single form where I want the data to be split up into two databases, an order and customer database. I have the following code[code=php:0] $connection = mysql_connect("localhost","root",""); mysql_select_db("db0369467", $connection) or die("Unable to select database"); $query1 = "INSERT INTO customer VALUES ('','$title','$fname','$lname','$housenum','$street','$town','$postcode', '$email', '', '')"; mysql_query($query1); echo mysql_error(). mysql_close($connection); $connection = mysql_connect("localhost","root",""); mysql_select_db("db0369467", $connection) or die("Unable to select database"); $query2 = "INSERT INTO order VALUES ('','$make','$model','$qty','$provider','$card_num','$type','$expires', '$price')"; mysql_query($query2); echo mysql_error(). mysql_close($connection); Print "Thank you for your order";[/code]But when I try to run the script I get the following error.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order VALUES ('','Samsung','D900','2','02','4545 4545 4545 4545','Delta','10/07'' at line 11Any idea's as to where I'm going wrong? ??? Quote Link to comment https://forums.phpfreaks.com/topic/32965-insert-into-not-working/ Share on other sites More sharing options...
Gruzin Posted January 5, 2007 Share Posted January 5, 2007 Think you have to try something like this:[code]$sql = mysql_query("INSERT INTO table(field1,field2,field3)VALUES('$var1','$var2','$var3')"); [/code] Quote Link to comment https://forums.phpfreaks.com/topic/32965-insert-into-not-working/#findComment-153507 Share on other sites More sharing options...
kenrbnsn Posted January 5, 2007 Share Posted January 5, 2007 You don't have two databases, you have two tables in one database. Therefore you can get rid of the close and re-open of the database.Change your code to something like:[code]<?php $connection = mysql_connect("localhost","root",""); mysql_select_db("db0369467", $connection) or die("Unable to select database<br>".mysql_error()); $query1 = "INSERT INTO customer VALUES ('','$title','$fname','$lname','$housenum','$street','$town','$postcode', '$email', '', '')"; mysql_query($query1) or die("Problem with query1: <pre>$query1</pre><br>" . mysql_error()); $query2 = "INSERT INTO order VALUES ('','$make','$model','$qty','$provider','$card_num','$type','$expires', '$price')"; mysql_query($query2) or die("Problem with query2: <pre>$query2</pre><br>" . mysql_error()); mysql_close($connection); Print "Thank you for your order";?>[/code]This is your code tightened up. The "or die" clauses will give you a better idea as to what is wrong.Ken Quote Link to comment https://forums.phpfreaks.com/topic/32965-insert-into-not-working/#findComment-153511 Share on other sites More sharing options...
NeilB Posted January 5, 2007 Author Share Posted January 5, 2007 Thnaks for the replies so far guys! :)I have tried your coding Ken and it doesnt like it it says -[i]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order VALUES ('','Samsung','D900','2','02','4545 4545 4545 4545','Delta','10/07'' at line 1[/i]I don't understand it as this insert command is just a modified version of one that works perfectly on another script. Quote Link to comment https://forums.phpfreaks.com/topic/32965-insert-into-not-working/#findComment-153549 Share on other sites More sharing options...
complex05 Posted January 5, 2007 Share Posted January 5, 2007 sometimes MySQL is weird, try changing your table name from order to client_orders. It may sound strange, but I bet it works.Also it is good practice to always list the column names INSERT INTO client_orders (column1, column2) values ('$var','$var') Quote Link to comment https://forums.phpfreaks.com/topic/32965-insert-into-not-working/#findComment-153554 Share on other sites More sharing options...
kenrbnsn Posted January 5, 2007 Share Posted January 5, 2007 The word "order" is a reserved word in MySQL. To use it as a table name in your query, you need to put backticks around it:[code]<?php $query2 = "INSERT INTO `order` VALUES ('','$make','$model','$qty','$provider','$card_num','$type','$expires', '$price')"; mysql_query($query2) or die("Problem with query2: <pre>$query2</pre><br>" . mysql_error());?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/32965-insert-into-not-working/#findComment-153567 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.