教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php操作数据库入门实例(插入、编辑、删除)

php操作数据库入门实例(插入、编辑、删除)

发布时间:2015-11-15   编辑:jiaochengji.com
为大家整理了几个php操作数据库的入门实例,分为插入数据、编辑数据、删除指定的数据三部分,有需要的朋友,可以参考学习下。

1、首先,插入数据的代码。

<?  
 $action=$_GET['action'];  
 switch($action){  
  //添加记录  
  case"add";  
  $mail = trim(htmlspecialchars($_POST["mail"]));  
  $username = trim(htmlspecialchars($_POST["username"]));  
  $tel = trim(htmlspecialchars($_POST["tel"]));  
  $fax = trim(htmlspecialchars($_POST["fax"]));  
  $company = trim(htmlspecialchars($_POST["company"]));  
  $address = trim(htmlspecialchars($_POST["address"]));  
  $sql="insert into crm_company (mail,username,tel,fax,company,address,addtime) values
 ('$mail','$username','$tel','$fax','$company','$address','$addtime')";  
  //echo $sql;  
  mysql_query($sql,$conn);  
  print "<mce:script type="text/javascript"><!--location.href='c_companylist.php'; // --></mce:script>";//*/  
 }?> 

2、编辑指定的数据的代码

<?  
 $action=$_GET['action'];  
 switch($action){  
  //添加记录  
  case"edit";  
  $uid = trim(htmlspecialchars($_POST["uid"]));  
  $regtime = trim(htmlspecialchars($_POST["regtime"]));  
  $deadline = trim(htmlspecialchars($_POST["deadline"]));  
  $item = trim(htmlspecialchars($_POST["item2"]));  
  $price = trim(htmlspecialchars($_POST["price"]));  
  $product = trim(htmlspecialchars($_POST["product"]));  
  $content = trim(htmlspecialchars($_POST["content"]));  
 
  $sql="update crm_info set uid='$uid',regtime='$regtime',deadline='$deadline',item='$item',price='$price',product='$product',
content='$content' where id='$id'";  
  //echo $sql;  
  mysql_query($sql,$conn);  
  print "<mce:script type="text/javascript"><!-- location.href='c_infolist.php';  // --></mce:script>";//*/  
}?>

html:

 <form id="form1" name="form1" method="post"  action="?action=edit&id=<?php echo $rs['id'];?>" onSubmit="return checkadd()">
 </form> 

3、删除指定数据的代码
<a href="?action=del&id=<? echo $rs['id'];?>" onclick="return confirm('确定删除?')">删除</a>

<?  
 $action=$_GET['action'];  
 switch($action){  
  //删除记录  
  case"del";  
  $id=$_GET['id'];  
  $sql="delete from crm_info where id='$id'";  
  mysql_query($sql,$conn);  
  print "<mce:script type="text/javascript"><!--location.href='c_infolist.php';// --></mce:script>";  
  break;  
 }  
?>
为您推荐:
php mysql数据库操作类
php数据库操作类(实现表增删改查、取行数、查询多条数据等)
PHP数据库调用类的应用实例(详细注释)

您可能感兴趣的文章:
php操作数据库入门实例(插入、编辑、删除)
php入门教程:php mysql数据保存,删除,修改,更新,查询 操作
防订单重复提交策略方法
php数据库连接、查询、显示结果的小例子
python如何连mysql数据库
使用mysql二进制日志恢复mysql数据库的方法
php入门教程(索引)
十天学会php之第三天
PostgreSQL从菜鸟到专家系列教程(5)什么是数据库管理系统
PHP的作用是什么?

[关闭]
~ ~