php最基本的增删改查

这是一些最基础的php增删该查,自我总结,方便以后复习回顾奋斗奋斗

form.php(表单提交)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
   <title></title>
</head>
<body>
<form method="post" action="sql.php">
   <h4>留言板</h4>
 <table>
 <tr>
   <td>请选择昵称:</td>
   <td><?php include('select.php')?></td>
 </tr>
 <tr>
   <td>留言内容:</td>
   <td><textarea name="text" rows="5" cols="10"></textarea></td>
 </tr>
 <tr>
   <td colspan=2><input type="submit" value="提交留言"></td>
    
 </tr>
 </table>
</form>    
</body>
</html>

sql.php(添加入库)

<?php
header("content-type:text/html;charset=utf-8");
 //接收表单值
 empty($_POST['names'])?$names="":$names=$_POST['names'];
 empty($_POST['text'])?$text="":$text=$_POST['text'];
 /*
连接数据库
选择数据库
写sql语句
选择
*/
mysql_connect("127.0.0.1","root","1111")or die('连接失败');
mysql_select_db("test")or die('选择失败');
$sql="insert into message(content,nid)values('$text','$names')";
//echo $sql;die;
$ra=mysql_query($sql);
if($ra){
echo ("<script>alert('添加成功');location.href='list.php'</script>");
}
else{
echo ("<script>alert('添加失败');location.href='list.php'</script>");
}
?>

list.php(展示)

<?php
header("content-type:text/html;charset=utf-8");
/*
连接数据库
选择数据库
写sql语句
选择
*/
mysql_connect("127.0.0.1","root","1111")or die('连接失败');
mysql_select_db("test")or die('选择失败');
$sql="select * from nickname";
//echo $sql;die;
$re=mysql_query($sql);
//将变量变成二维数组
while($arr=mysql_fetch_assoc($re)){
  $new_arr[]=$arr;
}  
//将二维数组循环到下拉列表中
?>
<select name="names">
<?php
foreach($new_arr as $k=>$v){
?>
<option value="<?php echo $v['nid']?>"><?php echo $v['name']?></option>
<?php
}
?>
</select>

delete.php(删除)

<?php
header("content-type:text/html;charset=utf-8");
$id=$_GET['id'];
/*
连接数据库
选择数据库
写sql语句
选择
*/
mysql_connect("127.0.0.1","root","1111")or die('连接失败');
mysql_select_db("test")or die('选择失败');
$sql="delete from message where mid='$id'";
//echo $sql;die;
$re=mysql_query($sql);
 if($re){
 echo ("<script>alert('删除成功');location.href=' list.php'</script>");
 }
else{
echo ("<script>alert('删除失败');location.href=' list.php'</script>");
}
?>



update1.php(修改----查询修改内容)

<?php
header("content-type:text/html;charset=utf-8");
$cc=$_GET['cc'];
/*
连接数据库
选择数据库
写sql语句
选择
*/
mysql_connect("127.0.0.1","root","1111")or die('连接失败');
mysql_select_db("test")or die('选择失败');
$sql="select * from message where mid='$cc'";
//echo $sql;die;
$re=mysql_query($sql);
//将变量变成二维数组
 $arr=mysql_fetch_assoc($re); 
   
?>
<pre name="code" class="php"><meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 <form method="post" action="update.php">
   <h4>修改板</h4>
 <table>
 <tr>
   <td>请选择昵称:</td>
   <td><?php include('select.php')?></td>
 </tr>
 <tr>
   <td>留言内容:</td>
   <td><textarea name="text" rows="5" cols="10"></textarea></td>
 </tr>
 <tr>
   <td colspan=2><input type="submit" value="确认修改"></td>   
 </tr>
 </table>
 <input type="hidden" name="xg" value="<?php echo $arr['mid']?>">
</form>   


 

update.php(修改----修改操作)

<?php
header("content-type:text/html;charset=utf-8");
 
//接收表单值
 empty($_POST['names'])?$names="":$names=$_POST['names'];
 empty($_POST['text'])?$text="":$text=$_POST['text'];
 empty($_POST['xg'])?$xg="":$xg=$_POST['xg'];
/*
连接数据库
选择数据库
写sql语句
选择
*/
mysql_connect("127.0.0.1","root","1111")or die('连接失败');
mysql_select_db("test")or die('选择失败');
$sql="update message set name='$names',content='$text' where mid='$xg'";
$re=mysql_query($sql);
 if($re){
 echo ("<script>alert('修改成功');location.href=' list.php'</script>");
 }
else{
echo ("<script>alert('修改失败');location.href=' list.php'</script>");
}
?>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值