我确实找到了有关如何检查您的数据是否已存在的文章,但是,如果有数据存在,则有其他选项可以替换该数据。
我的代码:
<?php
include "db_connect.php"; //Connection to database
if(isset($_POST['update']))
{
$ID = $_GET["ID"];
$UserID = $_GET['ID'];
$FirstName = $_POST["firstname"];
$LastName = $_POST["lastname"];
$OtherNames = $_POST["othernames"];
$Address = $_POST["address"];
$Gender = $_POST["gender"];
$Race = $_POST["race"];
$BirthDate = $_POST["birthdate"];
$NextOfKin = $_POST["nextofkin"];
$NokRelationship = $_POST["nokrelationship"];
$checkid= mysqli_query($connect,"SELECT * FROM tbl_users WHERE UserID= '".$UserID."'");
$match = mysqli_num_rows($checkid);
if ($match > 0){
echo '<script type="text/javascript">';
echo 'alert("User ID Already Exists!");';
echo 'window.location.href = "lookup.php";';
echo '</script>';
}
else
{
$query = "UPDATE tbl_users SET FirstName = '".$FirstName."',
LastName = '".$LastName."',
OtherNames = '".$OtherNames."',
Address = '".$Address."',
Gender = '".$Gender."',
Race = '".$Race."',
BirthDate = '".$BirthDate."',
NextOfKin = '".$NextOfKin."',
Relationship = '".$NokRelationship."'
WHERE UserID = '".$UserID."'";
$result = mysqli_query($connect,$query);
if($result)
{
echo '<script type="text/javascript">';
echo 'alert("Data Updated");';
echo 'window.location.href = "lookup.php";';
echo '</script>';
}
}
}
?>
由于某种原因,我破坏了此代码,无法再使用它。 以前发生的是,如果我尝试编辑名称,地址或除用户ID之外的其他任何内容,它会告诉我用户ID已经被占用,我怀疑这是因为我设置了代码来检查ID是否已存在并被阻止它。
我应该做的是在它已经存在的地方添加一些内容,如果我想替换它。但是,我似乎无法在网上找到任何可完成此操作的代码。帮帮我?