dongyuli4538 2012-04-13 17:07
浏览 8
已采纳

PHP IF条件建议需要

I'm using emails as username to login into a site being developed, now if a user updates their email from the profile page, how can i make sure that my email checking statement doesnt catch the user's email as already registered in the database.

the page profile

 /* Now we will store the values submitted by form in variable */
           $fullname=$_POST['fullname'];
           $dob=$_POST['dob'];
           $address=$_POST['address'];
           $myusername=$_POST['myusername'];           
           $telephone=$_POST['telephone'];

$queryuser=mysql_query("SELECT * FROM customer WHERE email='$myusername' ");
$checkuser=mysql_num_rows($queryuser);

if($checkuser != 0) 
{ 
$Merr[]='» Sorry this email is already registered!';
}
else {$insert_user=mysql_query("UPDATE CUSTOMER SET SYNTAX HERE");

Now these are the fields in question;

(name, dob, address, email, telephone) VALUES ('$fullname', '$dob', '$address', '$myusername', '$telephone')

As you can see if the user changes the login email then the syntax checks for the email being submitted against the database, however if the user leaves the email unchanged he will get the error because it is found in the database.

I was thinking of something like;

if($checkuser != 0) {
  if($myusername == $_POST['myusername'])
   (...dont show error.) 

but my php skills are limited. can anyone advise please, thanks

  • 写回答

3条回答 默认 最新

  • dongtui8593 2012-04-13 17:17
    关注
    /* Now we will store the values submitted by form in variable */
    $myusername=$_POST['myusername'];           
    
    
    $queryuser=mysql_query("SELECT username FROM customer WHERE email='$myusername' ");
    $checkuser=mysql_num_rows($queryuser);
    
    /*Mysql_fetch_row will select the first row from the query and put it into a 
    multidimensional array. As you only want 1 e-mail associated with one user the query
    should only ever select one row anyway.  */
    $row = mysql_fetch_row($queryuser);
    
    if($checkuser != 0) 
    { 
          if($row[0] != $SESSION['username'])
          {
          //ERROR
          }
    
    }
    else {
    //Update details
    }
    

    That way you can just submit the e-mail along with any other inputs and if the e-mail hasn't changed you wont need to do anything.

    Also you've said your posting the logged in users username. I would imagine you would actually want to be using sessions to do that.

    And don't forget to escape the POST data for security.

    Also the INSERT INTO should that not be an update statement if they are updating their e-mail address?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错