注册PHP页面出错

时间:2012-03-28 00:41:00

标签: php html

修改


所以我找到了解决办法。我用if替换了while语句,它完美地工作了。感谢所有帮帮忙的人!


当这个错误出现时,我正在编写一个注册页面(我仍然是编程和php的新手,所以这可能解释错误是什么,我只是不知道)

  

警告:mysql_fetch_array()要求参数1为资源,第22行的/Users/Jacob/Sites/website/postregister.php中给出布尔值

这是我的代码:

<!-- start sign up form -->
            <?php
            include("config.php");
            $firstname = mysql_real_escape_string($_REQUEST['firstname']);
            $lastname = mysql_real_escape_string($_REQUEST['lastname']);
            $email = mysql_real_escape_string($_REQUEST['email']);
            $birthyear = mysql_real_escape_string($_REQUEST['birthyear']);
            $city = mysql_real_escape_string($_REQUEST['city']);
            $address = mysql_real_escape_string($_REQUEST['address']);
            $state = mysql_real_escape_string($_REQUEST['state']);
            $phone = mysql_real_escape_string($_REQUEST['phone']);
            $password = mysql_real_escape_string($_REQUEST['password']);
            $zipcode= mysql_real_escape_string($_REQUEST['zipcode']);
            $active = 0;
            $hash = md5( rand(0,1000) ); // Generate random 32 character hash and assign it to a local variable.
            $sql= mysql_query("INSERT INTO member (firstname, lastname, email, phone, city, state, address, zipcode, password, hash, active)
            VALUES

            ('$firstname','$lastname','$email','$phone', '$city', '$state','$address', '$zipcode', '$password', '$hash', '$active')")or die(mysql_error());
                                    while($row == mysql_fetch_array($sql)){
                                    $to = $email; //Send email to our user
                                    $subject = 'Signup | Verification'; //// Give the email a subject 
                                    $message = '

                                    Thanks for signing up!<br/>
                                    Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.<br/>

                                    ------------------------------------------------------------------------------<br/>
                                    Email: '.$email.'<br/>
                                    Password: '.$password.'<br/>
                                    ------------------------------------------------------------------------------

                                    <a href="http://localhost/~jacob/pages/verify.php?email='.$email.'&hash='.$hash.'">Please Click Here to Activate Your Account</a>

                                    '; // Our message above including the link

                                    $headers  = "From: customersupport@example.com\r\n"; 
                                    $headers .= "Content-type: text/html\r\n";
                                    mail($to, $subject, $message, $headers); // Send the email
                                }

            ?>
            <form action="postregister.php" method="post">
                <label for="firstname">First Name:</label><br/>
                <input type="text" name="firstname" value="" size='90' style='height:20px;' /><br/><br/>
                <label for="lastname">Last Name:</label><br/>
                <input type="text" name="lastname" value="" size='90' style='height:20px;'/><br/><br/>
                <label for="email">Email:</label><br/>
                <input type="text" name="email" value="" size='90' style='height:20px;'/><br/><br/>
                <label for="birthyear">Birthyear:</label><br/>
                <input type="text" name="birthyear" value="" size='90' style='height:20px;'/><br/><br/>
                <label for="city">City:</label><br/>
                <input type="text" name="city" value="" size='90' style='height:20px;'/><br/><br/>
                <label for="phone">Phone:</label><br/>
                <input type="text" name="phone" value="" size='90' style='height:20px;'/><br/><br/>
                <label for="address">Address:</label><br/>
                <input type="text" name="address" value="" size='90' style='height:20px;'/><br/><br/>
                <label for="zipcode">Zipcode:</label><br/>
                <input type="text" name="zipcode" value="" size='90' style='height:20px;'/><br/><br/>
                <label for="State">State:</label><br/>
                <input type="text" name="state" value="" size='90' style='height:20px;'/><br/><br/>
                <label for="password">Password:</label><br/>
                <input type="password" name="password" value="" size='90' style='height:20px;'/><br/><br/>


                <input type="submit" class="submit_button" value="Sign up" />
            </form>

任何帮助都会非常赞赏。谢谢!

3 个答案:

答案 0 :(得分:2)

请阅读mysql_query()的手册。在返回值 ...

  

对于其他类型的SQL语句,INSERT,UPDATE,DELETE,DROP等,mysql_query()成功时返回TRUE,错误时返回FALSE。

由于您的查询是INSERT类型语句,mysql_query()将返回一个布尔值。也没有要获取的行。

您可以将代码更改为以下

$result = mysql_query("All that SQL");
if ($result === false) {
    // or die() is a terrible way to handle errors
    throw new Exception(mysql_error());
}

$to = $email;
// and so on

答案 1 :(得分:1)

大多数看到此消息的人都忘记了在执行他们的陈述时检查错误。您已经了解了or die行的mysql_query部分。

您的问题是您尝试在INSERT中循环生成的行,但INSERT不会返回一组行。来自PHP文档:

  

对于其他类型的SQL语句,INSERT,UPDATE,DELETE,DROP等,   mysql_query()成功时返回TRUE,错误时返回FALSE。

无论如何,你实际上并没有在你的循环中使用$row

而不是循环和mysql_fetch_array,您只需检查mysql_query是否返回true,在这种情况下您的行已添加,您可以发送电子邮件。你已经编写了查询的方式,虽然我不会亲自推荐它,但实际上这很容易:你只需要在查询行之后发送电子邮件,因为你已经die进入出现错误。

答案 2 :(得分:0)

您的查询似乎正在返回 false 。我想你的数据库连接有错误。尝试打印您的查询并在数据库上手动运行它以确认它正常运行。您也可以尝试一下这个简单的:

 $result = mysql_query("SELECT * FROM member LIMIT 10");
 $row = mysql_fetch_array($result);
 print_r($row);

看看会发生什么。我想这也会出错。检查您的include("config.php");并确保您的数据库链接已设置并正常运行。要确认您的$ sql没有获得有效的结果,您可以尝试将其转储并查看使用print_r或var_dump的类型。祝你好运。