php实现基本数据增删改操作之修改数据

先对要根据id值找到修改的信息并进行一个遍历展示 alter.php

<?php

include('./conn.php');
?>

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<meta charset="utf-8">
<body>

	

<?php

$id = $_GET['id'];

$sql = "select *from news where id = $id ";
$res  = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($res);
// echo $id;
echo '<form action="alter_add.php?id='.$id.'" method="post">';
	
echo '<table cellpadding="0" cellspacing="0">';
 echo  '<tr>';
	echo '<td class="leftlable">标题';
	echo	'</td>';
   echo     '<td>';
   echo     	'<input type="text" name="title" value="'.$row['title'].'">';
   echo     '</td>';
   	echo '</tr>';
echo	'<tr>';
	echo	'<td class="leftlable">作者';
	echo	'</td>';
      echo  '<td>';
        echo	'<input type="text" name="autor" value="'.$row['autor'].'">';
      echo ' </td>';
echo	'</tr>';
	echo '<tr>';
	echo	'<td class="leftlable">来源';
		echo	'</td>';
      echo  '<td>';
      echo  	'<input type="text" name="source" value="'.$row['source'].'">';
     echo   '</td>';
	echo '</tr>';
	echo '<tr>';
	echo	'<td class="leftlable">内容';
		echo '</td>';
     echo   '<td>';
        echo	'<textarea name="content" id="tbcontent" rows="10" cols="25" >';
        		echo $row['content'];
        echo	'</textarea>';
       echo' </td>';
	echo' </tr>';
	echo '<tr>';
	echo	'<td class="leftlable">&nbsp;';
	echo	'</td>';
	echo	'<td>';
	echo		'<input type="submit" id="btnsave" value="修改">';
	echo	'</td>';
	echo '</tr>';


?>




</table>

</form>
</body>
</html>

之后进行对应项目的修改操作: alter_add.php

<?php
// header('Content-type:text/html;charset=utf-8');
// $conn = @mysqli_connect('localhost','root','123456','itsource');
 include('./conn.php');
 mysqli_query($conn,'set names utf8'); 
$title = $_POST['title'];
$autor = $_POST['autor'];
$source = $_POST['source'];
$content = $_POST['content'];
// echo $title.$autor.$source.$content;

$id=$_GET['id'];
// (title,autor,source,content) values ('$title' ,'$autor','$source','$content')";
// echo $autor;
// echo $source;
// echo $content;
// echo $id;
$sql = "update news set title = $title ,autor = $autor, source = $source ,content = $content where id = $id"; 
$res  = mysqli_query($conn,$sql);
if($res)
{
	echo '<script>alert("恭喜你,修改成功"); location.href="list.php"</script>';
}else {
	echo '<script>alert("修改失败");</script>'.mysql_error($conn);
}
echo '<a href="save.html">返回</a>';
?>


发布了62 篇原创文章 · 获赞 102 · 访问量 3136

猜你喜欢

转载自blog.csdn.net/weixin_44763595/article/details/104997282