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
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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

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