将'eregi_replace'函数转换为'preg_replace'和'mysql_num_rows'参数修复

时间:2012-10-12 04:06:35

标签: php html mysql function preg-replace

我已经创建了一个register.php文件来注册我正在构建的网站。我正在运行XAMPP来托管我的网站并在我通过付费主机上传之前对其进行测试。在几个视频和在线论坛的帮助下制作了php文件之后,我在google chrome中打开了它,并填写了我创建的注册表单。但是在按下'submit'时会出现以下错误,而不是将用户信息成功写入mysql数据库。

  

不推荐使用:函数eregi_replace()在第53行的C:\ xampp \ htdocs \ register.php中已弃用

     

警告:mysql_num_rows()期望参数1为资源,第56行的C:\ xampp \ htdocs \ register.php中给出布尔值

     

不推荐使用:函数eregi_replace()在第97行的C:\ xampp \ htdocs \ register.php中已弃用

     

不推荐使用:函数eregi_replace()在第98行的C:\ xampp \ htdocs \ register.php中已弃用

     

不推荐使用:函数eregi_replace()在第99行的C:\ xampp \ htdocs \ register.php中已弃用

     

不推荐使用:函数eregi_replace()在第100行的C:\ xampp \ htdocs \ register.php中已弃用

我知道与eregi_replace()函数相关的错误原因是因为php语言不再支持/使用它。我也知道有preg_replace()的另一种选择但是问题在于,作为php领域的新手,我无法想出一个解决方案。我每天都在学习一点,但我需要快速完成这个页面以继续我的网站和学校我没有时间尝试这么多的多个代码块来提出解决方案。我道歉;我需要一点勺子喂食。 :/如果您可以接受我的代码并告诉我如何修复上面列出的错误,甚至更好地回复代码的固定副本,我们将非常感激!感谢您的时间,我再次为我缺乏知识而道歉。

register.php:

    <?php
//User check log
//include_once("Scripts/checkuserlog.php");
?>

<?php
// let's initialize vars to be printed to page in the HTML section so our script does not return errors 
// they must be initialized in some server environments
$errorMsg = "";
$firstname = "";
$lastname = "";
$email1 = "";
$email2 = "";
$pass1 = "";
$pass2 = "";

// This code runs only if the form submit button is pressed
if (isset ($_POST['firstname'])){

    /* Example of cleaning variables in a loop
    $vars = "";
    foreach ($_POST as $key => $value) {
       $value = stripslashes($value);
       $vars .= "$key = $value<br />";
    }
    print "$vars";
    exit();
    */
     $firstname = $_POST['firstname'];
     $lastname = $_POST['lastname'];
     $email1 = $_POST['email1'];
     $email2 = $_POST['email2'];
     $pass1 = $_POST['pass1'];
     $pass2 = $_POST['pass2'];

     $firstname = stripslashes($firstname);
     $lastname = stripslashes($lastname);
     $email1 = stripslashes($email1); 
     $pass1 = stripslashes($pass1); 
     $email2 = stripslashes($email2);
     $pass2 = stripslashes($pass2); 

     $firstname = strip_tags($firstname);
     $lastname = strip_tags($lastname);
     $email1 = strip_tags($email1);
     $pass1 = strip_tags($pass1);
     $email2 = strip_tags($email2);
     $pass2 = strip_tags($pass2);

     // Connect to database
     include_once "/Scripts/connect_to_mysql.php";
     $emailCHecker = mysql_real_escape_string($email1);
     $emailCHecker = eregi_replace("`", "", $emailCHecker);
     // Database duplicate e-mail check setup for use below in the error handling if else conditionals
     $sql_email_check = mysql_query("SELECT email FROM members WHERE email='$emailCHecker'");
     $email_check = mysql_num_rows($sql_email_check); 

     // Error handling for missing data
     if ((!$firstname) || (!$lastname) || (!$email1) || (!$email2) || (!$pass1) || (!$pass2)) { 

     $errorMsg = 'ERROR: You did not submit the following required information:<br /><br />';

     if(!$firstname){ 
       $errorMsg .= ' * First Name<br />';
     } 
     if(!$lastname){ 
       $errorMsg .= ' * Last Name<br />';
     } 
     if(!$email1){ 
       $errorMsg .= ' * Email Address<br />';      
     }
     if(!$email2){ 
       $errorMsg .= ' * Confirm Email Address<br />';        
     }  
     if(!$pass1){ 
       $errorMsg .= ' * Login Password<br />';      
     }
     if(!$pass2){ 
       $errorMsg .= ' * Confirm Login Password<br />';        
     }  

     } else if ($email1 != $email2) {
              $errorMsg = 'ERROR: Your Email fields below do not match<br />';
     } else if ($pass1 != $pass2) {
              $errorMsg = 'ERROR: Your Password fields below do not match<br />';
     } else if ($email_check > 0) { 
              $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our database. Please use another.<br />"; 

     } else { // Error handling is ended, process the data and add member to database
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     $firstname = mysql_real_escape_string($firstname);
     $lastname = mysql_real_escape_string($lastname);
     $email1 = mysql_real_escape_string($email1);
     $pass1 = mysql_real_escape_string($pass1);

     $firstname = eregi_replace("`", "", $firstname);
     $lastname = eregi_replace("`", "", $lastname);
     $email1 = eregi_replace("`", "", $email1);
     $pass1 = eregi_replace("`", "", $pass1);

     // Add MD5 Hash to the password variable
     $db_password = md5($pass1); 

     // Add user info into the database table for the main site table(audiopeeps.com)
     $sql = mysql_query("INSERT INTO members (firstname, lastname, email, password, sign_up_date) 
     VALUES('$firstname','$lastname','$email1','$db_password', now())")  
     or die (mysql_error());

     $id = mysql_insert_id();

     // Create directory(folder) to hold each user's files(pics, MP3s, etc.)        
     mkdir("members/$id", 0755);    

    //!!!!!!!!!!!!!!!!!!!!!!!!!    Email User the activation link    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    $to = "$email1";

    $from = "admin@Connect.CloudNine.com";
    $subject = "Complete your registration at Cloud Nine";
    //Begin HTML Email Message
    $message = "Hi $firstname,

   Complete this step to activate your login identity at [ yourdomain ].

   Click the line below to activate when ready.

   localhost/activation.php?id=$id&sequence=$db_password
   If the URL above is not an active link, please copy and paste it into your browser address bar

   Login after successful activation using your:  
   E-mail Address: $email1 
   Password: $pass1

   See you on the site!
   ";
   //end of message
    $headers  = "From: $from\r\n";
    $headers .= "Content-type: text\r\n";

    mail($to, $subject, $message, $headers);

   $msgToUser = "<h2>One Last Step - Activate through Email</h2><h4>OK $firstname, one last step to verify your email identity:</h4><br />
   In a moment you will be sent an Activation link to your email address.<br /><br />
   <br />
   <strong><font color=\"#990000\">VERY IMPORTANT:</font></strong> 
   If you check your email with your host providers default email application, there may be issues with seeing the email contents.  If this happens to you and you cannot read the message to activate, download the file and open using a text editor.<br /><br />
   ";


   include_once 'msgToUser.php'; 

   exit();

   } // Close else after duplication checks

} else { // if the form is not posted with variables, place default empty variables

      $errorMsg = "Fields marked with an [ * ] are required";
      $firstname = "";
      $lastname = "";
      $email1 = "";
      $email2 = "";
      $pass1 = "";
      $pass2 = "";
}

?>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome To Cloud Nine</title>
<link href="CSS/register.css" rel="stylesheet" type="text/css">
<link href="CSS/css_boxes_register.css" rel="stylesheet" type="text/css">
<link href="CSS/reg_table_register.css" rel="stylesheet" type="text/css">

</head>

<body>
    <!--Floating Dock-->
    <div id="floating_dock">
    <a href="html_index.html" id="logo"><img src="Images/cloudnine_logo.png" width="220px"></a>
    <img src="Images/button.png" width="75" height="50" id="button"></div>
    <!--Floating Dock End-->

    <!--Content Wrap-->
<div id="container_alt">

<form action="register.php" method="post" enctype="multipart/form-data" class="box">
<h3>Account Registration</h3>
<p>&nbsp;</p>
<p>

<table width="447" border="0" align="center" cellpadding="5" cellspacing="1">
  <tr>
    <td width="435" align="center" valign="middle"><?php print "$errorMsg"; ?></td>
  </tr>
  <tr>
    <td align="center">First Name</td>
  </tr>
  <tr>
    <td align="center"><input name="firstname" type="text" id="firstname" value="<?php print "$firstname";?>" size="35" maxlength="35"></td>
  </tr>
  <tr>
    <td align="center">Last Name</td>
  </tr>
  <tr>
    <td align="center"><input name="lastname" type="text" id="lastname" value="<?php print "$lastname";?>" size="35" maxlength="35"></td>
  </tr>
  <tr>
    <td align="center">Password</td>
  </tr>
  <tr>
    <td align="center"><input name="pass1" type="text" id="pass1" value="<?php print "$pass1";?>" size="35" maxlength="35"></td>
  </tr>
  <tr>
    <td align="center">Confirm Password</td>
  </tr>
  <tr>
    <td align="center"><input name="pass2" type="text" id="pass2" value="<?php print "$pass2";?>" size="35" maxlength="35"></td>
  </tr>
  <tr>
    <td align="center">Email</td>
  </tr>
  <tr>
    <td align="center"><input name="email1" type="text" id="email1" value="<?php print "$email1";?>" size="35" maxlength="35"></td>
  </tr>
  <tr>
    <td align="center">Confirm Email</td>
  </tr>
  <tr>
    <td align="center"><input name="email2" type="text" id="email2" value="<?php print "$email2";?>" size="35" maxlength="35"></td>
  </tr>
  <tr>
    <td align="center"><input type="submit" name="submit" value="Submit Form"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
</p>
</form>

</div>



</body>

</html>

1 个答案:

答案 0 :(得分:0)

如果您不需要,请不要进行正则表达式。改变

eregi_replace("`", "", $emailCHecker);

str_replace("`", "", $emailCHecker);

不要使用mysql_ *函数,因为它们已被弃用。使用mysqli或PDO或任何你喜欢的风格但不再使用mysql_ *!

  

不鼓励使用此扩展程序。相反,MySQLi或PDO_MySQL   应该使用扩展名。另请参见MySQL:选择API指南和   相关常见问题解答了解更多信息。