Jump to content

Need Help With Annoying Error: mysqli_fetch_array()


$Three3

Recommended Posts

Hey, I have a script that will update a users information through a html form and then update it in the database. Everything is working perfectly except for an error I keep getting which is:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/three/public_html/edit_user.php on line 60

 

The update of the database still works, but this error keeps showing up. I have googled it and tried everything I could think of but I have had no luck yet. Here is my php code:

 

<?php

//Set the page title
$page_title = 'Update User' ;
//Include the header files
include ('includes/header.html') ;

//Validate that id's are correct
// Check for a valid user ID, through GET or POST:
if ((isset($_GET['id'])) && (is_numeric($_GET['id']))) { // From view_users.php
$id = $_GET['id'] ;
} elseif ((isset($_POST['id'])) && (is_numeric($_POST['id']))) { // Form submission.
$id = $_POST['id'] ;
} else { // No valid ID, kill the script.
echo '<p class="error">This page has been accessed in error.</p>';
include ('includes/footer.html'); 
exit();
}

//Connect to the database
require_once ('../mysqli_connect.php') ;

//Check to see if the form has been submitted
if (isset($_POST['submitted'])) {

//Intialize errors array
$errors = array() ;

//Validate that all form input
if (empty($_POST['first_name'])) {
	$errors[] = '<p class="error">You must enter a first name.</p>' ;
} else {
	$fn = mysqli_real_escape_string($dbc, trim($_POST['first_name'])) ;
}
if (empty($_POST['last_name'])) {
	$errors[] = '<p class="error">You must enter a last name.</p>' ;
} else {
	$ln = mysqli_real_escape_string($dbc, trim($_POST['last_name'])) ;
}
if (empty($_POST['email'])) {
	$errors[] = '<p class="error">You must enter an email address.</p>' ;
} else {
	$email = mysqli_real_escape_string($dbc, trim($_POST['email'])) ;
}

//Check to see if there were any errors
if (empty($errors)) {

	//Create the query
	$query = "SELECT user_id FROM users WHERE email = '$email' AND user_id != $id" ;
	$run = mysqli_query($dbc, $query) ;
	if (mysqli_num_rows($run) == 0) {
		//Query was succesfull
		//Update the user with the newest values
		$query = "UPDATE users SET first_name = '$fn' , last_name = '$ln', email = '$email' WHERE user_id = $id LIMIT 1" ;
		$run = mysqli_query($dbc, $query) ;
		if (mysqli_affected_rows($dbc) == 1) {
			//Query was succesful
			//Print out success message
			$fetch = mysqli_fetch_array($run, MYSQLI_ASSOC) ;
			echo "<p>Your update was succesful on the user:<b> " . $fetch['first_name'] . " " . $fetch['last_name'] . "</b>.</p>" ;
			include ('includes/footer.html') ;
			mysqli_close($dbc) ;
			exit();
		} else {
			echo '<p class="error">There was a system error. Please try again.</p>' ;
		}
	} else {
		echo '<p class="error">We are sorry, but the email <b>' . $fetch['email'] . '</b> already exists in our database. Please choose a different email.</p>' ;
	}
} else {
	//Print out the error messages
	foreach ($errors as $message) {
		echo '<p class="error">The following errors have occurred:<br /></p>' ;
		echo "- $message" ;
	}
}
}

//Always show the form
$q = "SELECT first_name, last_name, email FROM users WHERE user_id=$id" ;
$r = mysqli_query($dbc, $q) ;
if (mysqli_num_rows($r) == 1) {
$row = mysqli_fetch_array($r, MYSQLI_NUM) ;
//Display the form
echo 
'<form action="edit_user.php" method="post">
<p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="' . $row[0] . '"></p>
<p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="' . $row[1] . '"></p>
<p>Email Address: <input type="text" name="email" size="20" maxlength="40" value="' . $row[2] . '"> </p>
<p><input type="submit" name="submit" value="Update"></p>
<input type="hidden" name="submitted" value="TRUE">
<input type="hidden" name="id" value="' . $id . '">
</form> ' ;
} else {
echo '<p>You have accessed this page in error(2).</p>' ; //Not a valid user
}

include ('includes/footer.html') ;
mysqli_close($dbc) ;

?>

 

AND HERE IS LINES 49-64 WHERE THE ERROR IS MOST LIKELY OCCURRING:

 

//Create the query
	$query = "SELECT user_id FROM users WHERE email = '$email' AND user_id != $id" ;
	$run = mysqli_query($dbc, $query) ;
	if (mysqli_num_rows($run) == 0) {
		//Query was succesfull
		//Update the user with the newest values
		$query = "UPDATE users SET first_name = '$fn' , last_name = '$ln', email = '$email' WHERE user_id = $id LIMIT 1" ;
		$run = mysqli_query($dbc, $query) ;
		if (mysqli_affected_rows($dbc) == 1) {
			//Query was succesful
			//Print out success message
			$fetch = mysqli_fetch_array($run, MYSQLI_ASSOC ;
			echo "<p>Your update was succesful on the user:<b> " . $fetch['first_name'] . " " . $fetch['last_name'] . "</b>.</p>" ;
			include ('includes/footer.html') ;
			mysqli_close($dbc) ;
			exit();

 

Thanks in advance for the help.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.