0

我正在尝试为人们创建一个小表单来访问我的网站,只需输入以下信息:

  • 姓名
  • 电子邮件地址

它应该将此信息从我的数据库发送回我的表中。

现在这就是事情变得“复杂”的地方(......对我来说,好吗?)

如果电子邮件地址确实存在于我们的表中,则它必须回显“已添加电子邮件地址。谢谢!”。否则,如果它不存在,那么它必须回显“电子邮件地址已存在!”

由于某种原因,它没有这样做。它吐出这个错误:

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\wamp\www\addemail.php on line 46

我附上了我的php代码(如下):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>

<script LANGUAGE="JavaScript">
<!--
function redirect () { setTimeout("go_now()",3000); }
function go_now ()   { window.location.href = "newsletter_frame.html"; }
//-->
</script>

<!-- This is the link to the external CSS file-->
<link rel="stylesheet" type="text/css" href="css/mystyle.css">

<!-- This is the link to the external Javascript file-->
<script type="text/javascript" src="js/myjs.js"></script>

<title>Page</title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="verify-v1" content="gDSxHR1Tk8vWtty9GoRHABGFEH+Bh2VHYCHv0Cx1/Ek=">
<meta name="copyright" content="Burger man">
<meta name="keywords" content="afro, afrodeep, soulful, funky, deep, vocal house, broken beats, disco">
<meta name="description" content="Burgers and chips">
<meta name="page-type" content="Information">

</head>
    <body  onLoad="redirect()" id="iframe_newsl_spec">

        <?php
            $dbc = mysqli_connect('localhost', 'root', '', 'si_store') or die('Error connecting to MySQL server.');

                if ( isset($_POST['email_address']) && isset($_POST['firstname']) && isset($_POST['surname']) )
                    {
                        $email_address = $_POST['email_address'];
                        $firstname = $_POST['firstname'];
                        $surname = $_POST['surname'];

                        $query = mysqli_query("SELECT * FROM email_list WHERE email_address = '$email_address'");

                        if ( mysql_num_rows($query) > 0)
                        {
                            echo 'Email address already exists!';
                        }
                        else
                        {
                            mysqli_query("INSERT INTO email_list(email_address, firstname, surname) VALUES ('$email_address','$firstname','$surname')");

                            echo 'Email Address Added. Thank you!';
                        }   
                    }

            //mysqli_query($dbc, $query) or die ('Error connecting to database.');

            mysqli_close($dbc);
        ?>



    </body>
</html>
4

1 回答 1

0

一旦你修复了这些mysql_*函数,并将它们更改为mysqli_*并且你已经更改了代码以便使用准备好的语句,你应该添加以下代码:

$stmt = mysqli_prepare('SELECT * FROM email_list WHERE email_address = ?');
//BTW, if all you want to check if a record exists, don't use SELECT *
//Just select one field, not the entire row
mysqli_stmt_bind_param($stmt,'s',$_POST['email_address']);
//bind param, to statement, as String, value ^^
mysqli_stmt_execute($stmt);
//execute the query
mysqli_stmt_bind_result($stmt,$col1,$col2,$col3);
//bind variables that will be assigned the value of the fields
while(mysqli_stmt_fetch($stmt))
{//get the values in question
    echo 'col1: '.$col1.'<br/>col2: '.$col2.'<br/>col3: '.$col3.'<br/>';
}

要获得结果集的关联数组,您将不得不阅读文档,那里有几个您可能想要使用的函数。

于 2012-10-05T12:46:42.243 回答