我觉得我错了。我有两个变量,一个是获取日期和月份,另一个是数字(000)。我将两个变量连接在一起并将其设置为另一个变量,然后将其插入数据库。所以我想要更新列,以便每个收据都有唯一的no。收据没有格式是(14-07000)做的是获取列的最大值插入变量并每次添加1找到新的最大值。但我没有得到我需要的东西。它插入我想要但不更新。我希望收据没有列是14-7000)。然后开始更新到14-07001,14-07002,14-07003,14-07004,14-07005,14-07006,14-07007等。所以明天,它可以改为15-07000和下个月,它可以改变到15-08000。
这是我使用的代码
<?php
// pass the data into the database
$tran_id1 = date('d-m');
$tran_id2 = "000";
$transaction_id = $tran_id1 . $tran_id2;
if (empty($received & $amount_to_pay & $amount_collected) == false)
{
$sql8 = "INSERT INTO `transactions` (`patient_name`,`total_amount`, `amount_tendered`, `payment_status`, `payment_date`, `paid_for`,`hospital_no`, `received_by`,`receipt_no`) VALUES ('$received', '$amount_to_pay', '$amount_collected', '$payment', '$payment_date', '$being_pay_for','$hosp_no', '$firstname','$transaction_id')";
$query8 = mysqli_query($dbconnect, $sql8) or die (mysqli_error($dbconnect));
$sql9 = "INSERT INTO `transaction_paid_for` (`paid_for_name`) VALUES ('$others')";
$query9 = mysqli_query($dbconnect, $sql9) or die (mysqli_error($dbconnect));
$max = "SELECT MAX(`receipt_no`) AS transaction_id FROM `transactions`";
$max2 = mysqli_query($dbconnect, $max);
$result1 = mysqli_fetch_array($max2);
$result2 = $result1['transaction_id'];
$result3 = $result2+1;
$sql = "UPDATE `transactions` SET `receipt_no`='$result3' WHERE `patient_name`='$received'";
$result4 = mysqli_query($dbconnect, $sql);
}
?>
答案 0 :(得分:1)
我的猜测是你不能在varchar字段上使用这样的数学计算。一个很好的选择是将d-m存储在单独的varchar列中,并将增量数存储在int列中。
然后,您可以使用代码获取所需的收据编号。
//create 2 columns in your table eg dm and receipt_no
//to store tran_id1 & tran_id2 into dm and receipt_no columns
$tran_id1 = date('d-m');
$tran_id2 = "000";
// Create connection & select
$sql = "SELECT MAX(receipt_no)+1 MAX_RECEIPT_NO FROM transactions WHERE dm='" . $tran_id1 . "' LIMIT 0,1";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$tran_id2 = $row["MAX_RECEIPT_NO"];
}
//create your receipt no
$transaction_id = $tran_id1 . str_pad($tran_id2, 3, '0', STR_PAD_LEFT);
//now use sql update with this $transaction_id
注意: