PHP注册表格带来错误

时间:2015-04-09 10:19:46

标签: php html

我正在尝试创建一个注册系统,它会在用户注册后向用户发送激活码,但是我收到了注册表单错误,其中包含语法错误,意外情况和回声消息。 (T_ECHO在第133行的register.php中。



<?php
include('database_connection.php');
?>
<head>
  <title>HireMyItems</title>
  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   <link rel="stylesheet" type="text/css" href="css/style.css" />
     <link rel="shortcut icon" type="image/x-icon" href="images/icons/logo.png" />`
   <link href="css/style.css" rel="stylesheet" type="text/css">
  </head>

<body>
  <div id="main">
    <div id="header">
	  <div id="banner">
	    <h1>HireMyItems</h1>
        <div id="menubar">
          <ul id="menu">
            <li><a href="index.php">Home</a></li>
            <li><a href="login.php">Login</a></li>
			<li><a href="signup.php">Signup</a></li>
            <li><a href="contact.html">ContactUs</a></li>
            <li><a href="basket.html">Basket</a></li>
            </ul>
        </div><!--close menubar-->
	    <div id="contact">
	      <a href="https://twitter.com/HireItems" target="_blank""><img src="images/icons/twitter.png" /></a>
		  <a href="https://en-gb.facebook.com/people/HireMy-Items/100009321336405"target="_blank""><img src="images/icons/facebook.png"  /></a>
		  <a href="https://www.linkedin.com/profile/preview?locale=en_US&trk=prof-0-sb-preview-primary-button"target="_blank"><img src="images/icons/linkedin.png" /></a>
	    </div><!--close welcome-->
	  </div><!--close banner-->
    </div><!--close header-->

	<div id="site_content">	
	  <div class="sidebar_container">       
		<div class="sidebar">
          <div class="sidebar_item">
            <h2>Rent Items</h2>
			
            <p>Please contact us by email or telephone if you want to list an item for renting.</p>
		  		<div class="button_small">
		      <a href="#">ContactUs</a>
		    </div><!--close button_small-->
          </div><!--close sidebar_item--> 
        </div><!--close sidebar-->     		
		<div class="sidebar">
          <div class="sidebar_item">
            <h2>GreenRecycle</h2>
			<a href="http://www.greenrecycling.co.uk/" target="_blank"><img src="images/banners/greenrecycle.png"></a>
            <p></p>
		  	<div class="">
		      <a href="#"></a>
		    </div><!--close button_small-->
          </div><!--close sidebar_item--> 
        </div><!--close sidebar-->  
       </div><!--close sidebar_container-->	
	   

<h6>Register Form</h6>           
<form action="Register.php" method="post">

<p>Create A new Account <span style="background:#EAEAEA none repeat scroll 0 0;line-height:1;margin-left:210px;;padding:5px 7px;"> Already a member? <a href="login.php">Log in</a></span> </p> 

<div id="registermain">
<div id="register">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Email :</label>
<input id="name" name="email" placeholder="email" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Register ">
<span><?php echo $error; ?></span>
</form>
</div>
</div>

<?php
if (isset($_POST['formsubmitted'])) { 

 $error = array(); //Declare An Array to store any error message 

  if (empty($_POST['name'])) { //if no name has been supplied 

  $error[] = 'Please Enter a UserName '; //add to array "error" 

 } else { 

$name = $_POST['name']; //else assign it a variable 

 } 

if (empty($_POST['e-mail'])) { 


$error[] = 'Please Enter your Email '; 

 } else { 

 if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", 


  $_POST['e-mail'])) { 

 //regular expression for email validation 

   $Email = $_POST['e-mail']; 

 } else { 

  $error[] = 'Your EMail Address is invalid  '; 
 } 
  } 
  
  if (empty($_POST['Password'])) { 
 $error[] = 'Please Enter Your Password '; 
 
    } else { 

$Password = $_POST['Password']; 

} 

if (empty($error)) //send to Database if there's no error ' 

{ // If everything's OK... 

// Make sure the email address is available:
 $query_verify_email = "SELECT * FROM login  WHERE email ='$Email'";
  $result_verify_email = mysql_query($dbc, $query_verify_email);
 if (!$result_verify_email) { //if the Query Failed ,similar to if
 ($result_verify_email==false)
  echo ' Database Error Occured ';
  }
  
  if (mysql_num_rows($result_verify_email) == 0) { //IF no previous user is using this email .  
  // Create a unique  activation code:
  $activation = md5(uniqid(rand(), true));
  
  $query_insert_user =   "INSERT INTO `members` ( `username`, `email`, `password`, `Activation`) VALUES ( '$name', '$Email', '$Password', '$activation')";
  
  
 $result_insert_user = mysql_query($dbc, $query_insert_user); 

if (!$result_insert_user) { 
echo 'Query Failed '; 
   } 
   if (mysql_affected_rows($dbc) == 1) { //If the Insert Query was successful.
   
   
 // Send the email: 

$message = " To activate your account, please click on this link:\n\n"; 

$message .= WEBSITE_URL . '/activate.php?email=' . urlencode($Email) . "&key=$activation"; 

 mail($Email, 'Registration Confirmation', $message, 'From:'.EMAIL); 

// Flush the buffered output. 

// Finish the page: 

echo '<div class="success">Thank you for registering! A confirmation email has been sent to ' . $Email . 

 ' Please click on the Activation Link to Activate your account </div>'; 
 } else { // If it did not run OK.
 
echo '<div class="errormsgbox">You could not be registered due to a system error. We apologize for any inconvenience.</div>'; 
   } 

   
 } else { // The email address is not available. 


echo '<div class="errormsgbox" >That email address has already been registered. </div>'; 
   } 
   
   } else { //If the "error" array contains error msg , display them
   
    echo '<div class="errormsgbox"> <ol>';
	foreach ($error as $key => $values) {
	 echo '  <li>' . $values . '</li>';
	 }
	 echo '</ol></div>';
	 }
	 
	 mysql_close($dbc); //Close the DB Connection
	 } // End of the main Submit conditional.
	 ?>
&#13;
&#13;
&#13;

Database_connection.php文件

&#13;
&#13;
<?php

/*define the database connection
DEFINE('DATABASE_USER', 'XXXX');
DEFINE('DATABASE_PASSWORD', 'XXXX');
DEFINE('DATABASE_HOST', 'localhost');
DEFINE('DATABASE_NAME', 'login');


//This is the address that will appear coming from ( Sender ) 
define('EMAIL', 'ZZZZ@gmail.com'); 


/*Define the root url where the script will be found such as */ 
DEFINE('WEBSITE_URL', 'http://zzz.com'); 


// Establishing Connection with Server and making the connection
$dbc = mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);

if (!$dbc) { 
trigger_error('Could not connect to MySQL: ' . mysql_connect_error()); 
} 
?> 
&#13;
&#13;
&#13;

Register.php,我收到错误。

&#13;
&#13;
$query_verify_email = "SELECT * FROM login WHERE email ='$Email'";
  $result_verify_email = mysql_query($dbc, $query_verify_email);
 if (!$result_verify_email) { //if the Query Failed ,similar to if
 $result_verify_email==false;
  echo ' Database Error Occured ';
  }
  
  if (mysql_num_rows($result_verify_email) == 0) { //IF no previous user is using this email .  
  // Create a unique  activation code:
  $activation = md5(uniqid(rand(), true));
  
  $query_insert_user =   "INSERT INTO `members` ( `username`, `email`, `password`, `Activation`) VALUES ( '$name', '$Email', '$Password', '$activation')";
  
  
 $result_insert_user = mysql_query($dbc, $query_insert_user); 
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

if条件

if (!$result_verify_email) { //if the Query Failed ,similar to if
 ($result_verify_email==false)
  echo ' Database Error Occured ';
  }

这一行($result_verify_email==false)应为

$result_verify_email==false;

更新:

database_connection.php文件中取消注释您的凭据

DEFINE('DATABASE_USER', 'XXXX');
DEFINE('DATABASE_PASSWORD', 'XXXX');
DEFINE('DATABASE_HOST', 'localhost');
DEFINE('DATABASE_NAME', 'login');

DEFINE('DATABASE_USER', 'XXXX');
DEFINE('DATABASE_PASSWORD', 'XXXX');
DEFINE('DATABASE_HOST', 'localhost');
DEFINE('DATABASE_NAME', 'login');

相应地更改$dbc

$dbc = mysql_connect(XXXX, XXXX, localhost, login);

并将此行更改为此行,因为它即使没有数据也会尝试获取数据

 if (!$result_verify_email) { 
 $result_verify_email==false;
  echo ' Database Error Occured ';
  exit();
  }