我一直在为我一直在建立的婚礼网站制作定制的结婚礼物注册表,有一段时间它似乎工作得很好,但现在它似乎无法正常工作,我不知道为什么......
礼品登记的工作方式如下:
代码最初会更新动态表格并刷新页面,以便用户可以看到他们选择的礼物现在已“被拿走”(刷新很重要,否则表格内容将不会更新)。现在发生的事情是,当用户填写表单并单击提交时,表单条目似乎没有被输入到数据库中。
这个代码是一个完整的黑客,在此之前我从未使用过 php、sql 或 javascript(我在 html 中涉足过一点)所以我自然觉得我有点迷路了。
所以有人知道我去了哪里错误的?
我将不胜感激任何人都可以提供的帮助。
代码如下:
下面构建动态表
<?php
echo"<thead>
<tr>
<th>Gift</th>
<th>Price</th>
<th>Where to buy</th>
<th>Availability</>
</tr>
</thead>";
$dbc = mysqli_connect('localhost','XXXXX','XXXXX','XXXXX_giftregistry') or die('Error connecting to MYSQL server.');
$results = mysqli_query($dbc,"SELECT gift_name, price, where_to_buy, status FROM gift_reg");
while($row = mysqli_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['gift_name']?></td>
<td><?php echo $row['price']?></td>
<td><?php echo $row['where_to_buy']?></td>
<td><?php echo $row['status']?></td>
</tr>
<?php
}
?>
</table>
下一部分是表单提交代码
<?php
$person_gifting = $_POST['name'];
$status = $_POST['status'];
$gift_name = $_POST['gift_name'];
if ($_POST['submit']) {
$dbc = mysqli_connect('localhost','XXXXXX','XXXXXX','XXXXX_giftregistry') or die('Error connecting to MYSQL server.');
mysqli_query($dbc,"UPDATE gift_reg SET person_gifting = '$person_gifting' WHERE gift_name = '$gift_name'") or die ('Error querying database.');
mysqli_query($dbc,"UPDATE gift_reg SET status = '$status' WHERE gift_name = '$gift_name'") or die ('Error querying database.');
mysqli_close($dbc);
echo "<script> formSubmit()</script>";
}
下一部分是表格。
echo "<form method='post' action='index.php'><label>Name</label><input name='name' placeholder='Type Here' required><label>What gift would you like to give?</label>";
$dbc = mysqli_connect('localhost','XXXXX','XXXXX','XXXXX_giftregistry') or die('Error connecting to MYSQL server.');
$query="SELECT gift_name FROM gift_reg WHERE status='Available'";
$result = mysqli_query ($dbc,$query);
echo "<select name='gift_name'>";
while($nt=mysqli_fetch_array($result)){
echo "<option value=$nt[gift_name]>$nt[gift_name]</option>";
}
echo "</select>";
mysqli_close($dbc);
?>
<label>Have you already purchased this gift?</label>
<input name='status' type="radio" value="Taken" id="r1" required>
<label for="r1"><span></span> Already purchased </label>
<input name='status' type="radio" value="Taken" id="r2" required>
<label for="r2"><span></span> Going to purchase </label>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
formSubmit() 指的是:
<script>
function formSubmit() {
window.location.reload();
}
</script>