【发布时间】:2019-05-31 08:26:24
【问题描述】:
今天,我为我的一个客户发布了一个新应用程序,但 INSERT 语句发生了一些不正常的事情。 在测试期间,我们没有遇到类似的情况,我不确定这是因为很多用户同时访问(100 个用户)还是与其他事情有关。 因此,在 1 次插入后,它在数据库表中插入了 1 条记录,其他 2-3 条重复...如果这将是解决方案。 非常感谢您的帮助!
$submit_the_bill = "INSERT INTO bill_status
(`bill_status_bill_id`,`bill_status_username`, `bill_status_phone_number`, `bill_status_status`,`bill_paid`, `deduction`)
VALUES ('".$bill_id."', '".$_SESSION['username']."', '".$_SESSION['phone_number']."','1','0','".$deduction."')";
$submit_the_bill_result = $conn->query($submit_the_bill);
//TRANSACTION STARTS HERE
try {
$conn->autocommit(FALSE);
$submit_the_bill = "INSERT INTO bill_status
(`bill_status_bill_id`,`bill_status_username`, `bill_status_phone_number`, `bill_status_status`,`bill_paid`, `deduction`)
VALUES ('".$bill_id."', '".$_SESSION['username']."', '".$_SESSION['phone_number']."','1','0','".$deduction."')";
$submit_the_bill_result = $conn->query($submit_the_bill);
$conn->commit();
} catch (Exception $e) {
// An exception has been thrown
// We must rollback the transaction
$conn->rollback();
}
这是整个页面,信息来自带有表单的前一页,并在执行查询和发送电子邮件后转到主页。有什么想法吗?
<?php session_start();
require('include/config.php'); //database connection
require "PHPMailer/PHPMailerAutoload.php";
$bill_id = $_POST['bill_id'];
$user_phone_number = $_POST['user_phone_number'];
$deduction = $_POST['deduction'];
$net_value_hidden = round($_POST['net_value_hidden'],2);
$official_cost_hidden = round($_POST['official_cost_hidden'],2);
$personal_cost_hidden = round($_POST['personal_cost_hidden'],2);
$tax_hidden = round($_POST['tax_hidden'],2);
$total_hidden = round($_POST['total_hidden'],2);
$total_private_price_with_vat = ($personal_cost_hidden/100)*20;
$total_private_price_with_vat = round($total_private_price_with_vat,2);
$total_payable_ammount = round($personal_cost_hidden + $total_private_price_with_vat,2);
if($total_hidden == 0) {$deduction = 4;}
if($deduction == 1) {$deduction_email = "Salary";}
else if($deduction == 2) {$deduction_email = "Cash";}
else if($deduction == 3) {$deduction_email = "Bank Transfer";}
else {$deduction_email = "No deduction";}
$submit_the_bill = "INSERT INTO bill_status
(`bill_status_bill_id`,`bill_status_username`, `bill_status_phone_number`, `bill_status_status`,`bill_paid`, `deduction`)
VALUES ('".$bill_id."', '".$_SESSION['username']."', '".$_SESSION['phone_number']."','1','0','".$deduction."')";
$submit_the_bill_result = $conn->query($submit_the_bill);
$sql_smtp = "SELECT * FROM smtp_settings";
$result_smtp = $conn->query($sql_smtp);
if ($result_smtp->num_rows > 0) {
while($row = $result_smtp->fetch_assoc()) {
$smtp_host = $row['smtp_server'];
$smtp_username = $row['smtp_user_email'];
$smtp_password = $row['smtp_user_password'];
$smtp_ssl = $row['smtp_ssl'];
$smtp_port = $row['smtp_port'];
}
}
if ($smtp_ssl == 1) {$smtp_ssl = "ssl";} else {$smtp_ssl = "tls";}
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $smtp_host; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $smtp_username; // SMTP username
$mail->Password = $smtp_password; // SMTP password
$mail->SMTPSecure = $smtp_ssl; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $smtp_port; // TCP port to connect to
$mail->setFrom('email@example.com', 'Email');
$mail->addAddress($_SESSION['username']); // Name is optional
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_SESSION['username']." submitted new bill - ".$_SESSION['bill_period'];
$mail->Body = $_SESSION['username']."(".$_SESSION['phone_number'].") Submit New Bill - ".$_SESSION['bill_period']." <br/><br/>";
$mail->AltBody = $_SESSION['username']."(".$_SESSION['phone_number'].") Submit New Bill - ".$_SESSION['bill_period']." <br/><br/>";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
//echo 'E-mail sent';
}
unset($_SESSION['bill_period']);
$conn->close();
header("Location:location"); //going to the home page
?>
【问题讨论】:
-
如果它没有 sql 注入漏洞,那就太接近了。你真的应该使用准备好的语句来安全地将数据插入到查询中。
-
那么,为什么 你会运行两次查询?
-
我认为这只是 2 个单独的代码示例挤在一起
-
第二个是我的建议,它不在代码中......这可以解决吗?谢谢你的回答,我只是想找到解决办法
-
为什么你没有一个唯一的索引来防止重复插入?其次,事务与防止重复无关。第三,仅仅显示插入语句并不能帮助我们解决您的问题。你需要明白为什么同一个插入会被多次调用。
标签: php mysql session insert duplicates