gpt4 book ai didi

php - 将数据插入 SQL 数据库不起作用,但说它已经起作用

转载 作者:行者123 更新时间:2023-11-29 21:37:19 24 4
gpt4 key购买 nike

所以我现在的网站没有正确插入数据。我确实有 id 和 sub_date (提交的日期),但我认为这些是自动的,所以不需要为它创建变量和其他东西?如我错了请纠正我?

PHP 代码:

<?php
if (isset($_POST['addBtn']))
{
$m_titleAdd = $_POST['m_titleAdd'];
$authorAdd = $_POST['authorAdd'];
$statusAdd = $_POST['statusAdd'];
$tutorialAdd = $_POST['tutorialAdd'];
if (empty($errors))
{
$SQLinsert = $odb -> prepare("INSERT INTO `methods` VALUES(NULL, :id, :m_title, :sub_date, :status, :author, :tutorial)");
$SQLinsert -> execute(array(':id' => null, ':m_title' => $m_titleAdd, ':sub_date' => null, ':status' => $statusAdd, ':author' => $authorAdd, ':tutorial' => $tutorialAdd));
echo '<div class="nNote nSuccess hideit"><p><strong>SUCCESS: </strong>Method has been added</p></div>';
}
else
{
echo '<div class="nNote nFailure hideit"><p><strong>ERROR:</strong><br />';
foreach($errors as $error)
{
echo '-'.$error.'<br />';
}
echo '</div>';
}
}
?>

HTML 代码:

<form action="" method="POST">
<fieldset>
<div class="formRow">
<label>Method Title:</label>
<div class="formRight"><input type="text" name="m_titleAdd" class="form-control round" style="width:300px;" /></div>
<div class="clear"></div>
</div>
<div class="formRow">
<label>Tutorial:</label>
<div class="formRight"><textarea style="resize:none;" rows="3" cols="15" name="tutorialAdd" class="autoGrow form-control round"></textarea></div>
<div class="clear"></div>
</div>
<div class="formRow">
<label>Author:</label>
<div class="formRight"><input type="text" name="authorAdd" class="form-control round" style="width:300px;" /></div>
<div class="clear"></div>
</div>
<div class="control-group">
<label class="control-label">Type:</label>
<div class="controls">
<select class="btn btn-default dropdown-toggle" name="status" tabindex="1">
<option value="Working" />Working
<option value="Plausible" />Plausible
<option value="Not Working" />Not Working
</select>
</div>
</div><br>
<div class="formRow">
<div style="width:100px;"><input type="submit" value="Add" name="addBtn" class="btn btn-primary btn-block btn-round" /></div>
<div class="clear"></div>
</div><br>
</fieldset>
</form>

最佳答案

$_POST['statusAdd'] 未定义,在您的表单中它实际上命名为 status。因此只需更改以下内容即可。

更改此:

$statusAdd = $_POST['statusAdd'];

对此:

$statusAdd = $_POST['status'];

然后,对于插入查询,请确保更改此设置,在这种情况下,不需要为 id 指定 null:

VALUES(NULL, :id, etc

对此:

VALUES(:id, etc

关于php - 将数据插入 SQL 数据库不起作用,但说它已经起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34824699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com