如果即使“if条件”为真,你可以帮我解决脚本中没有任何问题吗?
我已添加完整代码,请检查
if (!empty($_POST)) {
$client_name = $_POST['client_name'];
$other_client = $_POST['other_client'];
$district = $_POST['district'];
$taluka = $_POST['taluka'];
$village = $_POST['village'];
$mobile_no = $_POST['mobile_no'];
$case_no = $_POST['case_no'];
$sr_no = $_POST['sr_no'];
$sql3="SELECT total_amt from date where paydate ='". $payment_date. "'";
$q = $pdo->prepare($sql3);
$q->execute(array($client_id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$dataamt = $data['total_amt'];
$datadate = $data['paydate'];
$newamt = $dataamt + $fees ;
if ( $datadate == $payment_date ) {
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql12 = "UPDATE date set total_amt = ? where paydate = ?";
$q12 = $pdo->prepare($sql12);
$q12->execute(array($newamt,$payment_date));
} else {
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql13 = "INSERT INTO date(paydate,total_amt) values(?,?)";
$q13 = $pdo->prepare($sql13);
$q13->execute(array($payment_date,$fees));
}
答案 0 :(得分:0)
主要问题是您使用的是$data['paydate']
,但是您没有在查询中选择该列,因此结果集中不存在该值。
$sql3="SELECT total_amt from date where paydate ='". $payment_date. "'";
^^^^^^^^^ You are only selecting this value
...
// You are not selecting paydate in your query so this only generates a warning
// and the resulting value will be null
$datadate = $data['paydate'];
if ( $datadate == $payment_date ) {
请注意,您也应该在这里使用预备声明。
除此之外,当找到一行时,这些值将始终匹配。检查行是否存在更合乎逻辑。
if ($q->rowCount() === 1)
但是,根据您展示的内容,INSERT ... ON DUPLICATE KEY UPDATE ...
会更容易;只有1个查询,没有条件。假设paydate当然是唯一的,但是否则你的代码没有意义。
答案 1 :(得分:0)
use strtotime() function
eg:
$payment_date = strtotime($payment_date);
$datadate = strtotime($datadate);
if ( $datadate == $payment_date ) {
}
如果你不知道使用下面链接的strtotime功能