我需要你的帮助。我有删除所有记录按钮,当我删除所有记录它工作正常,直到我尝试在删除所有后添加记录,当我尝试添加第二条记录时,第一条记录将被删除。我认为问题是因为我删除所有记录后从未关闭我的查询..我不能在我的代码中使用的确切语法。请帮帮我们。 这是我删除所有的代码:
<?php
include_once 'connection.php';
$fname = $lname = $username = $phone = $email = $cemail = "";
$act="";
$txtid = 0;
if(isset($_GET['act'])){
$act=$_GET['act'];
if($act=='delete'){
$id=$_GET['id'];
$sql="delete from tbl_admin where id = $id";
$result = mysqli_query($conn,$sql)or die(mysqli_error());
if($result){
}
}
if($act=='deleteall'){
$sql="delete from tbl_admin";
$result = mysqli_query($conn,$sql)or die(mysqli_error());
}
if($act=='edit'){
$id=$_GET['id'];
$sql="select * from tbl_admin where id = $id";
$result = mysqli_query($conn,$sql)or die(mysqli_error());
while($row = mysqli_fetch_array($result)){
$txtid = $row['id'];
$fname = $row['fname'];
$lname = $row['lname'];
$username = $row['username'];
$phone = $row['mobileNo'];
$email = $row['email'];
}
}
if(isset($_POST['add'])){
// fname validate
if(empty($_POST["fname"])){
echo '<script> alert("first name is empty!"); </script>';
}else{
$fname = test_input($_POST["fname"]);
// lname validate
if(empty($_POST["lname"])){
echo '<script> alert("last name is empty!"); </script>';
}else{
$lname = test_input($_POST["lname"]);
//username
if(empty($_POST["username"])){
echo '<script> alert("username is empty!"); </script>';
}else{
$username = test_input($_POST["username"]);
//phone
if(empty($_POST["phone"])){
echo '<script> alert("mobile number is empty!!"); </script>';
}else{
$phone = test_input($_POST["phone"]);
//email
if(empty($_POST["email"])){
echo '<script> alert("email is empty!"); </script>';
}else{
$email = test_input($_POST["email"]);
//cemail
if(empty($_POST["cemail"])){
echo '<script> alert("confirm email is empty!"); </script>';
}else{
$cemail = test_input($_POST["cemail"]);
if($email != $cemail){
echo '<script> alert("Confirm Email!"); </script>';
}else{
$sql= "insert into tbl_admin(fname,lname,username,password,mobileNo,email,add_date) values('$fname','$lname','$username','123abc','$phone','$email',NOW())";
$result = mysqli_query($conn,$sql)or die(mysqli_error());
if($result){
echo '<script> alert("Successfully add admin user."); </script>';
$fname = $lname = $username = $phone = $email = $cemail = "";
}
}
}
}
}
}
}
}
}
if(isset($_POST['update'])){
if(empty($txtid)){
echo '<script>
alert("Select record to Update!");
</script>';
}else{
echo '<script>
alert("you can update record..");
</script>';
$fname = $lname = $username = $phone = $email = $cemail = "";
}
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
答案 0 :(得分:1)
因为所有代码都存在于此if语句
中if(isset($_GET['act'])){
然后我们必须假设$_GET['act']
始终设置为
由于$_GET['act']
始终设置为某项,因此DELETE代码正在运行始终。其delete
或deleteall
重建网页的代码仍然缺失,但基本上您需要更加逻辑地组织代码,并确保仅在您确实要执行删除时才设置$_GET['act']
< / strong>,这可能意味着更改HTML,因此不会被设置。
答案 1 :(得分:0)
我猜这些方法与mysql中的方法相同(没有i)
http://php.net/manual/en/mysqli-result.free.php
采用非面向对象的方式
mysqli_free_result($result);