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

?>

Link to comment
Share on other sites

...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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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