Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP代码插入数据库时输入字段中没有值_Php - Fatal编程技术网

PHP代码插入数据库时输入字段中没有值

PHP代码插入数据库时输入字段中没有值,php,Php,有人能给我解释一下为什么我在这里没有注释的代码仍然插入到我的数据库中,即使我将注册表单的输入值留空 谢谢 PHP <?php require_once("connection.php"); if ($_POST['submit'] == "Sign Up") { if (!$_POST['email']) { $error.="<br />Please enter your email"; } else if (

有人能给我解释一下为什么我在这里没有注释的代码仍然插入到我的数据库中,即使我将注册表单的输入值留空

谢谢

PHP

<?php

        require_once("connection.php");

    if ($_POST['submit'] == "Sign Up") {

        if (!$_POST['email']) { $error.="<br />Please enter your email"; 

        } else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $error.="<br />Please enter a valid email address";

        } if (!$_POST['password']) { $error.="<br />Please enter your password";

        } else if (strlen($_POST['password']) <8) { $error.="<br />Please enter a password of at least 8 characters in length";

        } if (!preg_match('`[A-Z]`', $_POST['password'])) { $error.="<br />Please enter at least one Uppder Case charater";

        }

        if ($error) { echo "There were error(s) in your signup details:".$error;

        } else {

                $query = "SELECT * FROM `users` WHERE `email`='".mysqli_real_escape_string($link, $_POST['email'])."'";

                $result = mysqli_query($link, $query);

                $results = mysqli_num_rows($result);

        } if ($results) { echo "That email address is already in registered. Do you want to log in?"; 

        } else {

                $query = "INSERT INTO `users` (`email`, `password`) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."', '".md5(md5($_POST['email']).$_POST['password'])."')";

                mysqli_query($link, $query);

                echo "You&apos;ve been signed up!";

        }

    }

        //if ($_POST['submit'] == "Log In") {

                //$query = "SELECT * FROM `users` WHERE `email` = '".mysqli_real_escape_string($link, $_POST['loginemail'])."' AND `password` = '".md5(md5($_POST['loginemail']).$_POST['loginpassword'])."' LIMIT 1";

                //$result = mysqli_query($link, $query);

                //$row = mysqli_fetch_array($result);

        //} if ($row) {

                //$_SESSION['id']=$row['id'];

                //print_r($_SESSION);

        //} else {

                //echo "We could not find a user with that email and password. Please try again.";

    //}
?>

可能您的DB表有一个自动递增字段,因此即使您插入空值,它也会添加一行。

可能您的DB表有一个自动递增字段,因此即使您插入空值,它也会添加一行。

可能您的DB表有一个自动递增字段,因此,即使您插入空值,它也会添加一行。

您的DB表可能有一个自动递增字段,因此即使您插入空值,它也会添加一行。

始终使用
if(!isset($\u POST['name'))
进行验证,否则“empty”也是POST的值。尝试一下,希望这会对您有所帮助。

请始终使用
if(!isset($\u POST['name']))
进行验证,否则“空”也是POST的值。尝试一下,希望这会对您有所帮助。

请始终使用
if(!isset($\u POST['name']))
进行验证,否则“空”也是POST的值。尝试一下,希望这会对您有所帮助。

请始终使用
if(!isset($\u POST['name']))
进行验证,否则“空”也是POST的值。尝试一下,希望这对您有所帮助。

您的逻辑错误:

if ($error) {
   echo "There were error(s) in your signup details:".$error;
} else {
   ...
   // here `$results` is set
}

// What happens if `$results` is not set?
if ($results) {
   echo "That email address is already in registered. Do you want to log in?"; 
} else {
   // Yep, we go here...
   // insert!
请注意,如果出现错误,
$results
将不会被设置,因此您将在第二条
if
语句的
else
部分中输入错误行

您应该将所有内容放入无错误块中,或者在要插入以下内容时再次检查:

if ($results && !$error) {
或者在错误块中将
$results
设置为false,等等。。几种可能的解决方案。

您的逻辑错误:

if ($error) {
   echo "There were error(s) in your signup details:".$error;
} else {
   ...
   // here `$results` is set
}

// What happens if `$results` is not set?
if ($results) {
   echo "That email address is already in registered. Do you want to log in?"; 
} else {
   // Yep, we go here...
   // insert!
请注意,如果出现错误,
$results
将不会被设置,因此您将在第二条
if
语句的
else
部分中输入错误行

您应该将所有内容放入无错误块中,或者在要插入以下内容时再次检查:

if ($results && !$error) {
或者在错误块中将
$results
设置为false,等等。。几种可能的解决方案。

您的逻辑错误:

if ($error) {
   echo "There were error(s) in your signup details:".$error;
} else {
   ...
   // here `$results` is set
}

// What happens if `$results` is not set?
if ($results) {
   echo "That email address is already in registered. Do you want to log in?"; 
} else {
   // Yep, we go here...
   // insert!
请注意,如果出现错误,
$results
将不会被设置,因此您将在第二条
if
语句的
else
部分中输入错误行

您应该将所有内容放入无错误块中,或者在要插入以下内容时再次检查:

if ($results && !$error) {
或者在错误块中将
$results
设置为false,等等。。几种可能的解决方案。

您的逻辑错误:

if ($error) {
   echo "There were error(s) in your signup details:".$error;
} else {
   ...
   // here `$results` is set
}

// What happens if `$results` is not set?
if ($results) {
   echo "That email address is already in registered. Do you want to log in?"; 
} else {
   // Yep, we go here...
   // insert!
请注意,如果出现错误,
$results
将不会被设置,因此您将在第二条
if
语句的
else
部分中输入错误行

您应该将所有内容放入无错误块中,或者在要插入以下内容时再次检查:

if ($results && !$error) {


或者在错误块中将
$results
设置为false,等等。。有几种可能的解决方案。

感谢我的帖子在那里编辑Nishant上的帮助。感谢我的帖子在那里编辑Nishant上的帮助。感谢我的帖子在那里编辑Nishant上的帮助。你好,Marco,谢谢你的回答。我有一个四列DB表。名称、电子邮件和密码设置为文本。然后id作为一个INT,PRIMARY和auto increment字段。我如何才能使用id注册用户,并解决这个自动递增问题?他们必须添加唯一的用户名,否则电子邮件就会出现id不用于登录或注册。@SeanRavenhill根据Misal的建议,您必须在查询之前检查空值。自切片面包以来,自动增量是最棒的东西,因为它为您提供了一个独一无二的id,不会带来麻烦。嗨,马可,谢谢你的回答。我有一个四列DB表。名称、电子邮件和密码设置为文本。然后id作为一个INT,PRIMARY和auto increment字段。我如何才能使用id注册用户,并解决这个自动递增问题?他们必须添加唯一的用户名,否则电子邮件就会出现id不用于登录或注册。@SeanRavenhill根据Misal的建议,您必须在查询之前检查空值。自切片面包以来,自动增量是最棒的东西,因为它为您提供了一个独一无二的id,不会带来麻烦。嗨,马可,谢谢你的回答。我有一个四列DB表。名称、电子邮件和密码设置为文本。然后id作为一个INT,PRIMARY和auto increment字段。我如何才能使用id注册用户,并解决这个自动递增问题?他们必须添加唯一的用户名,否则电子邮件就会出现id不用于登录或注册。@SeanRavenhill根据Misal的建议,您必须在查询之前检查空值。自切片面包以来,自动增量是最棒的东西,因为它为您提供了一个独一无二的id,不会带来麻烦。嗨,马可,谢谢你的回答。我有一个四列DB表。名称、电子邮件和密码设置为文本。然后id作为一个INT,PRIMARY和auto increment字段。我如何才能使用id注册用户,并解决这个自动递增问题?他们必须添加唯一的用户名,否则电子邮件就会出现id不用于登录或注册。@SeanRavenhill根据Misal的建议,您必须在查询之前检查空值。自切片面包以来,自动增量是最棒的东西,因为它给你一个独一无二的id,没有任何麻烦。嗨,杰罗姆。谢谢你的回答。我理解你的意思,但由于我对开发和PHP非常陌生,我正在努力将其付诸实践。如果可能的话,你能给我看看你的解决方案吗?非常感谢。@SeanRavenhill我已经做了,只需将你的
if($results){
替换为
if($results&!$error){
@jeroen谢谢你的快速回复。我确实按照建议进行了替换。刷新了页面,甚至重新启动了MAMP服务器。仍然可以获得