Jump to content

Insert into not working


NeilB

Recommended Posts

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 11


Any idea's as to where I'm going wrong? ???
Link to comment
https://forums.phpfreaks.com/topic/32965-insert-into-not-working/
Share on other sites

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
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.
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.