Menu
Hello,
Just another small problem.
I’m storing the Sex(M,F) , the Country and if the users are on a Mailing List(Y,N) in my ‘profiles’ table of the database.
I was wondering, I know that I can keep the current value in the database for , say, a text area by typing :
[code]<p><b>Interests:</b> <textarea rows=”3″ cols=”60″ name=”interest”>’ . $row[3] . ‘</textarea><small>(optional)</small></p>
But, I’m unsure how to use this with Radio Buttons or Dropdown Menus. So that it will automatically ‘default’ to the chosen sex, country and mailing list.
Any ideas would be greatly appreciated.
[code=php]<?php
$row['sex'] = 'm'; // used to emulate your database output
echo
'<form action="" method="POST">'."n".
'<input type="radio" name="sex" value="m"'.(('m' == @$row['sex'])?' checked="checked"':'').' />male<br>'."n".
'<input type="radio" name="sex" value="f"'.(('f' == @$row['sex'])?' checked="checked"':'').' />female<br>'."n".
'<input type="submit" name="submit" value="submit">'."n".
'</form>';
?>[/code]
[CODE]
<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>Zimbabwe</option></select></p>
<p><b>Subscribe:</b><input type="radio" name="maillist" value="Y" />Yes <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>';
[/CODE]
[code=php]echo'"'.$row['sex'].'"';[/code]
<i>
</i><?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.
<i> </i>// Make the Query to edit the user using UPDATE
<i> </i>$query = "UPDATE profiles SET nickname='$nick', sex='$sex', age='$age', interests='$interest', country='$country', maillist='$maillist' WHERE user_id=$id";
<i> </i>$result = @mysql_query ($query); // Run the Query
<i> </i>if (mysql_affected_rows($result) == 1) { // If it ran OK and one row was affected.
<i> </i> // Print a message
<i> </i> echo '<H2>Edit your Profile</H2>
<i> </i> <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.
<i> </i>// Get the user's information.
<i> </i>$row = mysql_fetch_array($result, MYSQL_NUM);
<i> </i>// Create the form.
<i> </i>echo '<h3>Edit a User</h3>
<i> </i><form action="edit_profile.php" method="post">
<i> </i><p><b>Nick Name:</b> <input type="text" name="nickname" size="15" maxlength="15" value="' . $row[0] . '" /></p>
<i> </i><input type="radio" name="sex" value="m"'.(('m' == @$row['sex'])?' checked="checked"':'').' />male<br>
<i> </i><input type="radio" name="sex" value="f"'.(('f' == @$row['sex'])?' checked="checked"':'').' />female<br>
<i> </i><p><b>Age:</b> <input type="text" name="age" size="3" maxlength="3" value="' . $row[2] . '" /></p>
<i> </i><p><b>Interests:</b> <textarea rows="5" cols="40" name="interest">' . $row[3] . '</textarea><small>(optional)</small></p>
<i> </i><p><b>Country:</b>
<i> </i><select name="country" size="1">
<i> </i><option>Afghanistan</option><option>Albania</option><option>Algeria</option><option>American Samoa</option><option><option>Zimbabwe</option></select></p>
<i> </i><p><b>Subscribe:</b><input type="radio" name="maillist" value="Y" />Yes&nbsp;&nbsp;<input type="radio" name="maillist" value="N" />No</p>
<i> </i><input type="submit" name="submit" value="Edit Profile" />
<i> </i><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.
?>
[code=php]$row = mysql_fetch_array($result, MYSQL_NUM);[/code]
to this for testing:[code=php]$row = mysql_fetch_array($result, MYSQL_NUM);
var_dump($row);
exit;[/code]
Then post the resulting output on here.[code=php]<?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[1])?' checked="checked"':'').' />male<br>
<input type="radio" name="sex" value="F"'.(('F' == $row[1])?' 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>Albania</option><option>Algeria</option><option>American Samoa</option><option><option>Zimbabwe</option></select></p>
<p><b>Subscribe:</b><input type="radio" name="maillist" value="Y" />Yes <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.
?>[/code]
Also you should read up on register globals and security when inserting data into mysql.0.1.9 — BETA 5.23