使用 PHP while 循环将数据插入 Mysql
Inserting data into Mysql with PHP while loop
我正在尝试根据我的数据库检查一封电子邮件,如果它不存在,请将其添加到数据库中。
$query = "SELECT * FROM users";
$inputQuery = "INSERT INTO users (`email`,
`password`) VALUES ('$emailInput',
'$passInput')";
$emailInput = ($_POST['email']);
$passInput = ($_POST['password']);
if ($result = mysqli_query($link, $query)) {
while ($row = mysqli_fetch_array($result)) {
if ($row['email'] == $emailInput) {
echo "We already have that email!";
} else {
mysqli_query($link, $inputQuery);
echo "Hopefully that's been added to the database!";
}
}
};
它可以检测现有的电子邮件,只是增加了一点...
目前这似乎为每个现有行添加一个新的空行(大小加倍)。
我试图理解为什么它不添加信息,以及如何以某种方式逃离循环。
另外,似乎每个人都重用 $query,但这对我来说似乎很奇怪。像我这里那样单独命名查询是好习惯吗?
如果还有什么需要补充的,请告诉我。
我不会谈论标准,而是直接、简单地回答你的问题。
方法 - 1:
INSERT INTO users (`email`,`password`) SELECT '$emailInput', '$passInput' from DUAL WHERE NOT EXISTS (select * from users where `email` = '$emailInput');
方法 - 2:
- 在 email
列上创建唯一键
- 使用 INSERT IGNORE 选项。
user3783243 comments are worth noting
您的代码似乎有点过度设计,因为为什么不将 $_POST['email'] 传递给 select 查询 where 子句
"SELECT * FROM users where email = $emailInput" 然后检查它是否已经存在。
另外请记住,这只是一个示例,您应该始终检查和清理用户输入。
另一方面,您可以使用 MySQL 仅使用 INSERT ... ON DUPLICATE KEY UPDATE 语法来做到这一点。 https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
这需要为电子邮件列添加唯一键。
试试这个:
$emailInput = mysqli_real_escape_string($link, $_POST['email']);
$passInput = mysqli_real_escape_string($link, $_POST['password']);
$qry3=mysqli_query($link,"select * from users where `email`='".$emailInput."'");
$num=mysqli_num_rows($qry3);
if($num==1) {
echo "Email-Id already exists";
} else {
$inputQuery = mysqli_query($link,"INSERT INTO users (`email`, `password`) VALUES ('".$emailInput."', '".$passInput."')");
if ($inputQuery) {
echo "Hopefully that's been added to the database!";
}
}
我正在尝试根据我的数据库检查一封电子邮件,如果它不存在,请将其添加到数据库中。
$query = "SELECT * FROM users";
$inputQuery = "INSERT INTO users (`email`,
`password`) VALUES ('$emailInput',
'$passInput')";
$emailInput = ($_POST['email']);
$passInput = ($_POST['password']);
if ($result = mysqli_query($link, $query)) {
while ($row = mysqli_fetch_array($result)) {
if ($row['email'] == $emailInput) {
echo "We already have that email!";
} else {
mysqli_query($link, $inputQuery);
echo "Hopefully that's been added to the database!";
}
}
};
它可以检测现有的电子邮件,只是增加了一点...
目前这似乎为每个现有行添加一个新的空行(大小加倍)。 我试图理解为什么它不添加信息,以及如何以某种方式逃离循环。
另外,似乎每个人都重用 $query,但这对我来说似乎很奇怪。像我这里那样单独命名查询是好习惯吗?
如果还有什么需要补充的,请告诉我。
我不会谈论标准,而是直接、简单地回答你的问题。
方法 - 1:
INSERT INTO users (`email`,`password`) SELECT '$emailInput', '$passInput' from DUAL WHERE NOT EXISTS (select * from users where `email` = '$emailInput');
方法 - 2:
- 在 email
列上创建唯一键
- 使用 INSERT IGNORE 选项。
user3783243 comments are worth noting
您的代码似乎有点过度设计,因为为什么不将 $_POST['email'] 传递给 select 查询 where 子句 "SELECT * FROM users where email = $emailInput" 然后检查它是否已经存在。 另外请记住,这只是一个示例,您应该始终检查和清理用户输入。 另一方面,您可以使用 MySQL 仅使用 INSERT ... ON DUPLICATE KEY UPDATE 语法来做到这一点。 https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html 这需要为电子邮件列添加唯一键。
试试这个:
$emailInput = mysqli_real_escape_string($link, $_POST['email']);
$passInput = mysqli_real_escape_string($link, $_POST['password']);
$qry3=mysqli_query($link,"select * from users where `email`='".$emailInput."'");
$num=mysqli_num_rows($qry3);
if($num==1) {
echo "Email-Id already exists";
} else {
$inputQuery = mysqli_query($link,"INSERT INTO users (`email`, `password`) VALUES ('".$emailInput."', '".$passInput."')");
if ($inputQuery) {
echo "Hopefully that's been added to the database!";
}
}