我创建了一个数据库,它包含:
desc users;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| UserID | int(25) | NO | PRI | NULL | auto_increment |
| Username | varchar(65) | YES | | NULL | |
| Fname | varchar(65) | YES | | NULL | |
| Lname | varchar(65) | YES | | NULL | |
| Password | varchar(32) | YES | | NULL | |
| Password1 | varchar(32) | YES | | NULL | |
| EmailAddress | varchar(255) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
但是,当我运行此代码时
if($email_exists == false && $Password == $Password1)
{
$sql="INSERT INTO users(u_Username, u_Fname, u_Lname, u_EmailAddress, u_Password)
VALUES('$_POST[$Username]', '$_POST[$Fname]', '$_POST[$Lname]', '$_POST[$EmailAddress]', '$_POST[$Password]')";
echo "<pre>"; print_r($sql); exit;
if(!mysqli_query($link, $sql))
{
#$query = mysqli_query($myConnection, $sqlCommand);
#die (mysqli_error($myConnection));
die('Error: ' . mysqli_error($link));
}
header("location: login.php");
}
我收到此错误:
INSERT INTO users(u_Username, u_Fname, u_Lname, u_EmailAddress, u_Password)
VALUES('', '', '', '', '')
这里有什么错误?我正在使用php和mysqli。我是新手,但我试图让我的register.php文件接受并添加到我的数据库。
编辑这里是完整的代码
<?php
session_start();
require_once('connect.php');
require_once "utils.php";
if(isset($_POST['submit']))
{
//CHECK EMPTY FORM DATA and SANITIZE
if(!empty($_POST['Username'])&&
!empty($_POST['Fname'])&&
!empty($_POST['Lname'])&&
!empty($_POST['EmailAddress'])&&
!empty($_POST['Password'])&&
!empty($_POST['Password1']))
{
$Username = mysqli_real_escape_string($link,htmlentities($_POST['Username']));
$Fname = mysqli_real_escape_string($link,htmlentities($_POST['Fname']));
$Lname = mysqli_real_escape_string($link,htmlentities($_POST['Lname']));
$Password = mysqli_real_escape_string($link,htmlentities($_POST['Password']));
$Password1 = mysqli_real_escape_string($link,htmlentities($_POST['Password1']));
$EmailAddress = mysqli_real_escape_string($link,htmlentities($_POST['EmailAddress']));
}
else { header("Location: register.php");exit(); }
if(isset($EmailAddress) && !empty($EmailAddress) &&
isset($Password) && !empty($Password))
{
$email_exists = false;
$sql="SELECT * FROM users WHERE EmailAddress='$EmailAddress'";
if($result = mysqli_query($link, $sql))
{
while($record = mysqli_fetch_row($result))
{
if($record = $EmailAddress)
{
$email_exists = true;
}
if($email_exists = true)
{
echo "<div id='reg_error1'>* The Email Used Already Exists! <br></div>";
}
}
}
if($Password != $Password1)
{
echo "<div id='reg_error2'>* Your Password Does Not Match!<br></div>";
}
if($email_exists == false && $Password == $Password1)
{
$sql="INSERT INTO users(Username, Fname, Lname, EmailAddress, Password)
VALUES('{$_POST['$Username']}', '{$_POST['$Fname']}', '{$_POST['$Lname']}', '{$_POST['$EmailAddress']}', '{$_POST['$Password']}')";
echo "<pre>"; print_r($sql); exit;
if(!mysqli_query($link, $sql))
{
#$query = mysqli_query($myConnection, $sqlCommand);
#die (mysqli_error($myConnection));
die('Error: ' . mysqli_error($link));
}
header("location: login.php");
}
echo "<hr>";
}
}
答案 0 :(得分:1)
$sql="INSERT INTO users(Username, Fname, Lname, EmailAddress, Password)
VALUES('{$_POST['$Username']}', '{$_POST['$Fname']}', '{$_POST['$Lname']}', '{$_POST['$EmailAddress']}', '{$_POST['$Password']}')";
???
您已经将POST变量提取到那些本地变量中,那么为什么要尝试将它们用作post数组的索引参数?
$sql="INSERT INTO users(Username, Fname, Lname, EmailAddress, Password)
VALUES('{$Username'}', '{$Fname}', '{'$Lname}', '{'$EmailAddress'}', '{$Password}')";
如果我错误地编辑了你的代码,就像在错误的位置引用的那样,那就是因为我不会像以前那样编写查询。 PDO和准备好的语句或存储过程就是我一直在使用sonce 2003而且我相信那些不这样做的人只是在惹麻烦。
答案 1 :(得分:0)
您已将$ _POST值存储在变量中,如
$Username = mysqli_real_escape_string($link,htmlentities($_POST['Username']));
所以在查询中使用这些变量。
INSERT INTO users(Username, Fname, Lname, EmailAddress, Password)
VALUES('$Username'', '$Fname', '$Lname', '$EmailAddress', '$Password')
答案 2 :(得分:0)
你可以在处理像这样的小表时插入它。这是一个简短的手,但运作良好。
$sql="INSERT INTO users VALUES('', '$_POST[Username]', '$_POST[Fname]', '$_POST[Lname]', '$_POST[EmailAddress]', '$_POST[Password]')";
&#39;&#39; ...未指定,因为它是自动生成的。 在表格中,因为ID是主键,我希望你把它放在自动增量上,我只是把它留空了一个值或&#39;&#39; ...没有指定,因为它是&#39; s自动生成。
此外,我不鼓励您直接从用户插入值而不进行任何服务器端验证。