用php做 mysql 一张表的 增、删、改、查。

2024-11-23 05:11:51
推荐回答(2个)
回答1:

$link=mysql_connect("localhost","root","123456")
or die("数据库服务器连接失败!
");
mysql_query("CREATE DATABASE student",$link)
or die("数据库student创建失败!
");
echo "数据库student创建成功!
";
mysql_select_db("student",$link)
or die("数据库student选择失败!
");
echo "数据库student选择成功!
";
//创建表t_student
$sql="CREATE TABLE t_student(xh CHAR(11) NOT NULL,";
$sql.="xm VARCHAR(12) NOT NULL,xb CHAR(2) NOT NULL,";
$sql.="bjdm CHAR(9) NOT NULL,csrq DATE,";
$sql.="PRIMARY KEY(xh))";
$sql.="ENGINE=MyISAM DEFAULT CHARSET=gb2312";
mysql_query($sql,$link) or die("表t_student创建失败!
");
echo "表t_student创建成功!
";
//创建表t_class
$sql="CREATE TABLE t_class(bjdm CHAR(9) NOT NULL,";
$sql.="bjjc VARCHAR(15) NOT NULL,bjqc VARCHAR(30) NOT NULL,";
$sql.="PRIMARY KEY(bjdm))";
$sql.="ENGINE=MyISAM DEFAULT CHARSET=gb2312";
mysql_query($sql,$link) or die("表t_class创建失败!
");
echo "表t_class创建成功!
";
$sql="ALTER TABLE t_class ADD bj CHAR(3) NOT NULL";
mysql_query($sql,$link) or die("字段添加失败!
");
//在表t_class中修改字段bj
$sql="ALTER TABLE t_class CHANGE bj bj CHAR(5) NOT NULL";
mysql_query($sql,$link) or die("字段修改失败!
");
//在表t_class中删除字段bj
$sql="ALTER TABLE t_class DROP bj";
mysql_query($sql,$link) or die("字段删除失败!
");
echo "表t_class修改成功!
";
$sql="select bjdm,bjjc,bjqc from t_class where bjdm='200501011'";
$result=mysql_query($sql,$link);
$row = mysql_fetch_array($result);
//$row = mysql_fetch_row($result);
//$row = mysql_fetch_assoc($result);
if (!$row) {
echo "无此班级代码!";
die();
}
$bjdm=$row['bjdm'];
$bjjc=$row['bjjc'];
$bjqc=$row['bjqc'];
echo "班级代码:".$bjdm."
";
echo "班级简称:".$bjjc."
";
echo "班级全称:".$bjqc."
";
$sql="insert into t_class(bjdm,bjjc,bjqc)";
$sql=$sql." values('2006','计1','计算机1班')";
if (mysql_query($sql,$link))
echo "班级增加成功!";
else
echo '班级增加失败!';
$sql="delete from t_class where bjdm='2006');
mysql_query($sql,$link);
$sql="DROP TABLE t_class";
mysql_query($sql,$link) or die("表t_class删除失败!
");
echo "表t_class删除成功!
";

回答2:

1增

insert.php







ID:




给自己起一个名号:




给自己设一个密码:




你的email地址:










doinsert.php


$linkID=@mysql_connect("localhost","root","123456");
if(!$linkID){
echo "没连接上";
}
$ss=@mysql_select_db("bean");
if(!$ss){
echo "没找到数据库";
}
$id = $_POST['id'];
$name = $_POST['name'];
$password = $_POST['password'];
$email = $_POST['email'];

$query = "INSERT INTO member3 (id,name,password,email) values(
'$id','$name','$password','$email')";
//$query = "insert into member3 set
// id='$id',name='$name',password='$password',email='$email'";

$result = mysql_query($query);

if($result) echo "

member successfuly insert!

";
else echo "

There was a problem inserting the member!

";

mysql_close();
include "insert.php" //返回并继续插入
?>

2查询并且修改

form.php












modify.php


$linkID=@mysql_connect("localhost","root","123456");
$ss=@mysql_select_db("bean");

$rowID = $_POST['rowID'];
$query="select id ,name,password,email from member3 where id='$rowID'";
$result=mysql_query($query);
if(!$result){
echo "没执行成功";
}
//echo "$result";

list($id,$name,$password,$email)=mysql_fetch_row($result);
//echo "$id";
//echo "$name";
//echo "$password";
//echo "$email";
mysql_close();
include "modifyform.php";

?>

modifyform.php








ID:

value="" />



你的名号:

value="" />



你的密码:

value="" />



你的邮箱:

value="" />









update.php

$linkID=@mysql_connect("localhost","root","123456");
$db=@mysql_select_db("bean");

$rowID=$_POST['rowID'];
$id=$_POST['id'];
$name=$_POST['name'];
$password=$_POST['password'];
$email=$_POST['email'];
//echo $rowID;
$query=" UPDATE member3 SET id='$id',name='$name',password='$password',email='$email'where id='$rowID' ";
//echo $query;
$result=mysql_query($query);

if($result) echo "

member successfuly update!

";
else echo "

There was a problem updating the member!

";

mysql_close();
?>

3删除 多选删除

deleteform.php 复选框






1 zcl

2 lv

3 ed

4 d







deleteform1.php 多选下拉列表












delete.php 遍历数组删除

$linkID=@mysql_connect("localhost","root","123456");
$db=@mysql_select_db("bean");
//echo $_POST['kk'];
//echo array('1','2','3');
foreach($_POST['kk'] as $pp)//遍历数组,给每个赋值
{
echo $pp;
$rowID=$pp;
$query="delete from member3 where id='$rowID'";//查询语句
$result=mysql_query($query);//放到数据库执行

if(!mysql_affected_rows()==0)
echo "

选择的项".$rowID."已经成功的被删除!

" ;

if(mysql_affected_rows()==0){
echo "

没有找到记录!

" ;
}
}