Solved

PHP MySQL Multiple SQL statements

Posted on 2007-04-01
3
1,237 Views
Last Modified: 2010-05-18
I have the below PHP code which dynamically generates a SQL statement which I then want to execute using 'mysql_query' function. For some reason no results are recorded in the database, however the SQL syntax is correct. If I copy and pate the SQL output into mySQL it executes fine. No errors are shown on the PHP page.

Can anyone explain the problem and suggest a possbile solution.

Many Thanks In Advance.

$sql = "INSERT INTO customerorder VALUES(".$OrderId.", ".$customerid.", ".$postageid.", '".date("Y/m/d")."', ".$total.");";

while (list($key, $val) = each($cart)){
      $sql .= " INSERT INTO filmorder VALUES('".$key."', ".$OrderId.", ".$val.");";      
}

mysql_query($sql, $db_connection);
0
Comment
Question by:inghfs
  • 2
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18832815
try to run the mysql_error() function to see any error displayed in the output.
...
mysql_query($sql, $db_connection);
print mysql_error();

now, in regards to your INSERT statements, you should always put the column list of the destination table:


$sql = "INSERT INTO customerorder (OrderID, CustomerID, PostageID, dateField, Total ) VALUES(".$OrderId.", ".$customerid.", ".$postageid.", '".date("Y/m/d")."', ".$total.");";
while (list($key, $val) = each($cart))
{
      $sql .= " INSERT INTO filmorder (KeyField, OrderID, valueField ) VALUES('".$key."', ".$OrderId.", ".$val.");";      
}



you might also print out the generated SQL string, and try that manually in the database (using mysql command line for example).
note however that I am not sure if mysql_query is able to run multiple sql statements without a special connection option set...
0
 
LVL 29

Expert Comment

by:rdivilbiss
ID: 18832871
>>I am not sure if mysql_query is able to run multiple sql statements without a special connection option set...

"mysql_query() sends an unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier."

No you should execute each statement, then execute the next and so on. You do not have to close and reopen the connection, however.
0
 
LVL 29

Accepted Solution

by:
rdivilbiss earned 500 total points
ID: 18832876
mysqli_multi_query,

(PHP 5 ONLY) Executes one or multiple queries which are concatenated by a semicolon.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question