以下代码可以让您查看朋友列表 并在同一页面上编辑或删除。查看工作,但删除和更新没有。任何帮助,将不胜感激。 查看代码:
$query ="SELECT * FROM tblFriends";
$result = mysqli_query($conn,$query);
while($row =$result->fetch_assoc()){
$fname=$row['fname'];
$lname=$row['lname'];
$address=$row['address'];
$desc=$row['description'];
$zip=$row['zip'];
$city=$row['city'];
$state=$row['state'];
$id =$row['key'];
echo
"<tr>
<td><input type='text' name='fname' value='$fname'/></td>
<td><input type='text' name='lname' value='$lname'/></td>
<td><input type='text' name='address' value='$address'/></td>
<td><input type='text' name='city' value='$city'/></td>
<td><input type='text' name='desc' value='$desc'/></td>
<td><input type='text' name='state' value='$state'/></td>
<td><input type='submit' name='Edit' value='Edit'/></td>
<td><input type='submit' name='Delete' value='Delete'/></td>
<input type='hidden' name='id' value='$id'/>
<input type='hidden' name='zip' value='$zip'/>
</tr>";
}
更新代码:
if(isset($_POST['Edit'])){
$fname1=$_POST['fname'];
$lname1=$_POST['lname'];
$city1=$_POST['city'];
$state1=$_POST['state'];
$zip1=$_POST['zip'];
$desc1=$_POST['desc'];
$address1=$_POST['address'];
$id1=$_POST['id'];
$UpdateQuery ="UPDATE tblfriends SET fname='$fname1', lname='$lname1',city='$city1',address='$address1',zip='$zip1',state='$state1' WHERE id=$id1";
echo $id1;
if( mysqli_query($conn,$UpdateQuery)){
echo "Updated";
}else{
echo "Not Updated";
}
}
删除代码:
此mysqli_query
和更新版本都返回false
if(isset($_POST['Delete'])){
$id2 =$_POST['id'];
$deleteQuery= "DELETE FROM tblfriends WHERE id=$id2";
if( mysqli_query($conn,$deleteQuery)){
echo "Deleted";
}else{
echo "Not Deleted";
}
答案 0 :(得分:0)
更改此查询"DELETE FROM tblfriends WHERE id=$id2" with "DELETE FROM tblfriends WHERE id='$id2'"
。如果通过在查询编辑器中运行它来检查查询(如tab sql中的phpmyadmin),那就更好了。
在代码中尝试这样的测试:
$deleteQuery= "DELETE FROM tblfriends WHERE id=$id2";
die($deleteQuery);
然后复制浏览器中显示的结果,然后粘贴到phpmyadmin中。在那里跑。希望它可以帮助你
答案 1 :(得分:0)
我看了你的问题大声笑,把奇怪的人拿出去了?
$id =$row['key'];
"UPDATE tblfriends SET fname='$fname1', lname='$lname1',city='$city1',address='$address1',zip='$zip1',state='$state1' WHERE id=$id1
"DELETE FROM tblfriends WHERE id=$id2";
只是为了帮助你,你的身份名称是关键?但你检查id是否匹配id?不确定,但这可能是你的问题
因此请将您的查询更改为此
$query ="SELECT * FROM tblFriends";
$result = mysqli_query($conn,$query);
while($row = $result->fetch_assoc()){
$fname=$row['fname'];
$lname=$row['lname'];
$address=$row['address'];
$desc=$row['description'];
$zip=$row['zip'];
$city=$row['city'];
$state=$row['state'];
$id =$row['key'];
/**
**Wrapped your input into a form which will post the request
**/
echo
"
<form action=\"#\" method=\"POST\">
<tr>
<td><input type='text' name='fname' value='$fname'/></td>
<td><input type='text' name='lname' value='$lname'/></td>
<td><input type='text' name='address' value='$address'/></td>
<td><input type='text' name='city' value='$city'/></td>
<td><input type='text' name='desc' value='$desc'/></td>
<td><input type='text' name='state' value='$state'/></td>
<td><input type='submit' name='Edit' value='Edit'/></td>
<td><input type='submit' name='Delete' value='Delete'/></td>
<input type='hidden' name='id' value='$id'/>
<input type='hidden' name='zip' value='$zip'/>
</tr>
<br>
</form>
";
};
if(isset($_POST['Edit'])){
$fname1= $_POST['fname'];
$lname1= $_POST['lname'];
$city1= $_POST['city'];
$state1= $_POST['state'];
$zip1= $_POST['zip'];
$desc1= $_POST['desc'];
$address1= $_POST['address'];
$id1= $_POST['id'];
/**
**Updated your query to match the key rather than ID. `key`
**/
$UpdateQuery ="UPDATE tblfriends SET fname='$fname1', lname='$lname1',city='$city1',address='$address1',zip='$zip1',state='$state1' WHERE `key` = '$id1' ";
echo $id1;
if( mysqli_query($conn,$UpdateQuery)){
echo "Updated";
}else{
echo "Not Updated";
};
};
if(isset($_POST['Delete'])){
$id2 = $_POST['id'];
/**
**Updated your query to match the key rather than ID. `key`
**/
$deleteQuery= "DELETE FROM tblfriends WHERE `key` = '$id2' ";
if( mysqli_query($conn,$deleteQuery)){
echo "Deleted";
}else{
echo "Not Deleted";
}
};