将数据插入mysql

时间:2014-02-25 16:10:38

标签: php mysql

我想通过ID和电子邮件进行检查,但下面的代码不起作用,谢谢你帮助我 执行以下步骤

通过facebook id进行验证

  • 如果有id ==>更新BD并重定向home.php

  • 如果id不存在,我们将检查BD上是否存在电子邮件(从Facebook检索)

  • if yes ==>显示“此电子邮件已被使用”

  • 如果不是==>在数据库上插入并==>重定向到home.php

    $cont = $idf;
    
    $idf = $user_profile['id'];
    
    $email = $user_profile['email'];
    
    $query = "SELECT idf from users where idf = $idf ";
    $result = mysql_query($query);
    
    
    if(mysql_num_rows($result) > 1)
    
    
    {
        // row exists. so update
    
        mysql_query("UPDATE users SET name = '".$name."', birthday = '".$birthday."', gender = '".$gender."', email = '".$email."' WHERE idf = '".$idf."';") or die(mysqli_error());
    
    header( "refresh:4;url=/home.php" );
    
    } 
    
    else 
    
    {
    
    $sql1 = "SELECT * FROM users WHERE email = $email ";
       $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());
    
       if (mysqli_num_rows($result1) > 0)
    
    
    
     echo "This Email is already used.";
    
    }
    
      else
    
    
    {
    
        // row  does not exists. so create new
        //insertion dans la base de donnée
    $sql= mysql_query("INSERT INTO users (name, email, birthday, gender,idl,idf ) VALUES ('".$name."', '".$email."', '".$birthday."', '".$gender."',NULL,'".$idf."');");
    
    //mysql_query ($sql) or die ('Erreur SQL !'.$sql.'<br />'.mysql_error());
    
    
    //echo 'Le cookie existe   ' . $cont . '<br />';
    
    mysql_close();
    
    setcookie("cokiz", $cont, time()+3600, "/", "NDD");
    setcookie("cokiz", $cont, time()+3600, "/", "NDD");
    
    header( "refresh:4;url=/home.php" );
    
    
    }
    }
    
    
    ?>
    

3 个答案:

答案 0 :(得分:0)

    use this this will work i think so , use myl_sqli_query  or  mysql_query

    $cont = $idf;

    $idf = $user_profile['id'];

    $email = $user_profile['email'];

    $query = "SELECT idf from users where idf = $idf ";
    $result = mysql_query($query);


    if(mysql_num_rows($result) > 1)


    {
        // row exists. so update

        mysql_query("UPDATE users SET name = '".$name."', birthday = '".$birthday."', gender = '".$gender."', email = '".$email."' WHERE idf = '".$idf."';") or die(mysqli_error());

    header( "refresh:4;url=/home.php" );

    } 

    else 

    {

    $sql1 = "SELECT * FROM users WHERE email = $email ";
       $result1 = mysql_query($mysql,$sql1) or die(mysql_error());

       if (mysql_num_rows($result1) > 0)



     echo "This Email is already used.";

    }

      else


    {

        // row  does not exists. so create new
        //insertion dans la base de donnée
    $sql= mysql_query("INSERT INTO users (name, email, birthday, gender,idl,idf ) VALUES ('".$name."', '".$email."', '".$birthday."', '".$gender."',NULL,'".$idf."');");

    //mysql_query ($sql) or die ('Erreur SQL !'.$sql.'<br />'.mysql_error());


    //echo 'Le cookie existe   ' . $cont . '<br />';

    mysql_close();

    setcookie("cokiz", $cont, time()+3600, "/", "NDD");
    setcookie("cokiz", $cont, time()+3600, "/", "NDD");

    header( "refresh:4;url=/home.php" );


    }
    }


    ?>

答案 1 :(得分:0)

错误是你错过了

{

行之前:

echo "This Email is already used.";

答案 2 :(得分:0)

此代码没问题:(仅通过ID验证):

$idf = $user_profile['id'];

$cont = $idf;

$query = "SELECT idf from users where idf = $idf ";
$result = mysql_query($query);


if(mysql_num_rows($result) > 1)
{
    // row exists. so update

    mysql_query("UPDATE users SET name = '".$name."', birthday = '".$birthday."', gender = '".$gender."', email = '".$email."' WHERE idf = '".$idf."';");
} else {
//echo $email;
    // row  does not exists. so create new
    //insertion dans la base de donnée
$sql= mysql_query("INSERT INTO users (name, email, birthday, gender,idl,idf ) VALUES ('".$name."', '".$email."', '".$birthday."', '".$gender."',NULL,'".$idf."');");

//mysql_query ($sql) or die ('Erreur SQL !'.$sql.'<br />'.mysql_error());


//echo 'Le cookie existe   ' . $cont . '<br />';

mysql_close();

setcookie("cokiz", $cont, time()+3600, "/", "NDD");
setcookie("cokiz", $cont, time()+3600, "/", "NDD");

header( "refresh:4;url=/home.php" );


}

?>

但是当我通过邮件添加验证时:它不起作用(下面的代码)

$cont = $idf;

    $idf = $user_profile['id'];

    $email = $user_profile['email'];

    $query = "SELECT idf from users where idf = $idf ";
    $result = mysql_query($query);


    if(mysql_num_rows($result) > 1)




    {
        // row exists. so update

        mysql_query("UPDATE users SET name = '".$name."', birthday = '".$birthday."', gender = '".$gender."', email = '".$email."' WHERE idf = '".$idf."';");


    } 

    else 

    {

    $sql1 = "SELECT * FROM users WHERE email = $email ";
       $result1 = mysql_query($mysql,$sql1) or die(mysql_error());


       if (mysql_num_rows($result1) > 0)

 {

     echo "This Email is already used.";

    }

      else


    {

        // row  does not exists. so create new
        //insertion dans la base de donnée
    $sql= mysql_query("INSERT INTO users (name, email, birthday, gender,idl,idf ) VALUES ('".$name."', '".$email."', '".$birthday."', '".$gender."',NULL,'".$idf."');");

    mysql_query ($sql) or die ('Erreur SQL !'.$sql.'<br />'.mysql_error());


    //echo 'Le cookie existe   ' . $cont . '<br />';

    mysql_close();

    setcookie("cokiz", $cont, time()+3600, "/", "NDD");
    setcookie("cokiz", $cont, time()+3600, "/", "NDD");

    header( "refresh:4;url=/home.php" );

    }
    }



    ?> 
有人有个主意吗?谢谢