无法选择一个表并同时插入一些内容(Mysql)

时间:2014-11-26 01:39:22

标签: php mysql select insert

我有这个注册表格,当用户完成表格时他会最终提交,但我想检查用户名和电子邮件是否已经存在,这对吗? 已经有2天试图解决这个问题,但没有运气。 对不起,我没有使用最新版本的MySQL,因为这是我在课堂上学到的唯一版本。我将学习改进的。

我已经对谷歌进行了大量的研究,我发现这是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>";
    }

即使所有这些都解决了有什么方法可以阻止表单在输入错误时提交,我的意思是我已经在另一个文件中尝试了这个并且它工作但是即使输入错误,表单也会提交。

无法等待您的回答。

3 个答案:

答案 0 :(得分:0)

您没有任何阻止脚本运行的内容

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

//all the rest of your code here

}

您在检查之前没有设置$ error,而且不确定目的是什么:

$error="";

if (!$error) {

答案 1 :(得分:0)

我认为你对所有的错误变量感到困惑。

不是将错误存储在不同的变量中,而是使用单个数组来保存所有错误。如果提交表单并且错误数组为空,那么这意味着一切都会成功。这是它的外观:

$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>";
    }
}

答案 2 :(得分:0)

您正在定义两种类型的错误,一种是数组,另一种是字符串我认为此时无需定义errors字符串。

阵列

$errors[]

字符串

 $error="";

还将已注册的电子邮件和用户值存储在错误数组中。请尝试以下。

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>";
    }