I am trying to create a small form for people to come to my website and simply enter the following information:

  • Name
  • Surname
  • Email Address

[ATTACH]4681[/ATTACH]

It should send this information back into my table from my database.

...Now here is where things get "complex" (...well it is to me, okay? )... :queasy:

If the email address does not exist in our table, then it must echo "Email Address Added. Thank you!". Otherwise if it does, then it must echo "Email address already exists!"

For some reason it is not doing this. It is spitting out this error:
Fatal error: Call to a member function fetch_assoc() on a non-object in C:\wamp\www\addemail.php on line 46

I have attached my php code (below):

<!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>
form.JPG

    Are you sure this is the file causing the error because I don't see any attempts to call fetch_assoc any where in the code. Heck I don't even see the word fetch in there (and neither does ctrl+f). Please check to make sure this is the right file, and if it is, that its the same version that's throwing the error.

      Derokorian;11014535 wrote:

      Are you sure this is the file causing the error because I don't see any attempts to call fetch_assoc any where in the code. Heck I don't even see the word fetch in there (and neither does ctrl+f). Please check to make sure this is the right file, and if it is, that its the same version that's throwing the error.

      Where do I add this fetch_assoc code? Total newbie to php.

        Sorry this is the actual error:

        Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\addemail.php on line 41

        Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\wamp\www\addemail.php on line 43

          You can't mix mysqli functions with mysql functions. They each reside in two mutually exclusive plugins. Stick with the [man]MySQLi[/man] functions.

          Also, I personally would get rid of your SELECT statement altogether and instead add a UNIQUE (or perhaps even PRIMARY?) KEY on the email_address column since that seems to be the desired constraint.

          Finally, note that user-supplied data should never be placed directly into a SQL query string, else your code will be vulnerable to SQL injection attacks and/or just plain SQL errors. Instead, you must first sanitize the data (e.g. via [man]mysqli_real_escape_string/man for string data) or use prepared statements. See this manual page for more info (and/or search Google for "SQL injection" to find a lot more): [man]security.database.sql-injection[/man].

            Write a Reply...