我想使用PHP防止表单中的重复值进入数据库表。
包含名为指定点的表的数据库:
CREATE TABLE `points` (
`appoint_id` int(11) NOT NULL,
`appoint_date` datetime NOT NULL,
`client_name` varchar(255) NOT NULL,
`client_contact` varchar(255) NOT NULL,
`sub_total` varchar(255) NOT NULL,
`vat` varchar(255) NOT NULL,
`total_amount` varchar(255) NOT NULL,
`discount` varchar(255) NOT NULL,
`grand_total` varchar(255) NOT NULL,
`paid` varchar(255) NOT NULL,
`due` varchar(255) NOT NULL,
`payment_type` int(11) NOT NULL,
`payment_status` int(11) NOT NULL,
`appoint_status` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
用于创建约会的表单处理脚本
<?php
require_once 'core.php';
$valid['success'] = array('success' => false, 'messages' => array(), 'appoint_id' => '');
if($_POST) {
$appointDate = date('Y-m-d H:i:s', strtotime($_POST['appointDate']));
$clientName = $_POST['clientName'];
$clientContact = $_POST['clientContact'];
$subTotalValue = $_POST['subTotalValue'];
$vatValue = $_POST['vatValue'];
$totalAmountValue = $_POST['totalAmountValue'];
$discount = $_POST['discount'];
$grandTotalValue = $_POST['grandTotalValue'];
$paid = $_POST['paid'];
$dueValue = $_POST['dueValue'];
$paymentType = $_POST['paymentType'];
$paymentStatus = $_POST['paymentStatus'];
$check=mysqli_query("SELECT * from points WHERE appoint_date='$appointDate'");
$checkrows=mysqli_num_rows($check);
if($checkrows>0) {
echo "appointment exists";
} else {
//insert results from the form input
$query = "INSERT IGNORE INTO points (appoint_date, client_name, client_contact, sub_total, vat, total_amount, discount, grand_total, paid, due, payment_type, payment_status, appoint_status) VALUES ('$appointDate', '$clientName', '$clientContact', '$subTotalValue', '$vatValue', '$totalAmountValue', '$discount', '$grandTotalValue', '$paid', '$dueValue', $paymentType, $paymentStatus, 1)";
$result = mysqli_query($query) or die('Error querying database.');
}
echo "Customer Added";
$appoint_id;
$AppointStatus = false;
if($connect->query($sql) === true) {
$appoint_id = $connect->insert_id;
$valid['appoint_id'] = $appoint_id;
$valid['appoint_id'] = $appoint_id;
$AppointStatus = true;
}
$AppointItemStatus = false;
for($x = 0; $x < count($_POST['serviceName']); $x++) {
$updateServiceQuantitySql = "
SELECT service.quantity
FROM service
WHERE service.service_id = ".$_POST['serviceName'][$x]."";
$updateServiceQuantityData = $connect->query($updateServiceQuantitySql);
while ($updateServiceQuantityResult = $updateServiceQuantityData->fetch_row()) {
$updateQuantity[$x] = $updateServiceQuantityResult[0] - $_POST['quantity'][$x];
// update table
$updateServiceTable = "
UPDATE service
SET quantity = '".$updateQuantity[$x]."'
WHERE service_id = ".$_POST['serviceName'][$x]."";
$connect->query($updateServiceTable);
$appointItemSql = "
INSERT INTO point_item
( appoint_id
, service_id
, quantity
, rate
, total
, appoint_item_id
) VALUES
( '$appoint_id'
, '".$_POST['serviceName'][$x]."'
, '".$_POST['quantity'][$x]."'
, '".$_POST['rateValue'][$x]."'
, '".$_POST['totalValue'][$x]."', 1)";
$connect->query($appointItemSql);
if($x == count($_POST['serviceName'])) {
$appointItemStatus = true;
}
} // while
} // /for quantity
$valid['success'] = true;
$valid['messages'] = "Successfully Added";
$connect->close();
echo json_encode($valid);
} // /if $_POST
// echo json_encode($valid);
上面的代码有效,问题是记录显示错误以添加唯一日期。但是,对于重复记录,它无法添加。我需要帮助来创建一个独特的日期和时间,以避免重复约会,使约会在被删除之前是唯一的。
如果在输入数据时发现重复行,则组合应显示已列出约会的消息。如果在表单上输入约会日期时未找到重复行,则应将其插入数据库并显示消息“已添加”。我不确定选择或插入的正确方法。