-1

データベース用の電子メール検証フォームを作成しようとしていますが、いくつかの問題があります。以下のコードを実行しようとすると、データベースが選択されていないというエラーが表示されます。

また、未定義変数エラーが発生します。ユーザー名フィールドの下のデータベースにユーザーの名前を入力したいのですが、どうやら $name は未定義の変数です。行 xx 未定義変数のエラーmysql_query("INSERT INTO registrations (username, password, email, hash) VALUES( '". mysql_real_escape_string($name) ."',

WAMP サーバーを使用しています。データベースの名前は sitememberdetails で、情報を入れる必要があるテーブルの名前は registrations です。私はこれにかなり慣れていません-変数を定義する方法とデータベースを選択する方法を誰かに教えてもらえますか(すでに選択されているように見えますが?)

     <?php

             $host = "localhost";
             $username = "";
             $password = "";
             $databasename = "sitememberdetails";
             $email="xxxxxx@xxxxxxxx.xxx";


              $connection = mysql_connect($host,$username,$password) or die        
             ("Error: ".mysql_error());

               mysql_select_db($databasename);("sitememberdetails") or  

               die(mysql_error());   



                    if(isset($_POST['name']) && !empty($_POST['name']) AND  

                    isset($_POST['email']) && !empty($_POST['email'])){  
                     $name = mysql_real_escape_string($_POST['name']);  
                     $email = mysql_real_escape_string($_POST['email']); }  



                      if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a- 

                        z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){

$msg = 'The email you have entered is invalid, please try again.';  
                }else{  

                    $msg = 'Your account has been made, <br /> please verify it     

              by clicking the activation link that has been send to  

                 your email.';  
                       }  


                        $hash = md5( rand(0,1000) ); 


                         $password = rand(1000,5000); 



                        mysql_query("INSERT INTO registrations (username, password,  

                          email, hash) VALUES( 
                          '". mysql_real_escape_string($name) ."', 
                          '". mysql_real_escape_string(md5($password)) ."', 
                          '". mysql_real_escape_string($email) ."', 
                          '". mysql_real_escape_string($hash) ."') ") or  

                          die(mysql_error());



                             $to      = $email; // Send email to our user  
                      $subject = 'Signup | Verification'; // Give the email a subject  
                      $message = ' 
                       Thanks for signing up! 
                      Your account has been created, you can login with the following  

                       credentials after you have activated your account by pressing  

                       the url below. 

                 Username: '.$name.' 
                 Password: '.$password.' 

                      Please click this link to activate your account: 
                      http://www.yourwebsite.com/verify.php?email='.$email.'& 

                      hash='.$hash.' 
                       ';  
                     $headers = 'From:noreply@yourwebsite.com' . "\r\n"; // Set from  

                       headers  
                       mail($to, $subject, $message, $headers); // Send our email  


                        ?>
4

4 に答える 4

1

この変更されたコードをチェックしてください:

<?php
         $host = "localhost";
         $username = "";
         $password = "";
         $databasename = "sitememberdetails";
         $email="xxxxxx@xxxxxxxx.xxx";


        $connection = mysql_connect($host,$username,$password) or die ("Error: ".mysql_error());
        mysql_select_db($databasename) or die(mysql_error());   


               $name = "";
           if(isset($_POST['name']) && !empty($_POST['name']) AND  
           isset($_POST['email']) && !empty($_POST['email'])){  
                 $name = mysql_real_escape_string($_POST['name']);  
                 $email = mysql_real_escape_string($_POST['email']); }  



                if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
                    $msg = 'The email you have entered is invalid, please try again.';  }
            else { 
                $msg = 'Your account has been made, <br /> please verify it     
                            by clicking the activation link that has been send to your email.';  
                  }  


            $hash = md5( rand(0,1000) ); 
            $password = rand(1000,5000); 

            mysql_query("INSERT INTO registrations (username, password,email, hash) VALUES( 
                      '". mysql_real_escape_string($name) ."', 
                      '". mysql_real_escape_string(md5($password)) ."', 
                      '". mysql_real_escape_string($email) ."', 
                      '". mysql_real_escape_string($hash) ."') ") or die(mysql_error());

            $to = $email; // Send email to our user  
                $subject = 'Signup | Verification'; // Give the email a subject  
            $message = ' Thanks for signing up! 
                Your account has been created, you can login with the following  
                credentials after you have activated your account by pressing  
                the url below. 
                    Username: '.$name.' 
                    Password: '.$password.' 
                Please click this link to activate your account: 
                http://www.yourwebsite.com/verify.php?email='.$email.'& 
                hash='.$hash.' 
               ';  

            $headers = 'From:noreply@yourwebsite.com' . "\r\n"; // Set from  

                mail($to, $subject, $message, $headers); // Send our email  

?>

mysql_関数の代わりにPDOを使用することをお勧めします

于 2012-10-30T15:29:34.733 に答える
1

このコードを変更してみてください

mysql_select_db($databasename);("sitememberdetails") or  

           die(mysql_error());

これに

mysql_select_db($databasename) or  die(mysql_error());

EOL;

if (database_connection) {
unset($undefined_variable_error)
} else {
echo $undefined_variable_error;
}
// Because mysql_real_escape_string needs an open mysql connection
于 2012-10-30T15:12:46.287 に答える
0

mysql_* の代わりに PDO を使用して、いくつかの改訂されたコードを次に示します。これが機能するかどうかお知らせください。そこから問題に対処できます。

<?php

            $host = 'localhost';
            $dbname = 'sitememberdetails';
            $user = '';
            $pass = '';
            try
            {
                $DB = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
            }
            catch(PDOException $e)
            {  
                echo $e->getMessage();  
            }

            if(isset($_POST['name']) && !empty($_POST['name']) AND isset($_POST['email']) && !empty($_POST['email']))
            {  
                $name = $_POST['name'];  
                $email = $_POST['email'];
            }
            else
            {
                $name = 'No Name';  
                $email = 'No Email';
            }

            if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email))
            {
                $msg = 'The email you have entered is invalid, please try again.';  
            }else{  
                $msg = 'Your account has been made, <br /> please verify it by clicking the activation link that has been send to your email.';  
            }  

            $hash = md5( rand(0,1000) ); 
            $password = rand(1000,5000); 

            $query = "INSERT INTO registrations (username, password, email, hash) VALUES('?', '?', '?', '?')";
            $sth = $DB->prepare($query);
            //By using ?'s and prepare/execute, PDO will prevent SQL Injection for you!
            $sth->execute(array($name, md5($password), $email, $hash));

            $to      = $email; // Send email to our user  
            $subject = 'Signup | Verification'; // Give the email a subject  
            $message = 'Thanks for signing up! Your account has been created, 
                        you can login with the following credentials after you 
                        have activated your account by pressing the url below. 
                        Username: '.$name.' 
                        Password: '.$password.' 

                        Please click this link to activate your account: 
                        http://www.yourwebsite.com/verify.php?email='.$email.'& 

                        hash='.$hash;  
            $headers = 'From:noreply@yourwebsite.com' . "\r\n"; // Set from header  
            mail($to, $subject, $message, $headers); // Send our email  
?>
于 2012-10-30T15:15:10.523 に答える
0

$name 値がフォームに投稿されていないようです。name 変数が設定されていて空でない場合、mysql_escaping を実行していますが、name 変数がまったく設定されていない場合はどうなりますか? これにはチェックがないため、INSERT ステートメントに到達してエラーが発生するまで続行されます。

データベースを選択するには、ここの例 #1 を見てください。($databasename); の後にセミコロンがあります。これは意味がありません。

于 2012-10-30T15:13:28.650 に答える