练习---新闻界面

首页:

<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>id</td>
<td>titlr</td>
<td>author</td>
<td>source</td>
<td>date</td>
<td>update</td>
<td>delete</td>
</tr>

<?php
$db = new MySQLi("localhost","root","","news");
!mysqli_connect_error() or die("连接失败!");
$sql = "select * from new";
$result = $db->query($sql);
if($result)
{
$attr = $result->fetch_all();
foreach ($attr as $v)
{
echo "<tr>
<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$v[2]}</td>
<td>{$v[3]}</td>
<td>{$v[5]}</td>
<td><a href='update.php?newsid={$v[0]}'>update</a></td>
<td><a href='delete.php?newsid={$v[0]}'>delete</a></td>
</tr>";
}
}

?>
</table>
<div><a href="fb.php">发布新闻</a></div>

发布新闻:


<style>
*
{
margin:0px auto;
padding:0px;
}
.tj
{
height:20px;
30px;
margin-left:100px;
position:relative;
}
.cl
{
height:20px;
30px;
position:relative;
left:140px;
top:-20px;
}
</style>


<h1 align="center" style="margin-top:100px">发布新闻</h1><br />
<form style=" margin-left:500px" action="tj.php" method="post">
<div>标题:<input type="text" name="title" style="300px;" /></div><br />
<div>作者:<input type="text" name="name" style="220px;" /></div><br />
<div>来源:<input type="text" name="froms" style="220px;" /></div><br />
<div style="vertical-align:middle;line-height:120px; float:left ">内容:</div><div style="float:left"><textarea name="nr" style="400px; height:120px; "></textarea></div>
<div style="clear:both"></div><br />
<br />
<div><input type="submit" value="提交" class="tj" /></div>
<div><a href="main.php"><input type="button" value="查看" class="cl" /></a></div>
</form>
发布处理:

<?php
$title = $_POST["title"];
$name = $_POST["name"];
$froms = $_POST["froms"];
$nr = $_POST["nr"];
$db = new MySQLi("localhost","root","","news");
!mysqli_connect_error() or die("连接失败!");
$sql1 = "select now()";
$r1 = $db->query($sql1);
$a = $r1->fetch_row();
$sql = "insert into new values ('','{$title}','{$name}','{$froms}','{$nr}','{$a[0]}')";
$r = $db->query($sql);
if($r)
{
header("location:fb.php");
}
else
{
echo "发布失败!";
}

修改:

<style>
*
{
margin:0px auto;
padding:0px;
}
.tj
{
height:20px;
30px;
margin-left:100px;
position:relative;
}
.cl
{
height:20px;
30px;
position:relative;
left:140px;
top:-20px;
}
</style>

<h1 align="center" style="margin-top:100px;">修改新闻</h1><br />
<?php
$newsid = $_GET["newsid"];
$db = new MySQLi("localhost","root","","news");
!mysqli_connect_error() or die("连接失败!");
$sql = "select * from new where newsid='{$newsid}'";
$re = $db->query($sql);
if($re)
{
$attr = $re->fetch_all();
}
?>
<form style=" margin-left:500px" action="updatechuli.php" method="post">
<div><input type="hidden" name="newsid" value="<?php echo $attr[0][0]; ?>"</div>
<div>标题:<input type="text" name="title" value="<?php echo $attr[0][1]; ?>" style="300px;" /></div><br />
<div>作者:<input type="text" name="name" value="<?php echo $attr[0][2]; ?>" style="220px;" /></div><br />
<div>来源:<input type="text" name="froms" value="<?php echo $attr[0][3]; ?>" style="220px;" /></div><br />
<div style="vertical-align:middle;line-height:120px; float:left ">内容:</div><div style="float:left"><textarea name="nr" style="400px; height:120px; "><?php echo $attr[0][4]?></textarea></div>
<div style="clear:both"></div><br />
<br />
<div><input type="submit" value="修改" class="tj" /></div>
<div><a href="main.php"><input type="button" value="查看" class="cl" /></a></div>
</form>
修改处理:

<?php
$newsid = $_POST["newsid"];
$title = $_POST["title"];
$name = $_POST["name"];
$froms = $_POST["froms"];
$nr = $_POST["nr"];
$db = new MySQLi("localhost","root","","news");
!mysqli_connect_error() or die("连接失败!");
$sql1 = "select now()";
$r1 = $db->query($sql1);
$a = $r1->fetch_row();
$sql = "Update new set title='{$title}',author='{$name}',source='{$froms}',content='{$nr}',times='{$a[0]}' where newsid='{$newsid}'";
$r = $db->query($sql);
if($r)
{
header("location:update.php?newsid={$newsid}");
}
else
{
echo "修改失败!";
}

删除:

<?php
$newsid = $_GET["newsid"];
$db = new MySQLi("localhost","root","","news");
!mysqli_connect_error() or die("连接失败!");
$sql = "delete from new where newsid='{$newsid}'";
$r = $db->query($sql);
if($r)
{
header("location:main.php");
}
else
{
echo "删除失败!";
}

原文地址:https://www.cnblogs.com/1116zsc/p/5470535.html