简体   繁体   English

使用php和mysql在注册表单上检索电子邮件地址很困难

[英]Difficulty on retrieving email address on a register form with php and mysql

Hi im am trying to allow users to be members in a website. 您好,我正在尝试允许用户成为网站的成员。 The code I have does not look to have any bugs or anything. 我拥有的代码看起来没有任何错误或任何东西。 When i try to register as a user it keeps show me an error that i haven't inserted the email address. 当我尝试注册为用户时,会一直显示我未​​插入电子邮件地址的错误。 The email field does exists in the database and i cannot find the problem. 电子邮件字段确实存在于数据库中,我找不到问题。

PHP Script PHP脚本

$errorMsg = "";
// First we check to see if the form has been submitted 
if (isset($_POST['username'])){
    //Connect to the database through our include 
    include_once "connect_to_mysql.php";
    // Filter the posted variables
    $username = str_replace("[^A-Z a-z0-9]", "", $_POST['username']); // filter everything but numbers and letters
    $country = str_replace("[^A-Z a-z0-9]", "", $_POST['country']); // filter everything but spaces, numbers, and letters
    $county = str_replace("[^A-Z a-z0-9]", "", $_POST['county']); // filter everything but spaces, numbers, and letters
    $city = str_replace("[^A-Z a-z0-9]", "", $_POST['city']); // filter everything but spaces, numbers, and letters
    $accounttype = str_replace("[^a-z]", "", $_POST['accounttype']); // filter everything but lowercase letters
    $email=str_replace( '/@/', '@', $email ); 
    $email = stripslashes($_POST['email']);
    $email = strip_tags($email);
    $email = mysql_real_escape_string($email);
    $password = str_replace("[^A-Z a-z0-9]", "", $_POST['password']); // filter everything but numbers and letters
    // Check to see if the user filled all fields with
    // the "Required"(*) symbol next to them in the join form
    // and print out to them what they have forgotten to put in
    if((!$username) || (!$country) || (!$county) || (!$city) || (!$accounttype) || (!$email) || (!$password)){

        $errorMsg = "You did not submit the following required information!<br /><br />";
        if(!$username){
            $errorMsg .= "--- User Name";
        } else if(!$country){
            $errorMsg .= "--- Country"; 
        } else if(!$county){ 
            $errorMsg .= "--- State"; 
       } else if(!$city){ 
           $errorMsg .= "--- City"; 
       } else if(!$accounttype){ 
           $errorMsg .= "--- Account Type"; 
       } else if(!$email){ 
           $errorMsg .= "--- Email Address"; 
       } else if(!$password){ 
           $errorMsg .= "--- Password"; 
       }
    } else {
    // Database duplicate Fields Check
    $sql_username_check = mysql_query("SELECT id FROM members WHERE username='$username' LIMIT 1");
    $sql_email_check = mysql_query("SELECT id FROM members WHERE email='$email' LIMIT 1");
    $username_check = mysql_num_rows($sql_username_check);
    $email_check = mysql_num_rows($sql_email_check); 
    if ($username_check > 0){ 
        $errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside our system. Please try another.";
    } else if ($email_check > 0){ 
        $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
    } else {
        // Add MD5 Hash to the password variable
       $hashedPass = md5($password); 
        // Add user info into the database table, claim your fields then values 
        $sql = mysql_query("INSERT INTO members (username, country, county, city, accounttype, email, password, signupdate) 
        VALUES('$username','$country','$state','$city','$accounttype','$email','$hashedPass', now())") or die (mysql_error());
        // Get the inserted ID here to use in the activation email
        $id = mysql_insert_id();
        // Create directory(folder) to hold each user files(pics, MP3s, etc.) 
        mkdir("memberFiles/$id", 0755); 
        // Start assembly of Email Member the activation link
        $to = "$email";
        // Change this to your site admin email
        $from = "info@chrysikourtina.x10.mx";
        $subject = "Complete your registration";
        //Begin HTML Email Message where you need to change the activation URL inside
        $message = '<html>
        <body bgcolor="#FFFFFF">
        Hi ' . $username . ',
        <br /><br />
        You must complete this step to activate your account with us.
        <br /><br />
        Please click here to activate now &gt;&gt;
        <a href="http://http://chrysikourtina.x10.mx/activation.php?id=' . $id . '">
        ACTIVATE NOW</a>
        <br /><br />
        Your Login Data is as follows: 
        <br /><br />
        E-mail Address: ' . $email . ' <br />
        Password: ' . $password . ' 
        <br /><br /> 
        Thanks! 
        </body>
        </html>';
        // end of message
        $headers = "From: $from\r\n";
        $headers .= "Content-type: text/html\r\n";
        $to = "$to";
        // Finally send the activation email to the member
        mail($to, $subject, $message, $headers);
        // Then print a message to the browser for the joiner 
        print "<br /><br /><br /><h4>OK $firstname, one last step to verify your email identity:</h4><br />
        We just sent an Activation link to: $email<br /><br />
        <strong><font color=\"#990000\">Please check your email inbox in a moment</font></strong> to click on the Activation <br />
        Link inside the message. After email activation you can log in.";
        exit(); // Exit so the form and page does not display, just this success message
    } // Close else after database duplicate field value checks
  } // Close else after missing vars check
} //Close if $_POST
?>

HTML Form HTML表格

 <table width="600" align="center" cellpadding="4">
  <tr>
    <td width="7%">REGISTER AS A MEMBER HERE </td>
  </tr>
</table>
<table width="600" align="center" cellpadding="5">
  <form action="join_form.php" method="post" enctype="multipart/form-data">
    <tr>
      <td colspan="2"><font color="#FF0000"><?php echo "$errorMsg"; ?></font></td>
    </tr>
    <tr>
      <td width="163"><div align="right">User Name:</div></td>
      <td width="409"><input name="username" type="text" value="<?php echo "$username"; ?>" /></td>
    </tr>
    <tr>
      <td><div align="right">Country:</div></td>
      <td><select name="country">
      <option value="<?php echo "$country"; ?>"><?php echo "$country"; ?></option>
      <option value="Cyprus">Cyprus</option>

      <option value="United Kingdom">United Kingdom</option>

      </select></td>
    </tr>
    <tr>
      <td><div align="right">County: </div></td>
      <td><input name="county" type="text" value="<?php echo "$county"; ?>" /></td>
    </tr>
    <tr>
      <td><div align="right">City: </div></td>
      <td>
        <input name="city" type="text" value="<?php echo "$city"; ?>" />
      </td>
    </tr>
    <tr>
      <td><div align="right">Account Type: </div></td>
      <td><select name="accounttype">
        <option value="<?php echo "$accounttype"; ?>"><?php echo "$accounttype"; ?></option>
        <option value="a">Normal User</option>
        <option value="b">Expert User</option>
        <option value="c">Super User</option>
      </select></td>
    </tr>
    <tr>
      <td><div align="right">Email: </div></td>
      <td><input name="email" type="text" id="<?php echo "$email"; ?>" value="<?php echo "$email"; ?>" />
      </td>
    </tr>
    <tr>
      <td><div align="right"> Password: </div></td>
      <td><input name="password" type="password" value="<?php echo "$password"; ?>" /> 
      <font size="-2" color="#006600">(letters or numbers only, no spaces no symbols)</font></td>
    </tr>
    <tr>
      <td><div align="right"> Captcha: </div></td>
      <td>Add Captcha Here for security</td>
    </tr>    
    <tr>
      <td><div align="right"></div></td>
      <td><input type="submit" name="Submit" value="Submit Form" /></td>
    </tr>
  </form>
</table>

Error : You did not submit the following required information! 错误:您未提交以下必需信息!

--- Email Address - - 电子邮件地址

All the other fields seem to be working fine!! 所有其他领域似乎都很好! If anyone has any idea of what is causing the problem please tell me!! 如果有人对导致问题的原因有任何想法,请告诉我! Thanks 谢谢

EDIT: IF you still get the same result the problem is with mysql_real_escape_string because it's the only function which return FALSE. 编辑:如果您仍然得到相同的结果,则问题出在mysql_real_escape_string,因为它是唯一返回FALSE的函数。

You forgot to define the value of the email variable. 您忘记定义email变量的值。

add this: 添加:

$email = $_POST['email'];

above this: 在此之上:

$email=str_replace( '/@/', '&#64;', $email ); 

and add this below: 并在下面添加:

   $email = mysql_real_escape_string(strip_tags(stripslashes($email)));

I believe you need to first get post value, then play with it. 我相信您首先需要获得职位价值,然后再使用它。 So consider the order change as below: 因此,请考虑以下订单更改:

$email = stripslashes($_POST['email']);
$email=str_replace( '/@/', '&#64;', $email );     
$email = strip_tags($email);
$email = mysql_real_escape_string($email);

Also a check in html file may work for further errors. 另外,检查html文件可能会导致其他错误。

<?php $email = isset($_POST['email']) ? $_POST['email'] : ''; ?>

Hope this works. 希望这行得通。

Use echo to display your variable's values. 使用echo显示变量的值。 See what $mail is when it is posted but before any filters are applied, and after each filter is applied. 查看$mail在发布时但在应用任何过滤器之前以及在应用每个过滤器之后是什么。 This can narrow down your search to where the error is actually occurring. 这样可以将搜索范围缩小到实际发生错误的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM