Jump to content

Inserting array into separate lines in database


Schlo_50

Recommended Posts

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);
}

?>

...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);
?>

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.

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);


}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.