所以,我对这段代码有点问题。它的工作正常,但如果我尝试插入两个(或更多......)数据,它只会插入第一个数据。我该如何解决这个问题?
<?php
error_reporting(E_ERROR);
session_start();
include("db_config.php");
if(isset($_SESSION['login_user']))
{
$username=$_SESSION['login_user'];
$sql[0] = "SELECT * from orders where username='$username'";
$result= mysqli_query($conn,$sql[0]) or die(mysqli_error());
if (mysqli_num_rows($result)>0)
{
while ($record = mysqli_fetch_array($result))
{
$name =$record[username];
$id = $record[id];
$nmb_products = $record[nmb_products];
$total_value = $record[total_value];
$order_date = $record[order_date];
$city = $record[dest_city];
$address = $record[address];
}
}
else echo "Failed Access";
$sql[1] = "INSERT INTO ordered (username,nmb_products,product_id,total_value, city, address) VALUES ('$name', '$nmb_products', '$id', '$total_value', '$city', '$address')";
$sql[2] = "DELETE FROM orders WHERE username='$username'";
if(mysqli_query($conn, $sql[1]) && mysqli_query($conn, $sql[2]))
{
header("Location:mypurchases.php");
exit();
}
}
else
echo"Login first";
$conn->close();
?>
答案 0 :(得分:0)
试试这个, 您必须将所有数据放在查询中:
if (mysqli_num_rows($result)>0)
{
$sql[1]='';
while ($record = mysqli_fetch_array($result))
{
$name =$record[username];
$id = $record[id];
$nmb_products = $record[nmb_products];
$total_value = $record[total_value];
$order_date = $record[order_date];
$city = $record[dest_city];
$address = $record[address];
$sql[1].=" INSERT INTO ordered (username,nmb_products,product_id,total_value, city, address) VALUES ('$name', '$nmb_products', '$id', '$total_value', '$city', '$address');";
}
}
else echo "Failed Access";
$sql[2] = "DELETE FROM orders WHERE username='$username'";
if(mysqli_multi_query($conn, $sql[1]) && mysqli_query($conn, $sql[2]))
{
header("Location:mypurchases.php");
exit();
}
}
答案 1 :(得分:0)
所有这一部分:
$sql[1] = "INSERT INTO ordered (username,nmb_products,product_id,total_value, city, address) VALUES ('$name', '$nmb_products', '$id', '$total_value', '$city', '$address')";
$sql[2] = "DELETE FROM orders WHERE username='$username'";
if(mysqli_query($conn, $sql[1]) && mysqli_query($conn, $sql[2]))
{
header("Location:mypurchases.php");
exit();
}
可能会在这个循环中:
if (mysqli_num_rows($result)>0)
{
while ($record = mysqli_fetch_array($result))
{
$name =$record[username];
$id = $record[id];
$nmb_products = $record[nmb_products];
$total_value = $record[total_value];
$order_date = $record[order_date];
$city = $record[dest_city];
$address = $record[address];
}
}
喜欢:
if (mysqli_num_rows($result)>0)
{
while ($record = mysqli_fetch_array($result))
{
$name =$record[username];
$id = $record[id];
$nmb_products = $record[nmb_products];
$total_value = $record[total_value];
$order_date = $record[order_date];
$city = $record[dest_city];
$address = $record[address];
$sql[1] = "INSERT INTO ordered (username,nmb_products,product_id,total_value, city, address) VALUES ('$name', '$nmb_products', '$id', '$total_value', '$city', '$address')";
$sql[2] = "DELETE FROM orders WHERE username='$username'";
if(mysqli_query($conn, $sql[1]) && mysqli_query($conn, $sql[2]))
{
/*header("Location:mypurchases.php");
exit();*/
//Do something
}
}
}