请考虑以下代码。 我想阅读持有人阵列上的电子邮件, 但是当我尝试访问$ holder ['email']时,它失败了。 我想用数据库中的旧电子邮件检查新输入的电子邮件。 这是代码:
$conn = mysqli_connect("localhost", "root", "" ,"note_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//echo "Connected successfully";
$email = $_REQUEST ['email'];
$pass = $_REQUEST ['pass'];
$result = mysqli_query($conn, "SELECT email FROM users");
$holder = array();
while($row = mysqli_fetch_array($result)){
$record = array();
$record ['email'] = $row['email'];
$holder [] = $record;
}
if($holder['email'] == $email){
echo "email was set before";
return;
}
else{
$sql = mysqli_query($conn,"INSERT INTO users VALUES ('$email', '$pass')") ;
echo "email Inserted to DataBase";
}
mysqli_close($conn);
答案 0 :(得分:2)
试试这个:
$conn = mysqli_connect("localhost", "root", "" ,"note_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//echo "Connected successfully";
$email = $_POST ['email'];
$pass = $_POST ['pass'];
$result = mysqli_query($conn, "SELECT email FROM users");
$holder = array();
while($row = mysqli_fetch_array($result)){
$holder [] = $row;
}
foreach($holder as $val){
if($val['email']==$email){
echo "email was set before";
return FALSE;
}
else{
$sql = mysqli_query($conn,"INSERT INTO users VALUES ('$email', '$pass')") ;
echo "email Inserted to DataBase";
}
}
mysqli_close($conn);
答案 1 :(得分:0)
使用以下代码
$conn = mysqli_connect("localhost", "root", "" ,"note_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//echo "Connected successfully";
$email = $_REQUEST ['email'];
$pass = $_REQUEST ['pass'];
$result = mysqli_query($conn, "SELECT email FROM users");
$holder = array();
while($row = mysqli_fetch_array($result)){
$record = array();
$record ['email'] = $row['email'];
$holder [] = $record;
}
foreach( $holder as $key => $hold){
if($hold['email'] == $email){
echo "email was set before";
return;
}else{
$sql = mysqli_query($conn,"INSERT INTO users VALUES ('$email', '$pass')") ;
echo "email Inserted to DataBase";
}
}
mysqli_close($conn);
答案 2 :(得分:0)
因为$ holder [' email']并不存在。 $ foo [] =" bar" //给你这个数组"数组([0] =>" bar)" 要获取$ foo中的所有电子邮件,您应该循环播放
编辑:就像@Shahid Ahmad的建议一样
foreach($holder as $h) {
echo $h['email'];
}
答案 3 :(得分:0)
如果您只想查看是否已经收到电子邮件,那么您可以使用布尔标志获得相同的结果。另外,请考虑使用PDO或准备好的语句。此外,如果您将从$ _POST或$ _GET中获取值,则显式设置。
$conn = mysqli_connect("localhost", "root", "" ,"note_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//echo "Connected successfully";
$email = $_POST['email']; //Or $_GET
$pass = $_POST['pass']; //Or $_GET
$result = mysqli_query($conn, "SELECT email FROM users");
$found = false; //Boolean to check if email is already taken
while($row = mysqli_fetch_array($result)) {
if ($row['email'] == $email) { //If email exists in database
$found = true;
break;
}
}
if ($found) { //If email exists in database
echo "email was set before";
return;
} else { //Email doesn't exist so inset, Consider using prepared statements
$sql = mysqli_query($conn, "INSERT INTO users VALUES ('$email', '$pass')") ;
echo "email Inserted to DataBase";
}
mysqli_close($conn);