我有一个页面,我正在制作一个非常简单的CMS,页面显示3个产品,它从数据库中获取标题,描述,价格,产品包含和产品不包括。这工作,不起作用的是我创建的一个表单,可以轻松更改我的数据库中的内容。现在,当我填写表单时,不是将表单中的所有数据添加到数据库,而是添加除最后一个输入字段之外的表单中的所有数据。如果我只尝试使用单个字段更新数据库中的单个项目,那么只有该项目会更新,但所有其他项目都会被删除。
这是我的代码。
<h2>Deal 1</h2>
<form method="POST" action="#">
<table>
<tr>
<td><label>Title: </label><br><input type="text" name="title1" placeholder="<?php echo htmlspecialchars($deal1title); ?>"></td>
</tr>
<tr>
<td>
<label>Description: </label><br><textarea style="height: 200px;" name="desc1" placeholder="<?php echo htmlspecialchars($deal1desc); ?>"></textarea>
</td>
</tr>
<tr>
<td><label>Price: </label><br><input type="text" name="price1" placeholder="<?php echo htmlspecialchars($deal1price); ?>"></td>
</tr>
<tr>
<td><label>Include: </label><br><input type="text" name="include1" placeholder="<?php echo htmlspecialchars($deal1inc); ?>"></td>
</tr>
<tr>
<td><label>Not Include: </label><br><input type="text" name="notinc1" placeholder="<?php echo htmlspecialchars($deal1noinc); ?>"></td>
</tr>
<tr>
<td><input type="submit" name="submit1" value="Save"></td>
</tr>
</table>
</form>
这是连接
<?php
$connect =mysqli_connect('localhost', 'root', 'password', 'specials' );
//Error Check//
if(mysqli_connect_errno($connect)){
echo "failed to connect";}
$fetcher = mysqli_query($connect, "SELECT * FROM specials WHERE id=1");
$fetcher2 = mysqli_query($connect, "SELECT * FROM specials WHERE id=2");
$fetcher3 = mysqli_query($connect, "SELECT * FROM specials WHERE id=3");
$deal1 = mysqli_fetch_array($fetcher);
$deal2 = mysqli_fetch_array($fetcher2);
$deal3 = mysqli_fetch_array($fetcher3);
//Insert Data
//mysqli_query($connect, "INSERT INTO deal_1 (title) VALUES ('Galapagos')");
//Deal 1
$deal1title=$deal1["title"];
$deal1desc=$deal1["description"];
$deal1price=$deal1["price"];
$deal1inc=$deal1["include"];
$deal1noinc=$deal1["notinclude"];
这是我的数据库更新表单
<h2>Deal 1</h2>
<form method="POST" action="#">
<table>
<tr>
<td><label>Title: </label><br><input type="text" name="title1" placeholder="<?php echo htmlspecialchars($deal1title); ?>"></td>
</tr>
<tr>
<td>
<label>Description: </label><br><textarea style="height: 200px;" name="desc1" placeholder="<?php echo htmlspecialchars($deal1desc); ?>"></textarea>
</td>
</tr>
<tr>
<td><label>Price: </label><br><input type="text" name="price1" placeholder="<?php echo htmlspecialchars($deal1price); ?>"></td>
</tr>
<tr>
<td><label>Include: </label><br><input type="text" name="include1" placeholder="<?php echo htmlspecialchars($deal1inc); ?>"></td>
</tr>
<tr>
<td><label>Not Include: </label><br><input type="text" name="notinc1" placeholder="<?php echo htmlspecialchars($deal1noinc); ?>"></td>
</tr>
<tr>
<td><input type="submit" name="submit1" value="Save"></td>
</tr>
</table>
</form>
这是我的db update php代码
<?php
//Get Data Deal 1
if(isset($_POST['submit1'])){
$getTitle1=$_POST["title1"];
$getDesc1=$_POST["desc1"];
$getPrice1=$_POST["price1"];
$getInc1=$_POST["include1"];
$getNoInc=$_POST["noinc1"];
//Set Title
if(!empty($getTitle1)){
mysqli_query($connect, "UPDATE specials SET title='$getTitle1' WHERE id='1'");
}
//Set Description
if(!empty($getDesc1)){
mysqli_query($connect, "UPDATE specials SET description='$getDesc1' WHERE id='1'");
}
//Set Price
if(!empty($getPrice1)){
mysqli_query($connect, "UPDATE specials SET price='$getPrice1' WHERE id='1'");
}
//Set Include
if(!empty($getInc1)){
mysqli_query($connect, "UPDATE specials SET include='$getInc1' WHERE id='1'");
}
//Set noinclude
if(!empty($getNoInc)){
mysqli_query($connect, "UPDATE specials set notinclude='$getNoInc' WHERE id='1'");
}
}