在一个表单中提交多个 Mysqli 插入
Multiple Mysqli insert in one form submit
我正在尝试保存用户填写某种表格的数据。
问题是当我按下提交按钮时,所有数据都完全按照我想要的方式保存,但它保存了多次。 (多行相同的数据)
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$share = $_POST['share'];
$date = $_POST['date'];
$dealtype = $_POST['dealtype'];
$enter = $_POST['enter'];
$exitdeal = $_POST['exitdeal'];
$no1 = $_POST['no1'];
$no2 = $_POST['no2'];
$profit = $_POST['profit'];
if ($share != "")
{
if ($date != "")
{
if ($dealtype != "" )
{
if ($enter != "" )
{
if ($exitdeal != "" )
{
$mysql = "INSERT INTO markettable (share, date, dealtype, enter, exitdeal, no1, no2, profit)
VALUES ('$share','$date', '$dealtype', '$enter', '$exitdeal', '$no1', '$no2', '$profit')";
}
}
}
}
}
$mysqli->query($mysql);
if ($mysqli->query($mysql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $mysql . "<br>" . $mysqli->error;
}
$mysqli->close();
}
?>
你调用query执行了两次,第一次在if语句之前,第二次在if语句条件内。
看这里,
$mysqli->query($mysql);
if ($mysqli->query($mysql) === TRUE) {
echo "New record created successfully";
}
$mysqli->query($mysql);
先插入数据,if ($mysqli->query($mysql) === TRUE)
再插入。
您将代码更改为,
$result = $mysqli->query($mysql); // save the result in $result variable
if ($result === TRUE) { // checks if its successfully inserted
echo "New record created successfully";
}
我正在尝试保存用户填写某种表格的数据。
问题是当我按下提交按钮时,所有数据都完全按照我想要的方式保存,但它保存了多次。 (多行相同的数据)
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$share = $_POST['share'];
$date = $_POST['date'];
$dealtype = $_POST['dealtype'];
$enter = $_POST['enter'];
$exitdeal = $_POST['exitdeal'];
$no1 = $_POST['no1'];
$no2 = $_POST['no2'];
$profit = $_POST['profit'];
if ($share != "")
{
if ($date != "")
{
if ($dealtype != "" )
{
if ($enter != "" )
{
if ($exitdeal != "" )
{
$mysql = "INSERT INTO markettable (share, date, dealtype, enter, exitdeal, no1, no2, profit)
VALUES ('$share','$date', '$dealtype', '$enter', '$exitdeal', '$no1', '$no2', '$profit')";
}
}
}
}
}
$mysqli->query($mysql);
if ($mysqli->query($mysql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $mysql . "<br>" . $mysqli->error;
}
$mysqli->close();
}
?>
你调用query执行了两次,第一次在if语句之前,第二次在if语句条件内。
看这里,
$mysqli->query($mysql);
if ($mysqli->query($mysql) === TRUE) {
echo "New record created successfully";
}
$mysqli->query($mysql);
先插入数据,if ($mysqli->query($mysql) === TRUE)
再插入。
您将代码更改为,
$result = $mysqli->query($mysql); // save the result in $result variable
if ($result === TRUE) { // checks if its successfully inserted
echo "New record created successfully";
}