MySQL inserts a piece of data into the database

Source: Internet
Author: User
The final result is fail.
OrderNo is the order number should be self-increment, but I do not, so I just randomly assigned a value
Session_Start ();
$corn =mysql_connect (' localhost ', ' root ', ');
mysql_query (' Set names UTF8 ', $corn);
if (! $corn) {
Die (' Could not connect: '. Mysql_error ());
}
$db _selected=mysql_select_db ("Daydream", $corn);
if (! $db _selected) {
Die (' can\ ' t use foo: '. mysql_error ());
}
$time =date ("YMD");
Echo $time;
$id =$_get[' id '];
$presult =mysql_query ("select * from production where prono= $id");
$pinfo =mysql_fetch_array ($presult);
$sum =$_post[' pronum ']* $pinfo [' Proprice '];
$num = 12;
$sql = "INSERT into order (orderno,cusno,orderdate,ordersum,orderaddress) VALUES (' $num ', ' $_session[admin] ', ' $time ', ' $sum ', ' $_post[orderaddress] ');
if (!mysql_query ($sql, $corn))
echo "fail";
Else
echo "Success";
?>


Reply to discussion (solution)

You changed echo "fail" to
Echo Mysql_error ();
It's the truth!

OrderNo is self-increasing, which is the primary key
And the primary key is non-repeatable
You give him $num value, it's possible to violate the rules.
Since you've listed the field names when inserting, the Increment field doesn't need to be in it.
$sql = "INSERT into order (cusno,orderdate,ordersum,orderaddress) VALUES (' $_session[admin] ', ' $time ', ' $sum ', ' $_post[ OrderAddress] ";

Because I'm not familiar with auto_increasement, and I didn't set OrderNo as the primary key, but I just changed it as you said. The error is as follows:
Error:you has an error in your SQL syntax; Check the manual-corresponds to your MySQL server version for the right syntax-use-near ' order (Cusno,orderdate,ord ersum,orderaddress) VALUES (' 1 ', ' 20140731 ', ' 94 ', ' 2 ') ' at line 1
It's still my insertion grammar is a problem or something ....

Order plus escape character try it.
$sql = "INSERT into ' order ' (cusno,orderdate,ordersum,orderaddress) VALUES (' $_session[admin] ', ' $time ', ' $sum ', ' $_post [OrderAddress] ') ";

It's no use adding an escape character.

What's wrong with also reported now? The Show create table order is also posted; Look.

Error or the top one:
Error:you has an error in your SQL syntax; Check the manual-corresponds to your MySQL server version for the right syntax-use-near ' order (Cusno,orderdate,ord ersum,orderaddress) VALUES (' 2 ', ' 20140731 ', ' 94 ', ' 111 ' in line 1
There's a grammatical error.
My data sheet is like this (this is an order form used to generate order records when purchasing)

OrderNo should be set to auto number and primary key

$sql = "INSERT into order (cusno,orderdate,ordersum,orderaddress) VALUES (' $_session[admin] ', ' $time ', ' $sum ', ' $_post[ OrderAddress] ";
Echo $sql;
Post the results and see

Now, how do you write your $sql? Stick it out and look. is not copying my # # code.

The result is:
INSERT into order (cusno,orderdate,ordersum,orderaddress) VALUES (' 2 ', ' 20140731 ', ' 94 ', ' 1111 ')
Error:you has an error in your SQL syntax; Check the manual-corresponds to your MySQL server version for the right syntax-use-near ' order (Cusno,orderdate,ord ersum,orderaddress) VALUES (' 2 ', ' 20140731 ', ' 94 ', ' 1111 ' at line 1
My code: Just like # #
$sql = "INSERT into order (cusno,orderdate,ordersum,orderaddress) VALUES (' $_session[admin] ', ' $time ', ' $sum ', ' $_post[ OrderAddress] ";
Echo $sql;
Echo
";
if (!mysql_query ($sql, $corn))
Die (' Error: '. mysql_error ());
Else
echo "Success";
?>

ORDERFNO has been set as the primary key and has since increased

' Order ' note the front and back escape characters.

Change to this:
$sql = "INSERT into ' order ' (cusno,orderdate,ordersum,orderaddress) VALUES (' $_session[admin] ', ' $time ', ' $sum ', ' $_post [OrderAddress] ') ";
Echo $sql;
Yes, the error message has changed:
INSERT into ' order ' (cusno,orderdate,ordersum,orderaddress) VALUES (' 2 ', ' 20140731 ', ' 94 ', ' 1111 ')
Error:you has an error in your SQL syntax; Check the manual-corresponds to your MySQL server version for the right syntax-use-near ' order ' (Cusno,orderdate, ordersum,orderaddress) VALUES (' 2 ', ' 20140731 ', ' 94 ', ' 1 ' at line 1

Because I'm making a simple shopping site,
Registration means that the user table can add information,
To buy here dead and alive added to die for a few days I think the general principle is the same but none of the success
Please help me, thank you.

Is it so hard for you to copy my code?

' Is the character above the TAB key, not the single quotation mark.

It worked
Thanks, brother!!!!!!!!!!!! upstairs. I really thought it was a single quote.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.