【发布时间】:2012-12-02 00:36:34
【问题描述】:
尝试向我的数据库发布一个简单的表单,但无法正常工作。我通过 XAMPP 激活了 PHP 和 MySQL。数据库“E-mail list”是用“Players”表建立的。
PHP 代码:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$query = 'INSERT INTO Players (
name,
email,
phone,
other
)
VALUES ('.$name.', "'.$email.'", "'.$phone.'","'.$other.'")';
if ($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}}
?>
还有形式:
<form id="myForm" method="post">>
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
</form>
<p><strong id="error"></strong></p>
<br><br>
<input type="button" id="save" name="save" value="Submit Form" />
<p id="response"></p>
【问题讨论】:
-
我建议在
$query上使用print_r,以便您可以验证要发送到数据库的内容。我也强烈推荐使用参数化查询。 -
查询中的所有字符串值都应该使用引号
-
在
<form>中缺少action,而在<form>之后还有一个> -
谢谢大家!我认为 INSERT 前面的 ' 可能是反派。