简体   繁体   中英

How to Insert Record in multiple rows PHP MYSQL

在此处输入图片说明

I have a problem whenever I wanted to insert record in rows, it only insert the last value of the rows

Here is my insert server record behavior code:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
 $insertSQL = sprintf("INSERT INTO ordering (productCode, Name, paymentMethod, Quantity, TotalPrice) VALUES (%s, %s, %s, %s, %s)",
                   GetSQLValueString($_POST['productcode'], "int"),
                   GetSQLValueString($_POST['Name'], "text"),
                   GetSQLValueString($_POST['PaymentMethod'], "text"),
                   GetSQLValueString($_POST['quantity'], "int"),
                   GetSQLValueString($_POST['totalprice'], "double"));

  mysql_select_db($database_perfume_connection, $perfume_connection);
  $Result1 = mysql_query($insertSQL, $perfume_connection) or die(mysql_error());

  $insertGoTo = "member_perfume_homepage.php";
  if (isset($_SERVER['QUERY_STRING'])) {
  $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
  $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

The SQL You are executing is:

INSERT INTO ordering (productCode, Name, paymentMethod, Quantity, TotalPrice) VALUES (%s, %s, %s, %s, %s)

which inserts only one row. Thats the syntax of SQL. Also, You should notice by Your common sense, that You are parsing $_POST only once, so it will not retrieve You multiple values to be used in rows used by sprintf.

Correct syntax of inserting multiple rows in one SQL query is:

INSERT INTO ordering (productCode, Name, paymentMethod, Quantity, TotalPrice) VALUES (%s, %s, %s, %s, %s), (%s, %s, %s, %s, %s), (%s, %s, %s, %s, %s);

If You do would like to prepare SQL query using sprintf function, You will need 3x more arguments passed to that method.

$insertSQL = sprintf("INSERT INTO ordering (productCode, Name, paymentMethod, Quantity, TotalPrice) VALUES (%s, %s, %s, %s, %s)",
                   GetSQLValueString($_POST['productcode'], "int"),
                   GetSQLValueString($_POST['Name'], "text"),
                   GetSQLValueString($_POST['PaymentMethod'], "text"),
                   GetSQLValueString($_POST['quantity'], "int"),
                   GetSQLValueString($_POST['totalprice'], "double"),
                   GetSQLValueString($_POST['productcode'], "int"),
                   GetSQLValueString($_POST['Name'], "text"),
                   GetSQLValueString($_POST['PaymentMethod'], "text"),
                   GetSQLValueString($_POST['quantity'], "int"),
                   GetSQLValueString($_POST['totalprice'], "double"),
                   GetSQLValueString($_POST['productcode'], "int"),
                   GetSQLValueString($_POST['Name'], "text"),
                   GetSQLValueString($_POST['PaymentMethod'], "text"),
                   GetSQLValueString($_POST['quantity'], "int"),
                   GetSQLValueString($_POST['totalprice'], "double"));

This should work, although, I don't suggest using this way, but think of a better solution.

Also, mysql_* functions are deprecated. Use PDO or MySQLi instead. Read about them in PHP manual: http://php.net/manual/en/book.pdo.php or http://php.net/manual/en/class.mysqli.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM