This is my first time on these forums, but I think I can ask for coding help. I'm going through a book to learn php but I keep getting this error...
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /var/www/html/manage.php
It referes me to line 66, 68, and 34, 36.
For some reason it can still detect duplicate entries giving me the statement
Duplicate entry 'email i eneterd' for key 2
But this is not the right output, should be
You are already subscribed!
If anyone even gives this a shot I appreciate it very much, I'm thinking you can read through this code very quickly with no troubles like me hahaha.
This is the source code for manage.php (The main page)
Code:
1 <?php
2 ini_set('display_errors', 1);
3 include("commonfunc.php");
4 //determine if they need to see the form or no
5 if (!$_POST){
6 //They need to see the form, so create the form block
7 $display_block = "
8 <form method=\"POST\" action =\"".$_SERVER["PHP_SELF"]."\">
9
10 <p><strong>Your e-mail address:</strong><br/>
11 <input type=\"text\" name=\"email\" size=\"40\">
12
13 <p><strong>Action:</strong><br/>
14 <input type=\"radio\" name=\"action\" value=\"sub\" checked> subscribe
15 <input type=\"radio\" name=\"action\" value=\"unsub\"> unsubcribe
16
17 <p><input type=\"submit\" name=\"submit\" value=\"Submit Form\"></p>
18 </form>";
19 }
20 else if (($_POST) && ($_POST["action"] == "sub")) {
21 //trying to subcribe; validate email
22 if ($_POST["email"] == ""){
23 header("Location: manage.php");
24 exit;
25 }
26 else{
27 //connect to database
28 doDB('mailinglist');
29
30 //check that email is in list
31 emailchecker($_POST["email"]);
32
33 //get number of results and do action
34 if (mysqli_num_rows($check_res) < 1) {
35 //free result
36 mysqli_free_result($check_res);
37
38 //add record
39 $add_sql = "INSERT INTO subscribers (email) VALUES ('".$_POST["email"]."')";
40 $add_res = mysqli_query($mysqli,$add_sql) or die (mysqli_error($mysqli));
41 $display_block = "<p>Thanks for subscribing!</p>";
42 echo"help me ".$check_res." Is it global?";
43 //close connection to MySQL
44 mysqli_close($mysqli);
45 }
46 else{
47 //print failure message
48 $display_block = "<p>You're already subscribed!</p>";
49 }
50 }
51 }
52 else if (($_POST) && ($_POST["action"] == "unsub")){
53 //trying to unsubscribe; validate email address
54 if ($_POST["email"]==""){
55 header("Location: manage.php");
56 exit;
57 }
58 else{
59 //connect to database
60 doDB('mailinglist');
61
62 //Check that emila is in list
63 emailchecker($_POST["email"]);
64
65 //get number of results and do action
66 if (mysqli_num_rows($check_res) < 1){
67 //free result
68 mysqli_free_result($check_res);
69
70 //print failure message
71 $display_block = "<p>Couldn't find your address!</p><p>No action was taken.</p>";
72 }
73 else{
74 //get value of ID from result
75 while ($row = mysqli_fetch_array($check_res)){
76 $id = $row["id"];
77 }
78
79 //unsubscribe the address
80 $del_sql = "DELETE FROM subscribers WHERE id = '".$id."'";
81 $del_res = mysqli_query($mysqli, $del_sql) or die(mysqli_error($mysqli));
82 $display_block = "<p>You're unsubscribed</p>";
83 }
84 mysqli_close($mysqli);
85 }
86 }
87 ?>
88 <html>
89 <head>
90 <title>Subscribe/Unsubscribe to a Mailing List</title>
91 </head>
92 <body>
93 <h1>Subscribe/Unsubscribe to a Mailing List</h1>
94 <?php echo "$display_block"; ?>
95 </body>
96 </html
And this is my commonfunc.php file, which is imported on the third line of manage.php
Code:
<?php
function doDB($database) {
global $mysqli;
//connect to server and select database;
$mysqli = mysqli_connect("localhost", "root", "*******", $database);
//if connection fails, stop script execution
if (mysqli_connect_errno()){
printf("Connect failed: %s \n", mysqli_connect_error());
exit();
}
}
function emailchecker($email){
global $mysqli, $check_res;
//Check that email is not already in list
$check_sql = "SELECT id FROM subscribers WHERE email = '".$email."'";
$chek_res = mysqli_query($mysqli, $check_sql) or die (mysqli_error($mysqli));
}
?>