您好我的php有问题,只有行中的最后一个值插入到mysql数据库中:Last value inserted from rows
mysql database only show the last value to be inserted from insert record
我希望三个数据不仅插入最后的值......我该怎么做?
这是我的插入记录行为代码:
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));
}
答案 0 :(得分:1)
请显示您发送帖子数据的html页面。我认为你应该制作一个$ _POST变量数组,然后你可以获得php端的所有记录,你可以在表格中插入所有三个记录。
试试这个
请查看以下链接,以便找到解决方案 Inserting Multiple Rows with PHP & MySQL
答案 1 :(得分:0)
创建包含所有表格列的模型。
class OrderModel extends BaseModel{
public $id;
// Fill here all columns
public function __construct($data) {
foreach ($data as $key => $value) {
$this->$key = $value;
}
}
public function get_table_name() {
return "ordering";
}
}
创建Base模型类并将insert方法放在此类中。
public function insert() {
$dbh = DB::connect();
$object_keys = array_keys(get_object_vars($this));
$query = "INSERT INTO " . $this->get_table_name() . " (" . implode(',', $object_keys) . ") VALUES(:" . implode(',:', $object_keys) . ");";
$sth = $dbh->prepare($query);
foreach ($object_keys as $key) {
$sth->bindParam(':' . $key, $this->$key);
}
$sth->execute();
$this->id = $dbh->lastInsertId();
return TRUE;
}
$filtered_post = filter_input_array(INPUT_POST);
$order = New OrderModel($filtered_post);
$order->insert();