选择查询工作正常但插入命令没有将数据保存在数据库中,我认为我没有正确完成事务,或者由于语法使用不当而可能是滚动支持事务
session_start();
if (isset($_COOKIE['username'])) {
$_SESSION['role'] = $_COOKIE['role'];
$_SESSION['username'] = $_COOKIE['username'];
$_SESSION['rid'] = $_COOKIE['rid'];
}
if (!isset($_SESSION['username'])) {
header('location: login.php');
}
$servername = "localhost";
$username = "johnalla_Mohsin";
$password = "Mohsin1982";
$database = "johnalla_m_billing";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
date_default_timezone_set('Asia/Karachi');
$currentdate = date("Y-m-d");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_COOKIE['username'])) {
$_SESSION['role'] = $_COOKIE['role'];
$_SESSION['username'] = $_COOKIE['username'];
$_SESSION['rid'] = $_COOKIE['rid'];
}
if (isset($_POST['invoice_no'])) {
$conn->autocommit(FALSE);
$result1 = $conn->query("select sum(cost * quantity) as total from temp_entries");
if ($result1->num_rows > 0) {
// output data of each row
while ($row1 = $result1->fetch_assoc()) {
$total = $row1["total"];
}
if ($conn->query("insert into bill (c_name, date, total) values ('$_POST[name]', '$_POST[date]', '$total')") === TRUE) {
$last_id = $conn->insert_id;
$result2 = $conn->query("select * from temp_entries");
if ($result2->num_rows > 0) {
// output data of each row
while ($row2 = $result2->fetch_assoc()) {
$entries[] = $row2;
$conn->query("insert into bill_entries (bill_id, item_name, description, cost, quantity) "
. "values ('$last_id', '$row2[item_name]', '$row2[description]', '$row2[cost]', '$row2[quantity])");
}
if ($conn->query("truncate table temp_entries") === TRUE) {
$conn->commit();
}
}
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();