gpt4 book ai didi

PHP - mysqli - 检查表是否已插入值

转载 作者:行者123 更新时间:2023-11-29 07:36:32 25 4
gpt4 key购买 nike

我正在尝试检查创建新 mysql 行时添加的 REF 编号是否已在使用中。我在添加新行时没有遇到问题,但是脚本不会首先检查数据库。

    if ($_POST['add_new_bus']){
if (($_POST['add_ref'] != "")&&($_POST['add_name'] != "")&&($_POST['add_address'] != "")&&($_POST['add_area'] != "")){
$add_ref = $_POST['add_ref'];
$add_name = $_POST['add_name'];
$add_address = $_POST['add_address'];
$add_area = $_POST['add_area'];
$chech_sql = "INSERT INTO `Details` (`REF`) VALUES ('$add_ref')";
if (!($conn->query($chech_sql))) {
echo "REF is already in use";
}else{
mysqli_query($conn, "INSERT INTO `Details` (`REF`, `NAME`, `ADDRESS`, `AREA`) VALUES ('$add_ref', '$add_name', '$add_address', '$add_area')");
echo "<p style='float:right;'>" . $_POST['add_name'] . " " . "has been added to the register with REF number:" . " " . $_POST['add_ref'] . "</p>";
}
}

知道如何检查 REF 编号是否已被使用吗?

最佳答案

为了让您正确了解如何操作,请检查以下代码:-

<?php
if (isset($_POST['add_new_bus']){
if (($_POST['add_ref'] != "") &&($_POST['add_name'] != "")&&($_POST['add_address'] != "")&&($_POST['add_area'] != "")){
$add_ref = $_POST['add_ref'];
$add_name = $_POST['add_name'];
$add_address = $_POST['add_address'];
$add_area = $_POST['add_area'];
$chech_sql = "SELECT add_ref FROM Details WHERE add_ref = '".$add_ref."'";
$result = $conn->query($chech_sql);
if (mysqli_num_rows($result) > 0) {
echo "REF is already in use";
}else{
mysqli_query($conn, "INSERT INTO `Details` (`REF`, `NAME`, `ADDRESS`, `AREA`) VALUES ('$add_ref', '$add_name', '$add_address', '$add_area')");
echo "<p style='float:right;'>" . $_POST['add_name'] . " " . "has been added to the register with REF number:" . " " . $_POST['add_ref'] . "</p>";
}
}
}
?>

注意:- 检查变量值和其他事情取决于您。因为你的代码中只有它们。谢谢。

关于PHP - mysqli - 检查表是否已插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30631781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com