php數據怎么刪除代碼,PHP刪除
如何用PHP寫一段刪除數據庫表中的一條記錄?急求啊
//刪除是記錄是用sql語句,比如 delete from xxx(table) where id=1 刪除 id=1的記錄
成都創新互聯公司是一家專業提供塔城企業網站建設,專注與成都做網站、網站制作、H5場景定制、小程序制作等業務。10年已為塔城眾多企業、政府機構等服務。創新互聯專業網站制作公司優惠進行中。
//數據庫操作后,手動關閉是好得習慣,不手動關閉,php程序也會最后自動關閉
PHP怎么實現批量刪除 實現批量刪除的代碼
1.前臺
!DOCTYPE html
html
head
title批量刪除/title
/head
body
scripttype="text/javascript"
//復選框
function checkall(all)
{
var ck = document.getElementsByClassName("ck");
if(all.checked)
{
for(var i=0;ick.length;i++)
{
ck[i].setAttribute("checked","checked");
}
}
else
{
for(vari=0;ick.length;i++)
{
ck[i].removeAttribute("checked");
}
}
}
/script
formaction="test.php"method="post"
tableborder="1"
trthinputtype="checkbox"name="all"onclick="checkall(this)"/id/thth名字/th/tr
!-- 此處調用顯示列表函數 --
?phpshow() ?
trtdcolspan="3"inputtype="submit"value="批量刪除"/td/tr
/table
/form
/body
?php
//顯示列表
function show()
{
//連接數據庫
@mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query('set names utf8');
$sql="select id,name from test";
$res=mysql_query($sql);
//循環取出數據
while($row=mysql_fetch_row($res))
{
echo "tr
td
inputtype='checkbox'value='{$row[0]}'name='item[]'class='ck'/
{$row[0]}
/td
td{$row[1]}/td
/tr";
}
}
?
/html
2.后臺
?php
//接收post傳來的數組
$arr=$_POST["item"];
/**
* 批量刪除
* 思路:把前臺批量選擇的數據放在數組里,刪除該數組即可
* @param $arr
* @return $res 成功or失敗
*/
functionbatch_del($arr)
{
@mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query('set names utf8');
//把數組元素組合為字符串:
$str= implode("','",$arr);
//in 表示多個
$sql="delete from test where id in('{$str}')";
$res= mysql_query($sql);
if(!$res){
echo"刪除失敗";
}else{
if(mysql_affected_rows()0){
echo"刪除成功";
}else{
echo"沒有行受到影響";
}
}
}
//調用批量刪除函數
batch_del($arr);
php怎么刪除json里面的數據?
參考方法就是先把文件讀出來,把不要的數組元素刪了后再寫回去;
參考代碼如下:
// std::string jsonPath // json文件路徑
Json::Reader reader;
Json::Value root;
ifstream is;
is.open (jsonPath.c_str(), std::ios::binary );
if (reader.parse(is, root))
{
std::string code;
Json::Value value;
int size = root.size();
for (int i = 0; i size; i++)
{
if(條件)
{
value[i] = root[i];
}
}
is.close();
Json::FastWriter writer;
std::string json_append_file = writer.write(value);
std::ofstream ofs;
ofs.open(jsonPath.c_str());
ofs json_append_file;
ofs.close();
}
網站欄目:php數據怎么刪除代碼,PHP刪除
文章地址:http://www.xueling.net.cn/article/dsegdph.html