Schlo_50 Posted February 26, 2008 Share Posted February 26, 2008 Hey guys, could you check out my attachment which contains my problem. For some reaon i can't copy and paste my code snippets.. Thanks very much! Quote Link to comment Share on other sites More sharing options...
aschk Posted February 26, 2008 Share Posted February 26, 2008 Well it seems you have to loop your array and insert each time. Here's an example of what you need to do: <?php $sql = "INSERT INTO Order1 (CustomerName, OrderDate, OrderNotes, Quantity, Total, Email, Phone, Fax) VALUES ('%s', '%s', '%i', '%d', '%d', '%s', '%s', '%s')"; foreach($newCustomer as $cust){ $query = sprintf($sql, $cust['cust_name'], $cust['$date'], $cust['$orderid'], $cust['$quantity'], $cust['$total'], $cust['$email'], $cust['$phone'], $cust['$fax']); mysql_query($query,$connection); } ?> Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 26, 2008 Share Posted February 26, 2008 ...or, if you'd rather only run a single insert: <?php $raw = "('%s', '%s', '%i', '%d', '%d', '%s', '%s', '%s')"; $sql = "INSERT INTO Order1 (CustomerName, OrderDate, OrderNotes, Quantity, Total, Email, Phone, Fax) VALUES "; $entries = array(); foreach ($newCustomer as $cust) { $entries[] = sprintf($raw, $cust['cust_name'], $cust['$date'], $cust['$orderid'], $cust['$quantity'], $cust['$total'], $cust['$email'], $cust['$phone'], $cust['$fax']); } $sql .= implode(', ', $entries); mysql_query($sql); ?> Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted February 26, 2008 Author Share Posted February 26, 2008 Can i just ask, the variables you both have used in the snippets, are they just made up examples? I do need to change them for my own don't i? Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted February 26, 2008 Author Share Posted February 26, 2008 I looked here: http://uk2.php.net/manual/en/function.sprintf.php but couldn't find what the type specifer ' %i ' is meant to be? Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 26, 2008 Share Posted February 26, 2008 I looked here: http://uk2.php.net/manual/en/function.sprintf.php but couldn't find what the type specifer ' %i ' is meant to be? "%i" is not a valid sprintf argument. I copied the pattern rote, so I didn't take the time to look it over. It should be "%d" as well, since it is an integer presented as a decimal number. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted February 26, 2008 Author Share Posted February 26, 2008 Warning: Invalid argument supplied for foreach() in C:\apachefriends\xampp\htdocs\wdlayout\final.php on line 32 Line 32 : foreach ($orderid as $cust) { Whole script: session_start(); include_once('odbc/odbc.php'); require_once('operate.php'); $cust_name = $_SESSION['userName']; if (isset($_POST['update']) && ($_POST['update']==="Confirm Order")) { $orderid = $_SESSION['orderID']; $quantity = $_SESSION['Quantity']; $total = $_SESSION['Total']; $email = $_SESSION['Email']; $phone = $_SESSION['Phone']; $fax = $_SESSION['Fax']; $comment = $_SESSION['comment']; $date = date('d m y'); $conn = odbc_connect('CentralProduct', 'root', '') or die('Could not Connect to ODBC Database!'); // execute SQL Statement $raw = "('%s', '%s', '%d', '%d', '%d', '%s', '%s', '%s')"; $sql = "INSERT INTO Order1 (CustomerName, OrderDate, OrderNotes, Quantity, Total, Email, Phone, Fax) VALUES "; $entries = array(); foreach ($orderid as $cust) { $entries[] = sprintf($raw, $cust['userName'], $cust['$date'], $cust['$orderid'], $cust['$quantity'], $cust['$total'], $cust['$email'], $cust['$phone'], $cust['$fax']); } $sql .= implode(', ', $entries); } Quote Link to comment 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.