这实际上是我的数据库的问题,而不是我的表单或我的问题。 如果有人想知道我最终是如何解决这个问题的,我会在这里概述一下。
我按照 in.k 的建议进行操作,并使用了与表单中相同的查询,然后添加了几行以生成一些错误代码,如下所示:
// query execution
$wr_pro = "INSERT INTO products (name, description, price, cat_id) VALUES ('$name', '$description', '$price', '$categories')";
$result = $db->query($wr_pro);
print "$db->error()";
if ($db->connect_error) {
die("Connection failed: ");
}
if ($result === TRUE) {
echo "Product added successfully!";
}
else {
echo "Problem loading to database...";
}
当我加载页面时,我收到一个数据库错误,告诉我需要为所有空字段设置默认值(这是一个空标签字段导致了问题)。
所以,是的,我基本上没有正确设置数据库。 但我现在知道,将来错误代码会很棒。
-------------- 原始问题 ----------------- 我对此很陌生,所以如果我错过了一些非常明显的东西,或者我的代码有点困惑,我提前表示抱歉。
我正在尝试创建一个页面,将新产品条目写入现有的 mysql 数据库表中。
根据其他问题,我尝试过:
- 在我的 isset 中添加 !empty
- 确保 form method = "post"
- 确保内容确实被保存以发布(在发布之前)
- 尝试在发布后打印帖子,但没有显示
- 确保提交按钮名称与 isset 函数中的名称相同
- 确保按钮名称未提交
- 将 isset 移至表单之前(我实际上不确定为什么要这样做,所以如果有人可以解释,我将非常感激)
没有显示无法写入数据库时的错误,并且也没有写入数据库,所以我认为它的 isset 不起作用?但我很容易错。
我非常确定与数据库的连接正常,因为表单有一个从数据库中选择的类别部分,并且在页面上显示得很好。
我不知道它是否相关,但我在本地 WAMP 上运行它。
任何人可以为我指出正确的方向,我将非常感激。
----我的代码
<?php
include("includes/connection.php");
include("includes/head.html");
include("includes/header.php");
?>
<div class="pagecontent">
<h1> Create New Product:</h1>
<!-- form submission action-->
<?php
if (isset($_POST['submitwrpro'])) {
// escape user inputs
$name = mysqli_real_escape_string($db, $_POST['name']);
$description = mysqli_real_escape_string($db, $_POST['description']);
$price = mysqli_real_escape_string($db, $_POST['price']);
$categories = mysqli_real_escape_string($db, $_POST['category_id']);
// query execution
$wr_pro = "INSERT INTO products (name, description, price, cat_id) VALUES ('$name', '$description', '$price', '$categories')";
if(mysqli_query($db, $wr_pro)){
echo "Product added successfully!";
}
else{
echo "Error writing to database $wr_pro. " . mysqli_error($db);
}
}
?>
<!-- form entry -->
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<table>
<tr>
<td>Name</td>
<td><input type='text' name='name'></td>
</tr>
<tr>
<td>Price</td>
<td><input type='text' name='price'></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name='description'></textarea></td>
</tr>
<tr>
<td>Category</td>
<td>
<!-- read product categories from the db -->
<?php
$req_cat = "SELECT cat_id, name FROM categories";
$result = $db->query($req_cat);
echo "<select class='form-control' name='category_id'>";
echo "<option>Select category...</option>";
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()){
extract($row);
echo "<option value=\"" . $row['cat_id'] . "\">" . $row["name"] . "</option>";
}
}
else {
echo "no results";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td></td>
<td>
<button name="submitwrpro" type="submit">Create</button>
</td>
</tr>
</table>
</form>
</div>
<?php
include("includes/footer.php");
?>
最佳答案
我认为您没有正确连接到数据库。我没有看到连接文件,但您在 select 中以表单形式获得的结果具有不同的连接到数据库的方式。我仔细检查了表格,没有发现任何错误。尝试这样的事情
$connection = "INSERT INTO products (name, description, price, cat_id) VALUES ('$name', '$description', '$price', '$categories')";
$result = $db->query($connection);
echo "Product added successfully!";
关于php - isset 不适用于表单提交(实际上是数据库问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47855265/