您好我试图从作业表中减去接受的出价是一个int,并从客户表中减去信用额,但由于某种原因它不会更新信用行。任何想法为什么会这样?
<?php
session_start();
require 'config.php';
$id = $_SESSION['login_user'];
$jobid = $_POST['job_id'];
$poster_id = $_POST['poster_id'];
$accepted_bidder = $_POST['accepted_bidder'];
$accepted_bid = $_POST['accepted_bid'];
$query = "SELECT credit FROM `customer` WHERE email_adress = '$.id'";
$success = $conn->query($query);
$updated_credit = $success - $accepted_bid
echo $updated_credit;
$query2 = " UPDATE job SET start_escrow = '1' WHERE job_id = '$jobid'";
$success2 = $conn->query($query2);
$query3 = " UPDATE credit SET credit_amount = '$updated_credit' WHERE credit_id = '$poster_id'";
$success3 = $conn->query($query3);
$poster_id = $_POST['poster_id'];
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
header("location: myjobs.php");
echo "Thank You For Contacting Us <br>";
$conn->close();
?>
答案 0 :(得分:0)
您确定已启用自动提交吗?如果没有,则在每个$conn->query(...)
之后您必须指定$conn->commit
,或者您可以创建一个交易,并且只在最后提供该代码
答案 1 :(得分:0)
您可以使用PHP过滤器功能来帮助您入门,或创建自己的自定义功能来处理清理。
电子邮件 - filter_input():
(?<=/> )([^<>]* <p="[^"]*"/>){3}(?= <head>)
电子邮件 - filter_val():
filter_input( INPUT_POST, 'form_input_email_name_here', FILTER_SANITIZE_NAME );
filter_var( $email, FILTER_SANITIZE_EMAIL );
或者,使用preg_replace()创建自定义函数以清理数据
$id = (int) $_POST['id'];
注意:上述代码段只是演示,以向您展示我们在谈论的内容。
// Will accept full names like: john doe, john doe smith
function sanitize_name( $data )
{
$full_name_regex = "/^([a-z]{2,20})([\s])([a-z]{2,20})(\s[a-z]{2,20})?$/i";
return preg_replace( $full_name_regex, '', $data );
}
这可能是由于以下结果:
$query3 = " UPDATE credit SET credit_amount = '$updated_credit' WHERE credit_id = '$poster_id'";
$success3 = $conn->query($query3);
如果 var_dump( $success3->rowCount );
小于1,则表示信用表中不存在$success3->rowCount
,这就是更新失败的原因。
或者,在其他方面要确保这一点,请执行select查询并检查结果:
'$poster_id'
检查查询是否返回任何结果
$sql = "SELECT credit_amount FROM credit WHERE credit_amount = '$updated_credit'";
$success3 = $conn->query( $sql );
因此,如果var_dump( $success3 );
为空,则表明信用表中不存在var_dump( $success3 )
。
还有一件事,我不确定这一行:'$poster_id'
'$.id'
应该是:$query = "SELECT credit FROM `customer` WHERE email_adress = '$.id'";