PHP - MYSQL不能插入多行

时间:2012-10-19 18:21:30

标签: php html mysql

我在html中注册表格所有设置和工作,它运行并在第一次插入一行到mysql表中,但我不能再插入任何行。

我的php是: -

include "config.php";

$gamerid = $_POST['gamerid_create'];
$email = $_POST['email_create'];
$password = md5($_POST['password_create']);
$country = $_POST ['country_create'];

echo "<pre>";
print_r($_POST);
echo "</pre>";

$insert = "insert into users (gamerid,email,password,country) values('$gamerid','$email','$password','$country')";
mysql_query($insert);

现在,当我在mysql中创建表时,我使用了以下代码: -

CREATE TABLE users
(
id INT primary key NOT NULL AUTO_INCREMENT,
gamerid varchar (255) NOT NULL,
email varchar (255) NOT NULL,
password varchar (255) NOT NULL,
country varchar (255) NOT NULL
)

为什么没有插入更多行?

我的HTML如下:

<form id = "signup_form" method = "POST" action = "http://localhost:8080/quizicle/pages/register.php">
<table id = "signup_form_table" border = "0" class = "signup_table_body">
<tr class = "signup_form_tr_label">
<td class = "signup_form_td"><label for "gamerid" class = "signup_form_label">Create a Gamer ID:</label></td>
<td class = "signup_form_td"><label for "email" class = "signup_form_label">Your Email:</label></td>
<td class = "signup_form_td"><label for "password" class = "signup_form_label">Password:</label></td>
<td class = "signup_form_td"><label for "country" class = "signup_form_label">Country:</label></td>
<td class = "signup_form_td"></td>
</tr>

<tr class = "signup_form_tr">
<td class = "signup_form_td"><input type = "text" name = "gamerid_create" id = "gamerid_create" maxlength="50" class = "signup_form_input"/></td>
<td class = "signup_form_td"><input type = "text" name = "email_create" id = "email_create" maxlength="50" class = "signup_form_input"/></td>
<td class = "signup_form_td"><input type = "password" name = "password_create" id = "password_create" maxlength="50" class = "signup_form_input"/></td>
<td class = "signup_form_td">
<select name = "country_create">                                    <option>Please select a country</option> 
<option>United Kingdom</option> 
</select>
</td>
<td class = "signup_form_td"><input type = "submit" value = "Sign me up!" id = "btn_register" value = "register" class = "signup_form_submit_btn"/></td>
</tr>   
</table>
</form>

2 个答案:

答案 0 :(得分:0)

    <?php

include "config.php";

// INPUT CLEANING FUNCTION
function clean($str)
{
    $cleaned = mysql_real_escape_string(strip_tags($str));
    return $cleaned;
}

$gamerid = clean($_POST['gamerid_create']);
$email = clean($_POST['email_create']);
$password = clean(md5($_POST['password_create']));
$country = clean($_POST ['country_create']);


$insert = "insert into users(gamerid,email,password,country) values('$gamerid' ,'$email',' $password', '$country')";                        
mysql_query($insert);
?>
  1. 检查您的ID字段是否为自动增量
  2. 检查输入标签或textarea标签的属性名称 //可选建议
  3. 使用内置于php
  4. 的filter_var函数来验证电子邮件
  5. 验证密码长度和强度
  6. 还要考虑一些命名约定
  7. 使用框架CODEIGNITER或LARAVEL

答案 1 :(得分:-1)

这可能与您的主键id

有关

让我们更改sql并查看它是否有效。

$insert = "INSERT INTO users (id, gamerid, email, password, country) 
  VALUES (NULL, '{$_POST['gamerid_create']}', '{$_POST['email_create']}', 
  '{$_POST['password_create']}', '{$_POST['country_create']}');";

在使用PHP / MySQL开发技能时应该考虑的一些事项:

  • 验证并清理用户输入。 SQL注入风险。请参阅:mysql_real_escape_string(),以获取基本帮助。

  • 您应该配置表格,确保将字段类型和大小设置为所需的最小值,如果不填写,则VARCHAR(255)将浪费大量空间。

  • 始终使用至少md5算法哈希密码。