The [man]mysqli_error/man function expects a 'MySQLi' object, which is the object you get when you create a connection to the MySQL server.

In your code above, it would appear that this resource is stored in the variable named $dbc.

    awebb88;10957498 wrote:

    Here's the full code:

    // Retrieve the user's information:
    $q = "SELECT first_name, last_name, email FROM users WHERE user_id=$id";
    if (!$r) {
        # error handling. error_log(), display some kind of error message to user, exit?
        error_log(mysqli_error($dbc));
    	echo '<p class="error">You are unable to edit your user\'s profile at this stage.</p>';
    }
    

    It seems to me you've removed actually calling the db: there's a line of code missing between $q= and if (!$r).
    Once you put mysqli_query back in between those lines, mysqli_error($dbc) would tell you what went wrong, if anything did.

      Aah I see, my bad!

      That's definately helped and shown what the error is:

      You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

      So something to do with one of the queries? I'll have a look through it now and see what I can come up with.

        You probably have an empty variable or something of that nature.

        Echo out the query string itself to visually inspect it. If you still can't spot the error, post the query for us to see as well.

          Hello...

          Are you sure the $id is being set? I'm having a hard time seeing where you're ever getting that data from.

          I see that $id is being set when you come from view_users.php, or a form submission happens.
          But I don't see where you set the $id for testing. Meaning... if you're just sitting there refreshing edit_user.php over and over again for the book's sake... I don't see where $id is set (i think it's not set) and thus the mysqli query fails (returning the FALSE boolean)

          To test my crazy theory... try adding the following right before any of your queries... example:

          // Retrieve the user's information: 
          echo ' user id is equal to: '.$id; exit();
          $q = "SELECT first_name, last_name, email FROM users WHERE user_id=$id";
          

          if it's not that query, try the other one you mention.

          Or I'm crazy... but worth a shot. =O)

            Sorry for the slight delay fellas - been majorly busy with work.

            I'll dig the code up later and give it a bash and post what happens. 🙂

              Looks like you guys are right, there is nothing being assigned to $id, when I put your code in Jeremy it comes up with:

              user id is equal to:

              Because at the start of the code it says accessed through view_users.php but looking at it I'm not sure I can actually see where it fetches the page from? Does it need to be included/required at some point?

                awebb88;10958282 wrote:

                Looks like you guys are right, there is nothing being assigned to $id, when I put your code in Jeremy it comes up with:

                Because at the start of the code it says accessed through view_users.php but looking at it I'm not sure I can actually see where it fetches the page from? Does it need to be included/required at some point?

                Two things to do:
                1) Hard code a value into your page for testing.
                Right above the line

                if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) 

                add $_GET['id] = 1; //use an id of a record that already exists in the db

                So your code will look like:

                $_GET['id] = 1; //use an id of a record that already exists in the db
                if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) 
                

                The above test will tell u if the script is running.

                2)

                You wrote: Because at the start of the code it says accessed through view_users.php but looking at it I'm not sure I can actually see where it fetches the page from? Does it need to be included/required at some point?

                view_users.php will be passing an id to your delete_user.php by using either a post or a get. In other words, view_users.php contains a form that will post to delete_users.php OR it contains a link that is to delete_users.php?id=1 (1 being what ever the id is of the record you wish to delete.)

                Jeff

                  Hi Jeff,

                  I put your line of code above the if (isset...) line but still only get what I was getting before ("user id is equal to: ").

                  Is there any other code I have to replace/is the variable just not set or retrieving the info from view_users.php?

                    awebb88;10958396 wrote:

                    Hi Jeff,

                    I put your line of code above the if (isset...) line but still only get what I was getting before ("user id is equal to: ").

                    Is there any other code I have to replace/is the variable just not set or retrieving the info from view_users.php?

                    how is your view_users.php passing the id to delete_user.php? Is it doing it by post? or by get?

                    Make sure the id is being passed. Do you have any users in your users table?

                    Jeff

                      I'm using the same book, aweb88, and I'm getting the same error.

                      I tweaked my scripts a little bit, but nothing that changes the way the book sends $id. Double-checking to see if a value is set for it, I just used

                      echo "$id";
                      
                      if (isset($_POST['submitted'])){
                      
                      
                      if ($_POST['sure'] == 'Yes') {
                      	$q = "DELETE FROM users WHERE user_id=$id LIMIT 1";
                      	$r = @mysqli_query($dbc, $q);
                      

                      Which returned "1" on my page. (The user I tested it with had the user_id of 1.)

                      This is the entire script we're using with the includes:

                      ('../mysqli_connect.php')

                      <?php
                      
                      DEFINE ('DB_USER', 'xx');
                      DEFINE ('DB_PASSWORD','xx');
                      DEFINE ('DB_HOST', 'xx');
                      DEFINE ('DB_NAME', 'xx');
                      
                      $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error());
                      
                      ?>

                      ('view_users.php')

                      <?php
                      $page_title = 'View the Current Users';
                      include ('includes/header.html');
                      echo '<h1>Registered Users</h1>';
                      require_once ('../mysqli_connect.php');
                      $q = "SELECT user_id, last_name, first_name, email, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM users ORDER BY registration_date ASC";
                      $r = @mysqli_query ($dbc, $q);
                      $num = mysqli_num_rows($r);
                      
                      
                      if ($num > 0) {
                      	echo "<p>There are currently $num registered users.</p>\n";
                      	echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
                      	<tr>
                      	<td align="left"><b>Edit</b></td>
                      	<td align="left"><b>Delete</b></td>
                      	<td align="left"><b>User ID</b></td>
                      	<td align="left"><b>Last Name</b></td>
                      	<td align="left"><b>First Name</b></td>
                      	<td align="left"><b>Email</b></td>
                      	<td align="left"><b>Date Registered</b></td>
                      	</tr>';
                      
                      while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
                      	echo ' 
                      	<tr>
                      	<td align="left"><a href="edit_user.php?id='.$row['user_id'] . '">Edit</a></td>
                      	<td align="left"><a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a></td>
                      	<td align="left">' . $row['user_id'] . '</td>
                      	<td align="left">' . $row['last_name'] . '</td>
                      	<td align="left">' . $row['first_name'] . '</td>
                      	<td align="left">' . $row['email'] . '</td>
                      	<td align="left">' . $row['dr'] . '</td>
                      	</tr>';
                      
                      }
                      echo '</table>';
                      mysqli_free_Result ($r);
                      } else {
                      	echo '<p class="error">There are currently no registered users.</p>';
                      	echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>';
                      }
                      mysqli_close($dbc);
                      include ('includes/footer.html');
                      ?>
                      

                      ('delete_user.php')

                      <?php
                      
                      
                      $page_title = 'Delete a User';
                      include ('includes/header.html');
                      echo '<h1>Delete a User</h1>';
                      
                      
                      if ( (isset($_GET['id'])) && (is_numeric($_GET['id']))){
                      	$id = $_GET['id'];
                      } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id']))){
                      	$id = $_POST['id'];
                      } else {
                      	echo '<p class="error">This page has been accessed in error.</p>';
                      	include ('includes/footer.html');
                      	exit();
                      }
                      
                      
                      require_once ('../mysqli_connect.php');
                      
                      
                      if (isset($_POST['submitted'])){
                      
                      
                      if ($_POST['sure'] == 'Yes') {
                      	$q = "DELETE FROM users WHERE user_id=$id LIMIT 1";
                      	$r = @mysqli_query($dbc, $q);
                      
                      
                      	if (mysqli_affected_rows($r) == 1) {
                      	echo '<p>The user has been deleted.</p>';
                      } else {
                      	echo '<p class="error">The user could not be deleted due to be a system error.</p>';
                      	echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>';
                      }
                      } else {
                      	echo '<p>The user has NOT been deleted.</p>';
                      } 
                      } else {
                      $q = "SELECT user_id, last_name, first_name, email FROM users WHERE user_id=$id";
                      $r = @mysqli_query ($dbc, $q);
                      $num = mysqli_num_rows($r);
                      $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
                      	echo '<form action="delete_user.php" method="post">
                      	<h3>User ID: ' . $row['user_id'] . '</h3>
                      	<h3>Name: ' . $row['last_name'] . ', ' . $row['first_name'] . '</h3>
                      	<h3>Email: ' . $row['email'] . '</h3>
                      	<p>Are you sure you want to delete this user?<br />
                      	<input type="radio" name="sure" value="Yes" />Yes
                      	<input type="radio" name="no" value="No" />No</p>
                      	<p><input type="submit" name="submit" value="Submit"></p>
                      	<input type="hidden" name="submitted" value="TRUE" />
                      	<input type="hidden" name="id" value="' . $id . '" />
                      	</form>';
                      }
                      
                      
                      mysqli_close($dbc);
                      include ('includes/footer.html');
                      
                      
                      ?>
                      

                        I just logged into phpMyAdmin to see if I could run the command straight from the SQL, but right before doing so I went ahead with checking the users table. Come to find out, the script is actually deleting the users, but it's giving the error as if it's not?

                        So something would have gone terribly wrong here, no?:

                        if (mysqli_affected_rows($r) == 1) {
                        		echo '<p>The user has been deleted.</p>';
                        		} else {
                        			echo '<p class="error">The user could not be deleted due to be a system error.</p>';
                        			echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>';
                        		}
                        	} else {
                        		echo '<p>The user has NOT been deleted.</p>';
                        	} 

                          @: In regards to your posts not appearing, here's some relevant text from the redirection screen after you post a reply on PHPBuilder:

                          Thank you for posting!

                          YOUR POST MAY NOT APPEAR IMMEDIATELY!

                          Please do not re-enter your post, it will appear.

                          Also, why are you using the '@' error suppressor in front of the mysqli_*() functions? You should never use this error suppressor, since it's hiding any error messages PHP might be generating (which is never good, considering you shouldn't have to worry about this on production servers on which display_errors should be disabled and log_errors enabled instead).

                            It was a different message.

                            Something along the lines of needing to wait for approval, but it only happened when trying to post that particular post, not the others.

                            Shrug, just as aweb88, I'm new to this, and so far this book has been my only source of information prior to googling the error and coming to this forum.

                            This is what the book says in reference to using the @ sign in the mysql connection script:

                            The function call is preceded by the error suppression operator (@). This prevents the PHP error from being displayed in the Web browser. This is preferable, as the error will be handled by the OR die() clause.

                            Aside from that, reasoning behind the usage of @ in later scripts isn't specified otherwise.

                              So anyhow, now we know (assuming his/her script is acting as mine is) that whether or not the script successfully deletes the record isn't the issue, but why it's showing this error still.

                              It's showing Deleted rows: 1 which I would assume would be picked up by

                              if (mysqli_affected_rows($r) == 1) {
                                      echo '<p>The user has been deleted.</p>';
                                      } else {
                                          echo '<p class="error">The user could not be deleted due to be a system error.</p>';
                                          echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>';
                                      }
                                  } else {
                                      echo '<p>The user has NOT been deleted.</p>';
                                  } 

                              but apparently it's not? I can't see why.

                                Although it's not good not knowing for the script it's nice to know it's not just me with the book!

                                And like slywins said, I was using the @ operator as was said in the book, this has been my main foundation of learning PHP so I'm not going to go against what an expert in the field says when I know little, but with more experiences I will feel comfortable to make my own choices and exclude it.

                                  awebb88 wrote:

                                  And like slywins said, I was using the @ operator as was said in the book, this has been my main foundation of learning PHP so I'm not going to go against what an expert in the field says when I know little

                                  Who ever said you had to be an expert to write a book? 😉 Besides, if that's their reasoning for using the @ suppressor, I'd have to question their "expert"-ness in managing PHP in a production environment. :p

                                    bradgrafelman;10959139 wrote:

                                    Who ever said you had to be an expert to write a book? 😉 Besides, if that's their reasoning for using the @ suppressor, I'd have to question their "expert"-ness in managing PHP in a production environment. :p

                                    Haha touche, and very true. Being a newbie to PHP I'm just going with the flow trying to learn as much as possible. You do seem like you know what your talking about though, so if you bring a book out let me know!

                                    But if you know of any books/material where I could gain more knowledge it would be appreciated.

                                      Hmm, no obvious solution to the error in the script eh?

                                        Write a Reply...