Click to See Complete Forum and Search --> : Errors when submitting simple form UPDATE query


invision
01-25-2006, 03:01 AM
Hello,

I'm having some slight problems with the following script. This will allow any user registered on my web site to edit their own 'profile', but I'm getting problems on submission.
It displays all the correct information in the form itself, but if I make no changes at all and click submit, I get the following error :

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/students/projects/p002/public_html/edit_profile.php on line 36

No changes have been made.

It also removes the content from the TEXTAREA 'Interests' field.

However, if I do make changes to the form, they are saved, but I still get the same error as above.

Any ideas ?


<?php # this file allows a user to edit a users details.

// This page edits a user.

$page_title = "Edit your own Profile";
include ('./includes/header.html');

require_once ('../mysql_connect.php'); // Connect to the db.

$id = $_SESSION['user_id'];

// Checks if the form has been submitted.
if (isset($_POST['submitted'])) {

if (eregi ('^[[:alpha:]\.\' \-]{2,15}$', stripslashes(trim($_POST['nickname'])))) {
$nick = escape_data($_POST['nickname']);
} else {
$nick = FALSE;
echo '<p><font color="red" size="+1">Please enter a valid nick name(only letters).</font></p>';
}

// Check for age between 1 and 120.
if (eregi ('[0-9]', stripslashes(trim($_POST['age'])))) {
$a = escape_data($_POST['age']);
} else {
$a = FALSE;
echo '<p><font color="red" size="+1">Please enter a valid age.</font></p>';
}

if ($nick && $age) { // If all of these variables(everything) are A-OK.

// Make the Query to edit the user using UPDATE

$query = "UPDATE profiles SET nickname='$nick', sex='$sex', age='$age', interests='$interest', country='$country', maillist='$maillist' WHERE user_id=$id";
$result = @mysql_query ($query); // Run the Query
if (mysql_affected_rows($result) == 1) { // If it ran OK and one row was affected.

// Print a message
echo '<H2>Edit a User</H2>
<P>The user has been edited</p><p><br /><br /></p>';

// Finish the page
echo '<H3>Thank you for adding your own profile to Pictures of Scotland! To edit this profile or delete your own profile, simply return to the Edit Profile page.</h3>';
include ('./includes/footer.html'); // Include the HTML footer.
exit();

} else { // Error 1
echo '<p><font color="red" size="+1">An Error has occurred</font></p>';
}
} else { // RE Error
echo '<p><font color="red" size="+1">Please make sure you have used only letters or numbers in the appropriate fields.</font></p>';
}



} // End of the main Submit conditional.

// Always show the form.

// Retrieve the user's information.

$query = "SELECT nickname, sex, age, interests, country, maillist FROM profiles WHERE user_id=$id";
$result = mysql_query($query); // Run the Query.

if (mysql_num_rows($result) == 1) { // Valid user ID, show the form.

// Get the user's information.
$row = mysql_fetch_array($result, MYSQL_NUM);

// Create the form.
echo '<h3>Edit a User</h3>
<form action="edit_profile.php" method="post">
<p><b>Nick Name:</b> <input type="text" name="nickname" size="15" maxlength="15" value="' . $row[0] . '" /></p>
<p><b>Sex:</b> <input type="radio" name="sex" value="male" checked />(male)<input type="radio" name="sex" value="female" />(female)</p>
<p><b>Age:</b> <input type="text" name="age" size="3" maxlength="3" value="' . $row[2] . '" /></p>
<p><b>Interests:</b> <textarea rows="3" cols="60" name="interest">' . $row[3] . '</textarea><small>(optional)</small></p>
<p><b>Country:</b>
<select name="country" size="1">
<option>Afghanistan</option><option>Albania</option><option>Algeria</option><option>Zimbabwe</option></select></p>

<p><b>Subscribe:</b><input type="radio" name="maillist" value="Y" />Yes&nbsp;&nbsp;<input type="radio" name="maillist" value="N" checked/>No</p>
<input type="submit" name="submit" value="Edit Profile" />
<input type="hidden" name="submitted" value="TRUE" />
</form>';

} else { // Not a valid User ID.
echo '<h2>Page Error</h2>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}

mysql_close(); // Close connection with db.

include ('./includes/footer.html'); // Include the HTML footer.
?>

NogDog
01-25-2006, 03:32 AM
The argument to mysql_affected_rows() is the connection ID returned by mysql_connect(), not the result ID returned by the query. If you are not opening up multiple connections, you can just leave the arg out completely. Note that if your update does not actually change any values, you may get a result of 0 rows even thought the query "worked".

invision
01-25-2006, 03:49 AM
It's pretty odd now.
It makes the changes to the 'profile' but still highlights an error.

No matter what I do, when I click 'Edit Profile' , I get :

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/students/projects/p002/public_html/edit_profile.php on line 36

An Error has occurred

Please help me out here.

NogDog
01-25-2006, 09:14 AM
It's pretty odd now.
It makes the changes to the 'profile' but still highlights an error.

No matter what I do, when I click 'Edit Profile' , I get :

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/students/projects/p002/public_html/edit_profile.php on line 36

An Error has occurred

Please help me out here.
Can you show us the edited source code?

invision
01-25-2006, 10:57 AM
<?php # this file allows a user to edit a users details.

// This page edits a user.

$page_title = "Edit your own Profile";
include ('./includes/header.html');

require_once ('../mysql_connect.php'); // Connect to the db.

$id = $_SESSION['user_id'];

// Checks if the form has been submitted.
if (isset($_POST['submitted'])) {

if (eregi ('^[[:alpha:]\.\' \-]{2,15}$', stripslashes(trim($_POST['nickname'])))) {
$nick = escape_data($_POST['nickname']);
} else {
$nick = FALSE;
echo '<p><font color="red" size="+1">Please enter a valid nick name(only letters).</font></p>';
}

// Check for age between 1 and 120.
if (eregi ('[0-9]', stripslashes(trim($_POST['age'])))) {
$age = escape_data($_POST['age']);
} else {
$age = FALSE;
echo '<p><font color="red" size="+1">Please enter a valid age.</font></p>';
}

if ($nick && $age) { // If all of these variables(everything) are A-OK.

// Make the Query to edit the user using UPDATE

$query = "UPDATE profiles SET nickname='$nick', sex='$sex', age='$age', interests='$interest', country='$country', maillist='$maillist' WHERE user_id=$id";
$result = @mysql_query ($query); // Run the Query
if (mysql_affected_rows($result) == 1) { // If it ran OK and one row was affected.

// Print a message
echo '<H2>Edit your Profile</H2>
<P>Your profile has been edited</p><p><br /><br /></p>';

// Finish the page
echo '<H3>Thank you for adding your own profile to Pictures of Scotland! To edit this profile or delete your own profile, simply return to the Edit Profile page.</h3>';
include ('./includes/footer.html'); // Include the HTML footer.
exit();

} else { // Error 1
echo '<p><font color="red" size="+1">An Error has occurred</font></p>';
}
} else { // RE Error
echo '<p><font color="red" size="+1">Please make sure you have used only letters or numbers in the appropriate fields.</font></p>';
}



} // End of the main Submit conditional.

// Always show the form.

// Retrieve the user's information.

$query = "SELECT nickname, sex, age, interests, country, maillist FROM profiles WHERE user_id=$id";
$result = mysql_query($query); // Run the Query.

if (mysql_num_rows($result) == 1) { // Valid user ID, show the form.

// Get the user's information.
$row = mysql_fetch_array($result, MYSQL_NUM);

// Create the form.
echo '<h3>Edit a User</h3>
<form action="edit_profile.php" method="post">
<p><b>Nick Name:</b> <input type="text" name="nickname" size="15" maxlength="15" value="' . $row[0] . '" /></p>
<input type="radio" name="sex" value="m"'.(('m' == @$row['sex'])?' checked="checked"':'').' />male<br>
<input type="radio" name="sex" value="f"'.(('f' == @$row['sex'])?' checked="checked"':'').' />female<br>
<p><b>Age:</b> <input type="text" name="age" size="3" maxlength="3" value="' . $row[2] . '" /></p>
<p><b>Interests:</b> <textarea rows="5" cols="40" name="interest">' . $row[3] . '</textarea><small>(optional)</small></p>
<p><b>Country:</b>
<select name="country" size="1">
<option>Afghanistan</option><option>Zimbabwe</option></select></p>

<p><b>Subscribe:</b><input type="radio" name="maillist" value="Y" />Yes&nbsp;&nbsp;<input type="radio" name="maillist" value="N" />No</p>
<input type="submit" name="submit" value="Edit Profile" />
<input type="hidden" name="submitted" value="TRUE" />
</form>';

} else { // Not a valid User ID.
echo '<h2>Page Error</h2>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}

mysql_close(); // Close connection with db.

include ('./includes/footer.html'); // Include the HTML footer.
?>

NogDog
01-25-2006, 11:10 AM
You still have not changed the arg to mysql_affected_rows:

if (mysql_affected_rows($result) == 1)

It needs to be one of the following:

if (mysql_affected_rows() == 1)
// or
if (mysql_affected_rows($xxxxx) == 1) // where $xxxxx is the value returned by the
// mysql_connect() in your include function (so probably just leave it blank)

invision
01-25-2006, 01:18 PM
Jackanackanorray!

It works, many thanks for your time and patience NogDog, very impressed!