/    Sign up×
Community /Pin to ProfileBookmark

Inserting values from MySQL into PHP

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>[/code]

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.

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@bokehJan 25.2006 — [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]
Copy linkTweet thisAlerts:
@invisionauthorJan 25.2006 — Still no luck. I've tried the above, and although it is stored in the database as 'M' , it doesn't have 'Male' radio button selected when I go to the edit_profile.php page...

Here's the current 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&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>';
[/CODE]
Copy linkTweet thisAlerts:
@bokehJan 25.2006 — See what [I]$row['sex'][/I] contains. No spaces etc.[code=php]echo'"'.$row['sex'].'"';[/code]
Copy linkTweet thisAlerts:
@bokehJan 25.2006 — By the way since you are using numeric keys for all the other values it would be helpful to see the query you are using to access the data and the one used to store it. Post the query and the surrounding lines.
Copy linkTweet thisAlerts:
@invisionauthorJan 25.2006 — <i>
</i>&lt;?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 '&lt;p&gt;&lt;font color="red" size="+1"&gt;Please enter a valid nick name(only letters).&lt;/font&gt;&lt;/p&gt;';
}

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

if ($nick &amp;&amp; $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 '&lt;H2&gt;Edit your Profile&lt;/H2&gt;
<i> </i> &lt;P&gt;Your profile has been edited&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;';

// Finish the page
echo '&lt;H3&gt;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.&lt;/h3&gt;';
include ('./includes/footer.html'); // Include the HTML footer.
exit();

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



} // 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 '&lt;h3&gt;Edit a User&lt;/h3&gt;
<i> </i>&lt;form action="edit_profile.php" method="post"&gt;
<i> </i>&lt;p&gt;&lt;b&gt;Nick Name:&lt;/b&gt; &lt;input type="text" name="nickname" size="15" maxlength="15" value="' . $row[0] . '" /&gt;&lt;/p&gt;
<i> </i>&lt;input type="radio" name="sex" value="m"'.(('m' == @$row['sex'])?' checked="checked"':'').' /&gt;male&lt;br&gt;
<i> </i>&lt;input type="radio" name="sex" value="f"'.(('f' == @$row['sex'])?' checked="checked"':'').' /&gt;female&lt;br&gt;
<i> </i>&lt;p&gt;&lt;b&gt;Age:&lt;/b&gt; &lt;input type="text" name="age" size="3" maxlength="3" value="' . $row[2] . '" /&gt;&lt;/p&gt;
<i> </i>&lt;p&gt;&lt;b&gt;Interests:&lt;/b&gt; &lt;textarea rows="5" cols="40" name="interest"&gt;' . $row[3] . '&lt;/textarea&gt;&lt;small&gt;(optional)&lt;/small&gt;&lt;/p&gt;
<i> </i>&lt;p&gt;&lt;b&gt;Country:&lt;/b&gt;
<i> </i>&lt;select name="country" size="1"&gt;
<i> </i>&lt;option&gt;Afghanistan&lt;/option&gt;&lt;option&gt;Albania&lt;/option&gt;&lt;option&gt;Algeria&lt;/option&gt;&lt;option&gt;American Samoa&lt;/option&gt;&lt;option&gt;&lt;option&gt;Zimbabwe&lt;/option&gt;&lt;/select&gt;&lt;/p&gt;

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

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

mysql_close(); // Close connection with db.

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


Here's all the code.

Thanks for taking a look.


It inserts all the other content :

Name, Age, Interests but doesn't do Sex, Country or MailList.
Copy linkTweet thisAlerts:
@bokehJan 25.2006 — Change this:[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.
Copy linkTweet thisAlerts:
@invisionauthorJan 25.2006 — array(6) { [0]=> string(9) "McGarrity" [1]=> string(1) "M" [2]=> string(2) "21" [3]=> string(35) "Football and Music and the Computer" [4]=> string(14) "United Kingdom" [5]=> string(1) "N" }
Copy linkTweet thisAlerts:
@bokehJan 25.2006 — Ok! try this:[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&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.
?>[/code]
Also you should read up on register globals and security when inserting data into mysql.
Copy linkTweet thisAlerts:
@invisionauthorJan 25.2006 — The Sex Works !

Thanks bokeh. What changes did you make to the script ?

I can't see any.

You've been a massive help ?
Copy linkTweet thisAlerts:
@bokehJan 25.2006 — Changed from [I]$row['sex'][/I] to [I]$row[1][/I] because you are using numeric keys... and I changed the [I]M[/I] and [I]F[/I] to upper case as that is what the database is returning in your example.

Also you will want to make the form sticky so if they fill it in wrong it will auto fill on reload with the user data not the data you pulled from mysql.
Copy linkTweet thisAlerts:
@invisionauthorJan 25.2006 — If I could I'd fly over to Spain and kiss ya ?

Heh, thanks again mate ?

Typical, most of my scripts aren't great, but tend to fail on simple parts.
×

Success!

Help @invision spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...