我一直在为我一直在建设的婚礼网站制作一个定制的结婚礼物登记册,有一段时间似乎工作正常,但现在似乎没有用,我不知道为什么......
礼品登记处的工作方式如下:
代码最初将更新动态表并刷新页面,以便用户可以看到他们选择的礼物现在已被“采取”(刷新很重要,否则表内容将不会更新)。现在发生的事情是,当用户填写表单并点击提交时,表单条目似乎没有输入到数据库中。
这个代码是一个完整的黑客,我之前从未使用过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>
答案 0 :(得分:0)
尝试更改
while($nt=mysqli_fetch_array($result)){
echo "<option value=$nt[gift_name]>$nt[gift_name]</option>";
}
到
while($nt=mysqli_fetch_array($result)){
echo "<option value=\"{$nt['gift_name']}\">{$nt['gift_name']}</option>";
}
这有效的原因是:
a)引用数组元素$ nt [giftname]与$ nt ['giftname']不同,第一个查找数组元素,其键值等于constant的值为'giftname' ',后者正在寻找一个带有'giftname'键的数组元素。
b)取决于您使用哪种Doc类型应使用value =“value”而不是value = value