Re: [請益] 如何用checkbox foreach 刪除多筆資料
※ 引述《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
→
05/14 10:01, , 1F
05/14 10:01, 1F
→
05/14 10:02, , 2F
05/14 10:02, 2F
→
05/14 10:06, , 3F
05/14 10:06, 3F
→
05/14 10:06, , 4F
05/14 10:06, 4F
→
05/14 10:07, , 5F
05/14 10:07, 5F
→
05/14 11:21, , 6F
05/14 11:21, 6F
<?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)
討論串 (同標題文章)
PHP 近期熱門文章
PTT數位生活區 即時熱門文章