作者ufly (飛)
看板PHP
標題Re: [請益] 如何用checkbox foreach 刪除多筆資料
時間Mon May 14 09:34:42 2012
※ 引述《ufly (飛)》之銘言:
: 各位前輩好,我想使用foreach語法刪除我資料表中的多筆資料
: 但無法執行...不知道哪邊語法有錯 (網頁執行正常,資料庫卻一動也不動)
: member_table 中的第二欄為 username
: [NO] [username] [password]
: 主鍵
: §這是秀資料的網頁碼
: <?php
: $data = "SELECT * FROM member_table";
: $result = mysql_query($data);
: echo '<form name="form" method="post" action="del.php">';
: echo '<table width="700" border="1">' ;
: while($row = mysql_fetch_row($result))
: {
: echo "<tr>";
: echo '<td><input type=checkbox name="del[]" value="$row[1]" /></td>';
: echo "<td>$row[1]</td>";
: echo "<td>$row[1]</td>";
: }
: echo "</table>";
: echo '<input type=submit value="删除">';
: echo '</form>';
: >?
: §這是del.php
: <?php
: $link=mysql_connect("localhost","root","test");
: mysql_select_db("mydb");
: $id=$_POST['del'];
: foreach($id as $ide){
: $data="delete from member_table where username=$ide";
: $result=mysql_query($data);
: if((mysql_affected_rows()==0) or (mysql_affected_rows==-1))
: {
: echo "沒有找到紀錄,或者刪除時出錯";
: exit;
: }
: else{
: echo "紀錄已刪除";
: }
: }
: mysql_close();
: ?>
我將語法改成 member.php
<?php
$data = "SELECT * FROM member_table";
$result = mysql_query($data);
echo '<form name="form" method="post" action="del.php">';
echo '<table width="700" border="1">' ;
while($row = mysql_fetch_
array($result))
{
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"del[]\" value=\"$row[1]\"
/></td>";
//這邊的value可以打$row[1]或是$row[username],秀出來的值都是一樣的
echo "<td>$row[1]</td>";
}
echo "</table>";
echo '<input type=submit value="删除">';
echo '</form>';
>?
--------------
del.php
--------------
<?php
$link=mysql_connect("localhost","root","test");
mysql_select_db("mydb");
$idArray=$_POST['del'];
foreach(
(array)$idArray as $username){
$sql = 'delete from member_table where username=
\''.$username.'\'';
if((mysql_affected_rows()==0) or (mysql_affected_rows==-1))
{
echo "沒有找到紀錄,或者刪除時出錯";
exit;
}
else{
echo "紀錄已刪除</br>";
echo "$sql</br>";
echo "$username";
}
}
mysql_close();
?>
--------------------
在member.php將 帳號為test2的checkbox打勾送出之後
del.php出現
紀錄已刪除
delete from member_table where username='test2'
test2
這邊member.php正確將要刪除的值送到del.php了
但是
資料表的紀錄一樣沒有刪除
但是如果把網頁的 delete from member_table where username='test2'
這段sql語法貼近phpmyadmin的sql執行指令
又可以刪除
已刪除欄數: 1 (查詢需時 0.0034 秒)
想請問這次問題出在哪邊....好不容易可以送值了,卻刪不掉..
(泣)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 125.231.208.76
1F:→ chenstin:看起來少了 05/14 10:01
2F:→ chenstin:mysql_query($sql,$link) or die(mysql_error($link)); 05/14 10:02
3F:→ chenstin:還有mysql_affected_rows==-1應該是 05/14 10:06
4F:→ chenstin:mysql_affected_rows()==-1 05/14 10:06
5F:→ chenstin:所以會恆false 導致都會跑到 "紀錄已刪除" 05/14 10:07
6F:→ ufly:感謝 chenstin ,可以正常刪除資料了。感激不盡 05/14 11:21
<?php
$link=mysql_connect("localhost","root","test");
mysql_select_db("mydb");
$idArray=$_POST['del'];
foreach(
(array)$idArray as $username){
$sql = 'delete from member_table where username=
\''.$username.'\'';
$result = mysql_query($sql,$link) or die (mysql_error($link));
if((mysql_affected_rows()==0) or (mysql_affected_rows
()==-1))
{
echo "沒有找到紀錄,或者刪除時出錯";
exit;
}
else{
echo "紀錄已刪除</br>";
echo "$sql</br>";
echo "$username";
}
}
mysql_close();
?>
修改成紅色部分就可以正常刪除資料庫資料了,感謝chenstin前輩
※ 編輯: ufly 來自: 125.231.208.76 (05/14 11:23)