php表单修改数据库

  • 2,013

php表单修改数据库

<?php
$conn=mysql_connect('localhost','root','12345678');
if($conn){
	print_r($conn);
}else{
	exit;
}
$sql="use test";
$rs=mysql_query($sql,$conn);

$sql='select * from stu';
$rs=mysql_query($sql,$conn);
var_dump($rs);

$list=array();
while($row=mysql_fetch_assoc($rs)){
	$list[]=$row;
}
//二维数组
//print_r($list);
//Array ( [0] => Array ( [id] => 22 [sname] => ijabde [yy] => ijabde ) [1] => Array ( [id] => 61 [sname] => stu5' [yy] => yy33333 ) [2] => Array ( [id] => 83 [sname] => bvntmh [yy] => bvntmh ) [3] => Array ( [id] => 58 [sname] => xingming58 [yy] => yyy585858 ) ) 


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
</head>
    <body>
    	<h2> 公益一期学员</h2> 
    	<table border='1'>
    		<tr>
    			<td>id</td>
    			<td>姓名</td>
    			<td>YY</td>
    			<td>操作 </td>
    		</tr>
    		<?php
    		foreach ($list as $v) {
    			echo  '<tr>';
    			echo '<td>',$v['id'],'</td>';
    			echo '<td>',$v['sname'],'</td>';
    			echo '<td>',$v['yy'],'</td>';
    			//echo '<td></td>';
    			echo '<td><a href="mysql08.php?id=',$v['id'],'">编辑</a></td>';
    		    echo '</tr>';
    		}
    		?>
    	</table>
    </body>
</html>


<?php
//print_r ($_GET);

 $conn=mysql_connect('localhost','root','12345678');
if($conn){
	print_r($conn);
}else{
	exit;
}
$sql="use test";
$rs=mysql_query($sql,$conn);
$id=isset($_GET['id']) ? $_GET[id]+0:0;
$sql='select * from stu where id='.$id;
$rs=mysql_query($sql,$conn);

if (!$rs) {
	echo '没有打通豆浆';
	exit;

}
$stu = mysql_fetch_assoc($rs);//为什么不用while取?  因为只有一条数组! 
//var_dump($stu);
if (empty($stu)) {
	echo'没有这个用户';
	exit;
}
print_r($stu);


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="布尔教育 http://www.itbool.com" />
</head>
    <body>
    	


    	<form action="mysql08-1.php" method="post">

    		<pre>
    			接收地址栏的参数
    			并根据参数拼接sql语句查询
    		</pre>
    		学生id:<input type="text" name="id" value="<?php echo $stu['id']; ?>"/></br>
    		新姓名:<input type="text" name="sname" value="<?php echo $stu['sname']; ?>"/></br>
    		新YY:<input type="text" name="yy" value="<?php echo $stu['yy']; ?>"/></br>
    		<input type="submit" value="修改 提交" />
    	</form>
    </body>
</html>
<?php

//链接
$conn=mysql_connect('localhost','root','12345678');
if($conn){
	print_r($conn);
}else{
	exit;
}

//选库
$sql="use test";
//发送sql进行查询
$rs=mysql_query($sql,$conn);//沿着conn通道进行查询



//接收表单数据
//addslashes() 函数返回在预定义字符之前添加反斜杠的字符串。
$sname=addslashes($_POST['sname']);
$yy=addslashes($_POST['yy']);
$id=$_POST['id']+0;


$sql="update stu set sname='" . $sname . "',yy='" . $yy . "'where id =" . $id;


echo $sql,'</br>';


if(mysql_query($sql,$conn)){
	echo "修改成功";
}else{
	echo "修改失败";
}



?>
<a href="mysql8.php">用户列表</a>
weinxin
我的微信
这是我的微信扫一扫
php最后更新:2016-5-29
开拓者博主
  • 本文由 发表于 2016年5月26日19:32:27
  • 转载请务必保留本文链接:https://www.150643.com/12.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: