【发布时间】:2016-09-05 19:40:32
【问题描述】:
我正在尝试制作一个简单的申诉表单,将数据发布到 SQL 数据库。但是当我提交时,要么什么都没有发生,要么提交了空白数据。
这是我的表格:
<form class="form-horizontal" role="form" action="insert.php" method="post">
<div class="form-group">
<label for="user" class="col-sm-2 control-label">
Username:
</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="user" id="user" placeholder="DiscordTag#0000" />
</div>
</div>
<div class="form-group">
<label for="date" class="col-sm-2 control-label">
Date of ban:
</label>
<div class="col-sm-10">
<input type="date" class="form-control" name="date" id="date" placeholder="mm/dd/yy" />
</div>
</div>
<div class="form-group">
<label for="admin" class="col-sm-2 control-label">
Who banned you?
</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="admin" id="admin" />
</div>
</div>
<div class="form-group">
<label for="appeal" class="col-sm-2 control-label">
Appeal:
</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="appeal" id="appeal"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">
Submit
</button>
</div>
</div>
</form>
这是我的 insert.php
<html>
<?
error_reporting(E_ALL);
$db_host = 'redacted';
$db_username = 'redacted';
$db_password = 'redacted';
$db_name = 'redacted';
if( $_POST )
{
$conn = mysql_connect( $db_host, $db_username, $db_password);
if (!$conn)
{
die('Could not connect: ' . mysql_error());
} else {
mysql_select_db("redacted");
}
$user = $_POST['user'];
$date = $_POST['date'];
$admin = $_POST['admin'];
$appeal = $_POST['appeal'];
$sql = 'INSERT INTO appeals' . '(user, date, admin, appeal)'
.'VALUES ($user, $date, $admin, $appeal)';
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
echo "<h2>Your appeal has been submitted.</h2>";
mysql_close($conn);
}
?>
</html>
如何让它将所有表单数据直接提交到我的 SQL 表中?
【问题讨论】:
-
每次您在 meow 代码 a Kitten is strangled somewhere in the world 中使用 the
mysql_数据库扩展时,它都会被弃用,并且已经存在多年并且永远消失了在 PHP7 中。如果您只是学习 PHP,请花精力学习PDO或mysqli数据库扩展。 Start here -
mysql_error()退回了什么,或者它一开始就做到了? -
你没有引用你的价值观。如果您没有收到 SQL 错误,我会感到惊讶。您的实际错误处理也为零,并且不考虑安全问题(即本例中的 SQL 注入)。
-
扔掉,阅读PDO手册,重新开始php.net/manual/en/book.pdo.php
-
如果您希望变量成为变量,则不能将变量放在单引号中。
Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.-php.net/manual/en/language.types.string.php