Jump to content

mysql_num_rows() error with Update form


ntroycondo

Recommended Posts

Trying to get this to work but I know I'm missing something here. It's a form that is meant to update a record in DB.

I am getting errore reference to first if (mysql_num_rows($result) == 1) in code and error message: Error has occurred.

 

 

$connection = mysql_connect("xxxxxxxx","xxxxxx","xxxxxx");
if (!$connection) {
  die("Error connecting to database " . mysql_error());
}
if (isset($_POST['submitted']))

$name = $_POST['name'];
$email = $_POST['email'];
$PHOTOGRAPHERID = $_POST['PHOTOGRAPHERID'];

$query = "SELECT * FROM Photographers WHERE PHOTOGRAPHERID = $PHOTOGRAPHERID";
	$result = mysql_query($query);
	if (mysql_num_rows($result) == 1){

		// Make the query.
		$query = "UPDATE Photographers SET name='$name',email='$email' WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID";
		$result = @mysql_query ($query); // Run the query.
		if (mysql_affected_rows() == 1) { // If it ran OK.

			header("Location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/"."search.php");


		} else { // If it did not run OK.
			echo '<h1 id="mainhead">System Error</h1>
			<p class="error">The name could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message.
			echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message.
			exit();
		}

	} else { 
		echo '<h1 id="mainhead">Error!</h1>
		<p class="error">Error has occurred.</p>';

	}


$query = "SELECT name, email FROM Photographer WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID";		
$result = @mysql_query ($query); // Run the query.

// Create the form.
echo '<h2>Edit a User</h2>
<form action="update.php" method="post">
<p>Name: <input type="text" name="name" size="60" maxlength="60" value="' . $row[0] . '" /></p>
<p>Email: <input type="text" name="email" size="60" maxlength="60" value="' . $row[1] . '" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="PHOTOGRAPHERID" value="' . $id . '" />
</form>';

Link to comment
https://forums.phpfreaks.com/topic/212698-mysql_num_rows-error-with-update-form/
Share on other sites

I've changed code around a little but still not working. But I've also been working at it way too long to.

 

Now I get a error from the second ELSE statement:

There has been an error in updating the database: 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

 

<?php

$connection = mysql_connect("xxxxxxxxxxxxx","xxxxxxxxxxxx", "xxxxxxxxxxxx");
if (!$connection) 
  die("Error connecting to database " . mysql_error());

mysql_select_db('Photographers');


if (isset($_POST['submitted'])) 
{

$name = $_POST['name'];
$email = $_POST['email'];
$PHOTOGRAPHERID = $_POST['PHOTOGRAPHERID'];

}

$query = "UPDATE Photographers SET name='$name',email='$email' WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID";

$result = mysql_query($query);

if($result){

echo "<p>The user has been updated.</p>";

}else{

echo "<p>There has been an error in updating the database:  ".mysql_error()."</p>";

}	

$query = "SELECT name, email FROM Photographers WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID";		
$result = @mysql_query ($query); // Run the query.

// Create the form.
echo '<h2>Edit a User</h2>
<form action="update.php" method="post">
<p>Name: <input type="text" name="name" size="60" maxlength="60" value="' . $row[0] . '" /></p>
<p>Email: <input type="text" name="email" size="60" maxlength="60" value="' . $row[1] . '" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="PHOTOGRAPHERID" value="' . $id . '" />
</form>';

?> 

If you change the the line with the query execution as below, you can see what the query string holds, and figure out which variable isn't getting set properly.

 

$query = "UPDATE Photographers SET name='$name',email='$email' WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID";
$result = mysql_query($query) or die( '<br />Query string: ' . $query . '<br />Produced error: ' . mysql_error() . '<br />');

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.