HTML鏈接數(shù)據(jù)庫(kù)垦搬,執(zhí)行添加刪除操作
添加操作
php代碼
創(chuàng)建數(shù)據(jù)庫(kù)
<?php
$content=$_REQUEST['content'];
$userName=$_REQUEST['userName'];
$phone=$_REQUEST['phone'];
$pubTime=$_REQUEST['pubTime'];
$_REQUEST[]:接收從html中傳來的參數(shù)傍念,變量
鏈接數(shù)據(jù)庫(kù)
$conn=mysqli_connect("127.0.0.1","root","","ifeng",3306);
127.0.0.1:主機(jī)名
root:用戶名
'':密碼笼才,因?yàn)闆]有所以為空
sbsh:數(shù)據(jù)庫(kù)名
3306:數(shù)據(jù)庫(kù)的端口號(hào)
設(shè)置編碼方式
$sql="SET NAMES UTF8";
mysqli_query($conn,$sql);
執(zhí)行插入語句
$mysql="INSERT INTO comment VALUES(NULL,'$content','$userName','$phone','$pubTime')";
$result=mysqli_query($conn,$mysql);
輸出$result判讀true還是false
var_dump($result);
true表示成功步责,false表示失敗
html代碼
用form表單完成
<form action='ifeng_new.php'>
<p>
內(nèi)容:<input type="" name="content" value=''>
</p>
<p>
用戶名: <input type="" name="userName">
</p>
<p>
電話: <input type="" name="phone">
</p>
<p>
發(fā)布日期: <input type="" name="pubTime">
</p>
<p>
<input type="submit" name="" value='提交'>
</p>
</form>
action:規(guī)定當(dāng)提交表單時(shí)拔疚,向何處發(fā)送表單數(shù)據(jù)变勇。后面跟路徑
name: 屬性用于對(duì)提交到服務(wù)器后的表單數(shù)據(jù)進(jìn)行標(biāo)識(shí)
只有設(shè)置了 name 屬性的表單元素才能在提交表單時(shí)傳遞它們的值恤左。
submit:創(chuàng)建一個(gè)提交按鈕
刪除操作
php代碼
<?php
//需要評(píng)論編號(hào)
$cid=$_REQUEST['cid'];
//2.連接數(shù)據(jù)庫(kù)
$conn=mysqli_connect('127.0.0.1','root','','ifeng',3306);
//3.設(shè)置編碼
$sql="SET NAMES UTF8";
mysqli_query($conn,$sql);
//4.執(zhí)行sql語句
$sql="DELETE FROM comment WHERE cid='$cid'";
$result=mysqli_query($conn,$sql);
// var_dump($result);
if($result===true){
echo '刪除成功';
}else{
echo "刪除失敗";
}
html代碼
<form action="ifeng_delete.php">
<p>請(qǐng)輸入編號(hào): <input type="" name="cid"></p>
<p>
<input type="submit" name="" value='提交'>
</p>
</from>