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 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)