PHP MySQLi 增刪改查

來源:互聯網
上載者:User

標籤:

     最近幾天,我們一直在學習利用MySQLi訪問資料庫並對其中的資料進行操作。今天給大家展現一個完整的例子,我們來製作一個新聞發布系統,利用MySQLi來操作資料庫,實現對新聞的添加、修改、刪除、查詢等準系統。(以下代碼分為最上層顯示和後台php處理代碼,中間用空行隔開,注意區分)

1、登陸頁面:這由本公司內部人員通過員工號和社會安全號碼才能登陸,代碼如下://最上層顯示部分

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<h2>管理員登陸</h2>
<form action="dlyz.php" method="post">
<div>使用者名稱:<input type="text" name="user" value="請輸入您的工號" /></div>
<br />
<div>密&nbsp;&nbsp;碼:<input type="password" name="psd" /></div>
<br />
<input type="submit" value="登入" />
<input type="submit" value="註冊新使用者" formaction="zhuc.php"/>
</form>
</body>
</html>

//php代碼對提交登陸的資訊進行處理

<?php

$user = $_POST["user"];
$psd = $_POST["psd"];


//造對象
$db = new MySQLi("localhost","root","","newssystem");

//判斷是否出錯
!mysqli_connect_error() or die("串連失敗!!");

//寫sql語句

$sql = "select psd from yonghu where user=‘{$user}‘";


//執行SQL語句

$result = $db-> query($sql);
$v = $result->fetch_row();
if($psd==$v[0])
{
header("location:fabuxinwen.php");
}
else
{
echo"您輸入的使用者名稱或密碼不正確,請重新輸入!!";
}

2、註冊頁面:公司/報社來了新員工,只有註冊後才能登陸

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<h2>歡迎註冊</h2>
<body>

<form action="zhucyz.php"method="post">
<div>使用者名稱:<input type="text" name="user" value="請輸入您的工號"/></div><br />

<div>密&nbsp;&nbsp;碼:<input type="password" name="psd" /></div><br />

<input type="submit" value="提交" />
</form>

</body>
</html>

 

<?php
$user = $_POST["user"];
$psd = $_POST["psd"];


//造對象
$db = new MySQLi("localhost","root","","newssystem");

//判斷是否出錯
!mysqli_connect_error() or die("串連失敗!!");

//寫sql語句

$sql = "insert into yonghu values(‘{$user}‘,‘{$psd}‘)";

//執行SQL語句

$result = $db-> query($sql);

if($result)
{
header("location:dl.php");
}
else
{
echo"很抱歉,註冊失敗!!";
}

 

3、登陸進去以後,是發布新聞頁面,點擊提交按鈕進行提交儲存到已經建立好的資料庫,點擊查看按鈕進行查看確認

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<div style="width:100%; text-align:center" >
<h2>發布新聞</h2>
<form method="post">
<input type="hidden" name="newsid"/>
<table style="margin:0 auto; text-align:left" >
<tr>
<td >標題:</td><td><input type="text" style="width:400px" name="title" /></td>
</tr>
<tr>
<td >
</td><td><input type="text" style="width:400px" name="Author" /></td>
</tr>
<tr>
<td >來源:</td><td><input type="text" style="width:400px" name="source"/></td>
</tr>
<tr>
<td >內容:</td>
<td><textarea cols="auto" rows="auto" style="width:400px; height:400px" name="content"></textarea></td>
</tr>

</table><br />
<?php
$time = date(‘y-m-d h:i:s‘);

echo "<input type=\"hidden\" name=\"time\" value=\"{$time}\"/>";
?>

<input type="submit" value="提交" formaction="tijiao.php"/>

<input type="submit" value="查看" formaction="chakan.php"/>
</form>
</div>
</body>
</html>

 

<?php
$title = $_POST["title"];
$Author = $_POST["Author"];
$source = $_POST["source"];
$content = $_POST["content"];

//造對象
$db = new MySQLi("localhost","root","","newssystem");

//判斷是否出錯
!mysqli_connect_error() or die("添加失敗!!");

//寫sql語句

$sql = "insert into news(title,Author,source,content) values(‘{$title}‘,‘{$Author}‘,‘{$source}‘,‘{$content}‘)";


//執行SQL語句

$result = $db-> query($sql);

if($result)
{
header("location:fabuxinwen.php");
}
else
{
echo"很抱歉,添加失敗!!";
}

 

4、查看頁面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<table width="70%" border="1px" cellpadding="0" cellspacing="0" style="text-align:center">
<tr>
<td>編號</td>
<td>標題</td>
<td>作者</td>
<td>來源</td>
<td>日期</td>
<td>刪除</td>
<td>修改</td>

</tr>

<?php
$db=new mysqli("localhost","root","","newssystem");
!mysqli_connect_error() or die("串連錯誤");
$sql="select * from news";
$result=$db->query($sql);
while($attr=$result->fetch_row())
{
echo "
<tr>
<td>{$attr[0]}</td>
<td>{$attr[1]}</td>
<td>{$attr[2]}</td>
<td>{$attr[3]}</td>
<td>{$attr[5]}</td>
<td><a onclick=\" return confirm(‘確定刪除‘)\" href=‘scchuli.php?newsid={$attr[0]}‘>刪除</a></td>
<td><a href=‘xiugai.php?newsid={$attr[0]}‘>修改</a></td>
</tr>
";
}

?>
</table>
</body>
</html>

5、在查看頁面可以進行相關內容的修改和刪除//修改:在點擊修改按鈕後,會跳轉到修改頁面,此時會顯示出之前已經發布的內容、標題等相關內容

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<?php
$id = $_GET["newsid"];

$db=new mysqli("localhost","root","","newssystem");
!mysqli_connect_error() or die("串連錯誤");
$sql="select * from news where newsid=‘{$id}‘";
$result=$db->query($sql);
$attr=$result->fetch_row();
?>
<div style="width:100%; text-align:center" >
<h2>修改新聞</h2>
<form action="xgchuli.php" method="post">
<input type="hidden" name="newsid" <?php echo "value=‘{$attr[0]}‘";?>/>
<table style="margin:0 auto; text-align:left" >
<tr>
<td >標題:</td><td><input type="text" style="width:400px" name="title" <?php echo "value=‘{$attr[1]}‘";?>/></td>
</tr>
<tr>
<td >
</td><td><input type="text" style="width:400px" name="Author" <?php echo "value=‘{$attr[2]}‘";?>/>
</td>
</tr>
<tr>
<td >來源:</td><td><input type="text" style="width:400px" name="source" <?php echo "value=‘{$attr[3]}‘";?>/>
</td>
</tr>
<tr>
<td >內容:</td>
<td><textarea cols="auto" rows="auto" style="width:400px; height:400px" name="content"><?php echo "{$attr[4]}";?>
</textarea></td>
</tr>

</table><br />
<?php
$time = date(‘y-m-d h:i:s‘);

echo "<input type=\"hidden\" name=\"time\" value=\"{$time}\"/>";
?>

<div><a href="chakan.php"><input type="button" title="查看" value="查看" /></a><input type="submit" title="修改" value="修改"/>
</div>

</form>

</body>
</html>

 

<?php

$id=$_POST["newsid"];
$title=$_POST["title"];
$Author=$_POST["Author"];
$source=$_POST["source"];
$content=$_POST["content"];
$time=$_POST["time"];

$db = new MySQLi("localhost","root","","newssystem");

!mysqli_connect_error() or die("串連失敗");

$sql="update news set title=‘{$title}‘,Author=‘{$Author}‘,source=‘{$source}‘,content=‘{$content}‘,time=‘{$time}‘ where newsid=‘{$id}‘ ";

$result=$db->query($sql);
if ($result)
{
header("location:chakan.php");
}
else
{
echo "修改失敗";
}

 

//刪除:如果要刪除一條新聞,在點擊刪除按鈕之後,不會跳轉到任何前台顯示頁面,只會在後台通過php代碼進行相關處理

<?php
$id=$_GET["newsid"];

$db=new mysqli("localhost","root","","newssystem");

!mysqli_connect_error() or die("串連失敗");

$sql="delete from news where newsid=‘{$id}‘";

$result=$db->query($sql);

if ($result)
{
header("location:chakan.php");
}
else
{
echo "刪除失敗";
}

6、修改完成之後,點擊頁面下方的修改按鈕進行提交,在這裡也不會有相關的最上層顯示頁面,只是在後台利用相關php代碼進行相應處理,成功後返回傳布新聞頁面

<?php
$title = $_POST["title"];
$Author = $_POST["Author"];
$source = $_POST["source"];
$content = $_POST["content"];

//造對象
$db = new MySQLi("localhost","root","","newssystem");

//判斷是否出錯
!mysqli_connect_error() or die("添加失敗!!");

//寫sql語句

$sql = "insert into news(title,Author,source,content) values(‘{$title}‘,‘{$Author}‘,‘{$source}‘,‘{$content}‘)";


//執行SQL語句

$result = $db-> query($sql);

if($result)
{
header("location:fabuxinwen.php");
}
else
{
echo"很抱歉,添加失敗!!";
}

 

PHP MySQLi 增刪改查

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.