我已经制作了一个注册脚本。表格有7个字段。 HTML代码如下,
<input type="text" id="user_name" />
<input type="password" id="user_password" />
<input type="text" id="user_surname" />
<input type="email" id="user_email" />
<input type="text" id="user_phone" />
<input type="text" id="user_address" />
<input type="text" id="user_pin" />
<input type="submit" value="Submit" />
相应的PHP代码插入语句是
$query="INSERT INTO register
VALUES (
user_name = '$user_name',
user_password = '$pass',
user_surname = '$surname',
user_email = '$email',
user_phone = '$phone',
user_address = '$address',
user_pin = '$pin') ";
$result = mysql_query($query,$con);
if($result)
{
echo "Details updated successfully";
}
查询工作正常。运行查询后,我收到详细信息更新成功消息。但是在我的TABLE中没有正确更新。在我的表中的所有列中仅更新1(数字1)。请帮我解决这个问题。提前谢谢。
答案 0 :(得分:5)
你需要给你的html元素赋予name属性。没有name属性,在表单提交后不会发布任何值。请尝试以下内容。记住你给出的任何名称属性同样是$ _POST数组的键。在表单提交后,在你的php中尝试以下内容,看看通过表单提交的值是什么。然后在必要的编码后插入查询。
echo '<pre>';
print_r($_POST);
die();
<form method="post" action="">
<div class="line"><label for="user_name">Username *: </label><input type="text" id="user_name" name="user_name" /></div>
<div class="line"><label for="user_password">Password *: </label><input type="password" id="user_password" name="user_password" /></div>
<div class="line"><label for="user_surname">Surname *: </label><input type="text" id="user_surname" /></div>
<div class="line"><label for="user_email">Email *: </label><input type="email" id="user_email" name="user_email" /></div>
<div class="line"><label for="user_phone">Telephone: </label><input type="text" id="user_phone" name="user_phone" /></div>
<div class="line"><label for="user_address">Address *: </label><input type="text" id="user_address" name="user_address" /></div>
<div class="line"><label for="user_pin">Post Code *: </label><input type="text" id="user_pin" name="user_pin"/></div> <br>
<div style="margin-left:50px;"><input type="submit" value="Submit" /></div>
</form>
答案 1 :(得分:2)
使用以下
<?php
$user_name = $_POST['user_name'];
$pass = $_POST['user_password'];
$surname = $_POST['user_surname'];
$email = $_POST['user_email'];
$phone = $_POST['user_phone'];
$address = $_POST['user_address'];
$pin = $_POST['user_pin'];
$query="INSERT INTO register
(`user_name`,`user_password`,`user_surname`,`user_email`,`user_phone`,`user_address`,`user_pin`) values ('".$user_name."','".$pass."','".$surname."','".$email."','".$phone."','".$address."','".$pin."')";
$result = mysql_query($query,$con);
if($result)
{
echo "Details updated successfully";
}
答案 2 :(得分:1)
对于您的变量:您必须为输入元素指定一个名称attribut,否则它将无法在PHP中使用。在PHP方面,它们不会神奇地显示为变量(希望,如果它们启用register_globals
并且您有巨大的安全风险),它们将显示在$_GET
或{{1根据你的形式。
对于你的SQL:你混合了两种语法,使用它:
$_POST
或
INSERT INTO register (user_name,user_password,user_surname,user_email,user_phone,user_address,user_pin) VALUES ('$user_name','$pass','$surname','$email','$phone','$address','$pin')
注意:我不知道DEFAULT应该做什么。 INSERT INTO register SET user_name='$user_name',user_password='$pass',user_surname='$surname',user_email='$email',user_phone='$phone',user_address='$address',user_pin='$pin'
函数已被弃用,请改用mysql_*
或PDO。查看准备好的语句以防止MYSQL注入。
答案 3 :(得分:0)
您已经指出,您的问题是您没有为您的输入设置name
属性,这意味着您的表单中没有任何内容发布到服务器。修复后,还应修改后端mysql代码以使用mysqli和prepared statements来提高安全性,因为不推荐使用mysql_函数。以下是您当前查询的示例:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_database');
$stmt = $mysqli->prepare("INSERT INTO register
(user_name,user_password,user_surname,user_email,user_phone,user_address,user_pin)
VALUES (?,?,?,?,?,?,?)");
$stmt->bind_param('sssssss',
$post_user_name,$post_user_password,$post_user_surname,$post_user_email,
$post_user_phone,$post_user_address,$post_user_pin);
// Quick and easy way to get all of your POST array values
// set to prefixed variables:
extract($_POST, EXTR_PREFIX_ALL, "post");
// you can overwrite any of the variables before running the query, like:
$post_user_password = password_hash($post_user_password, PASSWORD_DEFAULT);
$stmt->execute();
if( $stmt->affected_rows ) {
echo "Details updated successfully";
}
答案 4 :(得分:0)
试试这个。
<?php
$con=mysql_connect("db_server","$db_user","$db_pass");//Establishing Database connection
mysql_select_db("$db_name",$con);//Selecting required database
$user_name = $_POST['user_name'];
$pass = $_POST['user_password'];
$surname = $_POST['user_surname'];
$email = $_POST['user_email'];
$phone = $_POST['user_phone'];
$address = $_POST['user_address'];
$pin = $_POST['user_pin'];
$query="INSERT INTO register
(`user_name`,`user_password`,`user_surname`,`user_email`,`user_phone`,`user_address`,`user_pin`) values ('".$user_name."','".$pass."','".$surname."','".$email."','".$phone."','".$address."','".$pin."')";
$result = mysql_query($query);
if($result)
{
echo "Record is inserted successfully!!";
}
else
{
echo "Record insertion failed";
}
?>