简体   繁体   English

无法选择表并同时向其中插入内容(Mysql)

[英]Can't Select a table and insert something into it at the same time (Mysql)

I have this registration form, when the user is finished with the form he will finally submit it, but I would like to check if the username and email is already there or not, easy right ? 我有此注册表格,当用户填写完该表格后,他将最终提交该表格,但是我想检查用户名和电子邮件是否已经存在,容易吗? It's been 2 days trying to figure this out but no luck. 试图解决这个问题已经有2天了,但是没有运气。 Excuse me I'm not using the latest version of MySQL since this is the only version I learned at class. 对不起,我没有使用最新版本的MySQL,因为这是我在课堂上中学到的唯一版本。 I will learn the improved one. 我将学习改进的。

I've done a lot of research on Google, I found that this is some king of LOCK, that when we're inserting the table get locked .. 我在Google上做了很多研究,发现这是LOCK的王者,当我们插入表时,它会被锁定..

if(isset($_POST['button'])){


    $query_global = mysql_query("SELECT Username FROM users WHERE Username = '".$_POST['username']."' ") or die(mysql_error());

        $row = mysql_num_rows($query_global);

        if($row == 1){
            $error_username = "The username is already registered, please choose another one <br>";
        }

        $query_email = mysql_query("SELECT Email FROM users WHERE Email = '".$_POST['email']."' ") or die(mysql_error());

        $row_email = mysql_num_rows($query_email);

        if($row_email == 1){
            $error_email = "This email : '".$_POST['email']."' is already registered ";
        }

    if(isset($_POST['Username'])) { $Username = $_POST['Username']; }
    if(isset($_POST['email'])) { $email = $_POST['email']; }



    $nom = $_POST['nom'];
    $sexe = $_POST['sexe'];
    $email = $_POST['email'];
    $tel = $_POST['tel'];
    $adresse = $_POST['adresse'];
    $ville = $_POST['ville'];
    $pseudo = $_POST['pseudo'];
    $mdp = $_POST['mdp'];
    $date = $_POST['date'];
    $profession = $_POST['profession'];


    // location where initial upload will be moved to
    $target = "images/" .$_FILES['uploaded']['name'] ;

    // find thevtype of image
    switch ($_FILES["uploaded"]["type"]) {
    case $_FILES["uploaded"]["type"] == "image/gif":
        move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
        break;
    case $_FILES["uploaded"]["type"] == "image/jpeg":
        move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
        break;
    case $_FILES["uploaded"]["type"] == "image/pjpeg":
        move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
        break;
    case $_FILES["uploaded"]["type"] == "image/png":
        move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
        break;
    case $_FILES["uploaded"]["type"] == "image/x-png":
        move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
        break;

    default:
        $error[] = 'Seulement les JPG, PNG ou GIF sont acceptés!.';
    }

    $error="";

    if (!$error) {

    $query = "INSERT INTO Users Values ('', '".$nom."', '".$sexe."', '".$email."', ".$tel.", '".$adresse."', '".$ville."',  '".$pseudo."', '".$mdp."', curdate(), '$target', '".$date."', '".$profession."')";
    $add_user = mysql_query($query) or die(mysql_error());

    header('Location: Login/index.php');

        }

    }


    //display any errors
    if (!empty($error))
    {
            $i = 0;
            echo "<p><span class='error'>";
            while ($i < count($error)){
            echo $error[$i].'<br />';
            $i ++;}
            echo "</span></p>";
    }

Even if all this works out is there any way to stop the form from submitting when the inputs are wrong, I mean I've tried this in another file and it worked but even if the inputs are wrong the form gets submitted. 即使所有这些工作都能解决,当输入错误时,也有任何方法可以阻止表单提交,我的意思是我已经在另一个文件中尝试过该方法,并且仍然有效,但是即使输入错误,也可以提交表单。

Can't wait to hear your answers. 迫不及待想听听您的答案。

You do not have anything that stops the script from running 您没有阻止脚本运行的任何内容

if($row_email == 1){
        $error_email = "This email : '".$_POST['email']."' is already registered ";
    }
else{

//all the rest of your code here

}

You are setting $error to nothing right before checking it's value not sure what's the purpose: 您正在将$ error设置为空,然后再检查其值,不确定其目的是什么:

$error="";

if (!$error) {

I think you're confusing yourself with all your error variables. 我认为您将自己与所有错误变量混淆了。

Instead of storing your errors in different variables, use a single array to hold all your errors. 不要将错误存储在不同的变量中,而应使用单个数组来保存所有错误。 If the form is submitted and the array of errors is empty, then it would mean that everything worked out. 如果提交表单并且错误数组为空,则意味着一切都解决了。 Here's how it might look: 这是它的外观:

$errors = array(); // this will contain all your errors

if (isset($_POST['button'])) {

    // store all your submitted values
    $nom = $_POST['nom'];
    $sexe = $_POST['sexe'];
    $Username = $_POST['Username'];
    $email = $_POST['email'];
    $tel = $_POST['tel'];
    $adresse = $_POST['adresse'];
    $ville = $_POST['ville'];
    $pseudo = $_POST['pseudo'];
    $mdp = $_POST['mdp'];
    $date = $_POST['date'];
    $profession = $_POST['profession'];

    // check username
    $query_global = mysql_query("SELECT Username FROM users WHERE Username = '". $Username ."'") or die(mysql_error());

    $row = mysql_num_rows($query_global);

    if($row == 1){
        $errors[] = "The username is already registered, please choose another one <br>";
    }

    // check email
    $query_email = mysql_query("SELECT Email FROM users WHERE Email = '". $email ."'") or die(mysql_error());

    $row_email = mysql_num_rows($query_email);

    if($row_email == 1){
        $errors[] = "This email : '". $email ."' is already registered ";
    }

    // location where initial upload will be moved to
    $target = "images/" .$_FILES['uploaded']['name'] ;

    // find thevtype of image
    switch ($_FILES["uploaded"]["type"]) {
        case $_FILES["uploaded"]["type"] == "image/gif":
            move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
            break;
        case $_FILES["uploaded"]["type"] == "image/jpeg":
            move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
            break;
        case $_FILES["uploaded"]["type"] == "image/pjpeg":
            move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
            break;
        case $_FILES["uploaded"]["type"] == "image/png":
            move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
            break;
        case $_FILES["uploaded"]["type"] == "image/x-png":
            move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
            break;
        default:
            $errors[] = 'Seulement les JPG, PNG ou GIF sont acceptés!.';
    }

    // create user if no errors
    if (empty($errors)) {
        $query = "INSERT INTO Users Values ('', '".$nom."', '".$sexe."', '".$email."', ".$tel.", '".$adresse."', '".$ville."',  '".$pseudo."', '".$mdp."', curdate(), '$target', '".$date."', '".$profession."')";
        $add_user = mysql_query($query) or die(mysql_error());

        header('Location: Login/index.php');

    // otherwise display errors
    } else {
        $i = 0;
        echo "<p><span class='error'>";
        while ($i < count($errors)){
            echo $errors[$i] . '<br />';
            $i++;
        }
        echo "</span></p>";
    }
}

You are defining error in two types one is array and another one is string I think there is no need to define errors string at this time. 您正在定义两种类型的错误,一种是数组,另一种是字符串,我认为目前无需定义errors字符串。

Array 排列

$errors[]

String

 $error="";

Also store the already register email and user values in error array. 还将已注册的电子邮件和用户值存储在错误数组中。 Try below. 请尝试以下。

if(isset($_POST['button'])){


    $query_global = mysql_query("SELECT Username FROM users WHERE Username = '".$_POST['username']."' ") or die(mysql_error());

        $row = mysql_num_rows($query_global);

        if($row == 1){
            $error_username = "The username is already registered, please choose another one <br>";
        $error[] = "The username is already registered, please choose another one <br>";
        }

        $query_email = mysql_query("SELECT Email FROM users WHERE Email = '".$_POST['email']."' ") or die(mysql_error());

        $row_email = mysql_num_rows($query_email);

        if($row_email == 1){
            $error_email = "This email : '".$_POST['email']."' is already registered ";
        $error[] = "This email : '".$_POST['email']."' is already registered ";
        }

    if(isset($_POST['Username'])) { $Username = $_POST['Username']; }
    if(isset($_POST['email'])) { $email = $_POST['email']; }



    $nom = $_POST['nom'];
    $sexe = $_POST['sexe'];
    $email = $_POST['email'];
    $tel = $_POST['tel'];
    $adresse = $_POST['adresse'];
    $ville = $_POST['ville'];
    $pseudo = $_POST['pseudo'];
    $mdp = $_POST['mdp'];
    $date = $_POST['date'];
    $profession = $_POST['profession'];


    // location where initial upload will be moved to
    $target = "images/" .$_FILES['uploaded']['name'] ;

    // find thevtype of image
    switch ($_FILES["uploaded"]["type"]) {
    case $_FILES["uploaded"]["type"] == "image/gif":
        move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
        break;
    case $_FILES["uploaded"]["type"] == "image/jpeg":
        move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
        break;
    case $_FILES["uploaded"]["type"] == "image/pjpeg":
        move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
        break;
    case $_FILES["uploaded"]["type"] == "image/png":
        move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
        break;
    case $_FILES["uploaded"]["type"] == "image/x-png":
        move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
        break;

    default:
        $error[] = 'Seulement les JPG, PNG ou GIF sont acceptés!.';
    }



    if (empty($error)) {

    $query = "INSERT INTO Users Values ('', '".$nom."', '".$sexe."', '".$email."', ".$tel.", '".$adresse."', '".$ville."',  '".$pseudo."', '".$mdp."', curdate(), '$target', '".$date."', '".$profession."')";
    $add_user = mysql_query($query) or die(mysql_error());

    header('Location: Login/index.php');

        }

    }


    //display any errors
    if (!empty($error))
    {
            $i = 0;
            echo "<p><span class='error'>";
            while ($i < count($error)){
            echo $error[$i].'<br />';
            $i ++;}
            echo "</span></p>";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM