php增删改查的添加怎么添加性别代码

如题所述

方法/步骤

链接数据库方法:conn.php 。
代码如下:
<?php
$link=mysql_connect('localhost','root','tianqing')or die('数据库连接错误');
mysql_select_db('studyexam',$link)or die('数据库访问错误');
mysql_query("set names 'utf8'");
?>

增加 add.php
代码如下:
<?php
include("conn.php");//引入链接数据库
if(!empty($_POST['tj'])){
$title=$_POST['title'];
$con=$_POST['con'];
echo $sql="insert into news(id,title,dates,contents) value (null,'$title',now(),'$con')" ;
mysql_query($sql);
echo"插入成功";
}
?>
<form action="add.php" method="post">
标题: <input type="text" name="title"><br /><br />
内容: <textarea rows="6" cols="55" name="con"></textarea><hr>
<input type="submit" name="tj" value="提交">
</form>

删除del.php
代码如下:
<?php
include("conn.php");//引入链接数据库<pre name="code" class="html"><?php
include("conn.php");//引入链接数据库
if(!empty ($_GET['id'])){
$sql="select * from news where id='".$_GET['id']."'";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
}
if(!empty($_POST['sub'])){
$title=$_POST['title'];
$con=$_POST['con'];
$hid=$_POST['hid'];
$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 ";
mysql_query($sql);
echo "<script> alert('更新成功'); location.href='index.php'</script>";
echo"更新成功";
}
?>
<form action="edit.php" method="post">
<input type="hidden" name="hid" value="<?php echo $rs['id']?>"/>
标题: <input type="text" name="title" value="<?php echo $rs['title']?>"><br>
内容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br>
<input type="submit" name="sub" value="发表">
</form></pre><br>
<?php
if(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=mysql_query($sql); echo "删除成功"; ?><p></p>
<pre></pre>
<br>

改 edit.php页面
代码如下:
<?php
include("conn.php");//引入链接数据库
if(!empty ($_GET['id'])){
$sql="select * from news where id='".$_GET['id']."'";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
}
if(!empty($_POST['sub'])){
$title=$_POST['title'];
$con=$_POST['con'];
$hid=$_POST['hid'];
$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 ";
mysql_query($sql);
echo "<script> alert('更新成功'); location.href='index.php'</script>";
echo"更新成功";
}
?>
<form action="edit.php" method="post">
<input type="hidden" name="hid" value="<?php echo $rs['id']?>"/>
标题: <input type="text" name="title" value="<?php echo $rs['title']?>"><br>
内容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br>
<input type="submit" name="sub" value="发表">
</form></pre><br>

查询select.php页面
代码如下:
<?php
include("conn.php");
$sql="select * from entries";//entries这是数据库名词,大家自己改
if($result=mysql_query($sql)){
/*
$num=mysql_num_rows($result)
for($i=1;$i<=$num;$i++);
*/
while($array=mysql_fetch_assoc($result)){
//$biaoti=$array['title'];
$neirong=$array['entry'];
$id=$array['entry_id'];
echo "<p>{$array['title']}</p><p>$neirong</p><p><a href='edit.php?id=$id'>编辑</a>
<a href='delete.php?id=$id'>删除</a></p><hr />";
}
}
?>
温馨提示:答案为网友推荐,仅供参考
相似回答