将表单数据插入 MySQL?
Posted
技术标签:
【中文标题】将表单数据插入 MySQL?【英文标题】:Insert Form Data Into MySQL? 【发布时间】:2013-02-20 05:48:20 【问题描述】:我正在使用 jQuery 表单来收集用户数据,我正在尝试将表单值插入到表 ptb_registrations
中。
我收到错误“更新数据库错误”
谁能告诉我哪里出错了,这应该很简单。
html:
<form action="process_registration.php" method="post" id="reg_form">
<!-- #first_step -->
<div id="first_step">
<h1>SIGN UP FOR A FREE <span>ACTION-TIME.COM</span> ACCOUNT</h1>
<div class="form">
<input type="text" name="username" id="username" value="username" />
<label for="username">At least 4 characters. Uppercase letters, lowercase letters and numbers only.</label>
<input type="password" name="password" id="password" value="password" />
<label for="password">At least 4 characters. Use a mix of upper and lowercase for a strong password.</label>
<input type="password" name="cpassword" id="cpassword" value="password" />
<label for="cpassword">If your passwords aren’t equal, you won’t be able to continue with signup.</label>
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<input class="submit" type="submit" name="submit_first" id="submit_first" value="" />
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<!-- #second_step -->
<div id="second_step">
<h1>SIGN UP FOR A FREE <span>WEBEXP18</span> ACCOUNT</h1>
<div class="form">
<input type="text" name="firstname" id="firstname" value="first name" />
<label for="firstname">Your First Name. </label>
<input type="text" name="lastname" id="lastname" value="last name" />
<label for="lastname">Your Last Name. </label>
<input type="text" name="email" id="email" value="email address" />
<label for="email">Your email address. We send important administration notices to this address. </label>
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<input class="submit" type="submit" name="submit_second" id="submit_second" value="" />
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<!-- #third_step -->
<div id="third_step">
<h1>SIGN UP FOR A FREE <span>WEBEXP18</span> ACCOUNT</h1>
<div class="form">
<select id="age" name="age">
<option> 0 - 17</option>
<option>18 - 25</option>
<option>26 - 40</option>
<option>40+</option>
</select>
<label for="age">Your age range. </label> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<select id="gender" name="gender">
<option>Male</option>
<option>Female</option>
</select>
<label for="gender">Your Gender. </label> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<select id="country" name="country">
<option>United States</option>
<option>United Kingdom</option>
<option>Canada</option>
<option>Serbia</option>
<option>Italy</option>
</select>
<label for="country">Your country. </label> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<input class="submit" type="submit" name="submit_third" id="submit_third" value="" />
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<!-- #fourth_step -->
<div id="fourth_step">
<h1>SIGN UP FOR A FREE <span>WEBEXP18</span> ACCOUNT</h1>
<div class="form">
<h2>Summary</h2>
<table>
<tr><td>Username</td><td></td></tr>
<tr><td>Password</td><td></td></tr>
<tr><td>Email</td><td></td></tr>
<tr><td>Name</td><td></td></tr>
<tr><td>Age</td><td></td></tr>
<tr><td>Gender</td><td></td></tr>
<tr><td>Country</td><td></td></tr>
</table>
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<input class="send submit" type="submit" name="submit_fourth" id="submit_fourth" value="" />
</div>
</form>
PHP/mysql:
<?php ob_start();
include '../includes/_config/connection.php';
$query="INSERT INTO ptb_registrations (ID,
username,
password,
firstname,
lastname,
email,
age,
gender,
country
)
VALUES('NULL',
'".$username."',
'".$password."',
'".$firstname."',
'".$lastname."',
'".$email."',
'".$age."',
'".$gender."',
'".$country."'
)";
mysql_query($query) or die ('Error updating database');
?>
<?php include ('includes/send_email/reg_email.php'); ?>
<? ob_flush(); ?>
【问题讨论】:
php.net/manual/en/function.mysql-error.php 可能会让你走上正轨。 为什么不检查错误信息?与或死(mysql_error()); 将mysql_query($query) or die ('Error updating database');
更改为 mysql_query($query) or die (mysql_error());
并发布结果。
您的参数中存储了哪些值?您没有显示初始化它们的位置。 $username = $_POST["username"];
@uɐɥʇɐᴎ 您可以将其传递为NULL
或'NULL'
或"NULL"
。
【参考方案1】:
如果它是自动递增的,你不必指定 id 值
试试这个
$query="INSERT INTO ptb_registrations (
username,
password,
firstname,
lastname,
email,
age,
gender,
country
)
VALUES(
'".mysql_real_escape_string($username)."',
'".mysql_real_escape_string($password)."',
'".mysql_real_escape_string($firstname)."',
'".mysql_real_escape_string($lastname)."',
'".mysql_real_escape_string($email)."',
'".mysql_real_escape_string($age)."',
'".mysql_real_escape_string($gender)."',
'".mysql_real_escape_string($country)."'
)";
你应该通过mysql_real_escape_string()
转义你的价值观
【讨论】:
【参考方案2】:如果 ID 是主键和自动增量,那么您不需要传入查询并使用此查询:
$query = " INSERT INTO ptb_registrations SET
username = '".mysql_real_escape_string($username)."',
password = '".mysql_real_escape_string($password)."',
firstname = '".mysql_real_escape_string($firstname)."',
lastname = '".mysql_real_escape_string($lastname)."',
email = '".mysql_real_escape_string($email)."',
age = '".mysql_real_escape_string($email)."',
gender = '".mysql_real_escape_string($gender)."',
country = '".mysql_real_escape_string($country)."'";
希望对你有帮助。
【讨论】:
【参考方案3】:只需从 null 中删除单引号并使用不带引号的例如空
【讨论】:
这应该没关系。如果你甚至像这样使用"NULL"
它应该可以工作。
@Mahdi:我认为'null'和null是有区别的
你可以简单地检查一下:)【参考方案4】:
删除列名并尝试,它可以工作。试试这样的
INSERT INTO table_name values(NULL, column_name);
【讨论】:
【参考方案5】:试试这个查询:
$query="INSERT INTO ptb_registrations(username,password,firstname,lastname,email,age, gender,country) VALUES('".$username."','".$password."','".$firstname."', '".$lastname."','".$email."','".$age."','".$gender."','".$country."')";
【讨论】:
以上是关于将表单数据插入 MySQL?的主要内容,如果未能解决你的问题,请参考以下文章
使用 NodeJS Express 时将表单数据插入 MySQL 数据库
使用 PDO 和 PHP 将表单中的数据插入 MySQL 数据库