【问题标题】:Getting an unexpected "syntax error" in my PHP code [closed]在我的 PHP 代码中出现意外的“语法错误”[关闭]
【发布时间】:2014-04-06 09:12:39
【问题描述】:

我真的被这个问题困住了。我怎样才能让它工作?它从电子邮件中选择用户并通过电子邮件重新发送密码。

我在第 45 行遇到错误,但找不到:

解析错误:语法错误,第 45 行 /home/xxxxxx/public_html/xxxx/email.php 中的意外 'not' (T_STRING)

这是我的代码:

    <?php

$host="localhost"; // Host name - update all those fields
 $username="xxxxx"; // Mysql username
  $password="xxxx"; // Mysql password 
  $db_name="xxxxxx"; // Database name

//Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB");

// value sent from form 
$email_to=$_POST['email_to']; // table name 
$tbl_name=tz_members;

// retrieve password from table where e-mail is equal
 $sql="SELECT usr, pass, regIP FROM $tbl_name WHERE email='$email_to'"; $result=mysql_query($sql);

// if found this e-mail address, row must be 1 row // keep value in variable name "$count" 
$count=mysql_num_rows($result);

// compare if $count =1 row 
if($count==1) { $rows=mysql_fetch_array($result);

$your_username=$rows['usr']; $your_password=$rows['pass']; $your_ip=$rows['regIP']; $pass = substr(md5($your_ip.microtime().rand(1,100000)),0,6); // create a new pass
 mysql_query(" UPDATE tz_members SET pass='".md5($pass)."' WHERE email='".$email_to."'"); // update the db

// ---------------- SEND MAIL FORM ---------------- // send e-mail to ...
$to=$email_to;

// Your subject 
$subject="Your password here";

// From 
$header="from: your name ";

// Your message
 $messages= "Your password for login has been reset.\r\n"; $messages.="The new password is $pass \r\n"; $messages.="Regards \r\n";

// send email
 $sentmail = mail($to,$subject,$messages,$header);

mysql_free_result($result); }

// else if 
$count not equal 1 else if ($count==0) echo "Your email was not found in our database"; else echo "More than one (".$count.") email records was found in our database, please contact the administrator.";

// if your email succesfully sent 
if($sentmail) { echo "The new password has been reset and sent to the email on record."; } else { echo "Cannot send the password to this e-mail address"; }

?>

这是一种从数据库中重置密码的表单。

【问题讨论】:

  • 危险:您使用的是an obsolete database API,应该使用modern replacement。您也容易受到SQL injection attacks的影响,现代 API 可以让您更轻松地从 defend 中获得。
  • "我在第 45 行遇到错误但找不到它" — 什么错误?
  • 您使用的是an unsuitable hashing algorithm,需要take better care的用户密码。
  • 第 45 行的 $count not equal 1 是一个无效的表达式(据我所知)。你的意思可能是if ($count != 1)
  • 您好。提问时,请添加所有必要的信息。如果您遇到错误,最好在问题中说明错误,而不是等待有人问您这是什么错误。

标签: php


【解决方案1】:

我认为发生的事情是您的评论中有回车。

改变

  // else if 
   $count not equal 1 else if ($count==0) echo "Your email was not found in our database";     
   else echo "More than one (".$count.") email records was found in our database, please contact the administrator.";

   // else if $count not equal 1 

else if ($count==0) echo "Your email was not found in our database"; 
else echo "More than one (".$count.") email records was found in our database, please contact the administrator.";

编辑

根据 Halfer 的评论,有关单行 if 语句的讨论,请参见此处。 Formatting of if Statements

【讨论】:

  • 建议始终使用大括号可能是个好主意 - 仅在某些时候使用它们可能会成为初学者的绊脚石,因为可能不清楚 if/else 子句中包含的内容和什么不是。
最近更新 更多