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! Link to comment https://forums.phpfreaks.com/topic/93096-inserting-array-into-separate-lines-in-database/ 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); } ?> Link to comment https://forums.phpfreaks.com/topic/93096-inserting-array-into-separate-lines-in-database/#findComment-476961 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); ?> Link to comment https://forums.phpfreaks.com/topic/93096-inserting-array-into-separate-lines-in-database/#findComment-476962 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? Link to comment https://forums.phpfreaks.com/topic/93096-inserting-array-into-separate-lines-in-database/#findComment-476969 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? Link to comment https://forums.phpfreaks.com/topic/93096-inserting-array-into-separate-lines-in-database/#findComment-476983 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. Link to comment https://forums.phpfreaks.com/topic/93096-inserting-array-into-separate-lines-in-database/#findComment-476991 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); } Link to comment https://forums.phpfreaks.com/topic/93096-inserting-array-into-separate-lines-in-database/#findComment-476993 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.