我在将表单数据插入数据库时遇到问题。它成功连接到数据库,但数据并没有插入我的MySQL数据库。希望你能帮忙!提前谢谢!
这是表格代码:
<form id="score" method="post" action="process.php">
<fieldset>
<div class="box1">
<legend>Your details</legend>
<ol>
<li>
<label for=name>Name</label>
<input id=name name=name type=text placeholder="First and last name" required autofocus>
</li>
<li>
<label for=email>Email</label>
<input id=email name=email type=email placeholder="example@domain.com" required>
</li>
<li>
<label for=phone>Phone (country code if overseas)</label>
<input id=phone name=phone type=tel placeholder="Eg. +447500000000" required>
</li>
</ol>
</div>
</fieldset>
<fieldset style="border: none;">
<button type=submit>Continue</button>
</fieldset>
</form>
这是php代码(filename process.php):
<?php
if( $_POST )
{
$con = mysql_connect("localhost","database_user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con);
$insert_query = "insert into feedback(
name,
phone,
email
) values (
".$_POST['name'].",
".$_POST['phone'].",
".$_POST['email'].")";
($insert_query);
echo "<h2>Thank you for your feedback!</h2>";
mysql_close($con);
}
?>
答案 0 :(得分:2)
您错过了mysql_query
功能:
使用这种方式:
$retval = mysql_query( $insert_query, $con );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
参考link
答案 1 :(得分:1)
尝试这种方式:
<?php
if( $_POST )
{
$con = mysql_connect("localhost","database_user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con);
$insert_query = "insert into feedback(
name,
phone,
email
) values (
'".$_POST['name']."',
'".$_POST['phone']."',
'".$_POST['email']."')";
mysql_query($insert_query);
echo "<h2>Thank you for your feedback!</h2>";
mysql_close($con);
}
?>
mysql_query()缺少。
同时使用&#39;&#39;关闭查询变量。由于他们是字符串,你需要使用它们,否则就会失败。
干杯
答案 2 :(得分:0)
mysql_query()
。替换
($insert_query);
与
mysql_query($insert_query);