Page 1 of 1
Function Help
Posted: Thu Jul 31, 2003 7:22 pm
by php_wiz_kid
I'm not sure how to make this function work:
Code: Select all
<?php
function update_db ($table, $col, $var, $new_var, $id) {
$mysql_query = "UPDATE $table SET $col = '$new_var' WHERE userid = '$userid'";
$mysql_query_result = mysql_query($mysql_query);
$var = $new_var;
}
update_db($users, 'email', $email, $new_email, $id);
echo $email;
?>
Now, I'm kind of stupid so bear with me.
When I
echo() $email I want it to
echo() whatever is stored in the $var variable in the function.
I put the ampersand sign in front of $var and that just gave me an error.
Posted: Thu Jul 31, 2003 9:39 pm
by DuFF
I don't understand why the current code isn't working. To call that function you need to have some type of info stored in the variables ($users, 'email', $email, $new_email, $id). Therefore you must have passed in information through some type of form, right?
Why can't you just echo the $email variable, because it should be storing the information that it is passing into the database.
Do I not understand your question correctly?
Re: Function Help
Posted: Thu Jul 31, 2003 9:46 pm
by McGruff
Code: Select all
<?php
function update_db ($table, $col, $new_var, $id) // $var arg removed - not needed in the fn
{
$mysql = "UPDATE $table SET $col='$new_var' WHERE userid='$id'"; // you pass $id as an arg not $userid
mysql_query($mysql);
}
update_db($users, 'email', $new_email, $id);
echo $new_email;
?>
Posted: Fri Aug 01, 2003 2:16 pm
by php_wiz_kid
Here's some more code. Maybe this can explain things a little better
Code: Select all
<?php
$new_email = $_POST['email']; //Get the email value from the form
$new_fname = $_POST['fname']; //Get the fname value from form
//--Get e-mail address and fname from the database--//
$mysql_query = "SELECT * FROM users WHERE userid="$userid"";
$mysql_query_result = mysql_query($mysql_query);
$mysql_fetch_array = mysql_fetch_array($mysql_query_result);
$email = $mysql_fetch_array[email];
$fname = $mysql_fetch_array[fname];
//-----------------------------------------------//
function update_db ($table, $col, $var, $new_var, $id) {
$mysql_query = "UPDATE $table SET $col = '$new_var' WHERE userid = '$userid'";
$mysql_query_result = mysql_query($mysql_query);
$var = $new_var;
}
//--------Error check e-mail and fname--------//
if($error_email == true) {
error_message("*E-MAIL ERROR*"); // Ignore this, just a function of mine
elseif($error_email == false) {
update_db('usrs', 'col_name', $email, $new_email, $id);
}
if($error_fname == true) {
error_message("*FNAME ERROR*");
elseif($error_fname == false) {
update_db('users', 'col_name', $fname, $new_fname, $id);
}
//-----------------------------------------------//
echo '<form name="profile" method="post" action="profile.php">' .
"<input type="textfield" name="email" value="$email" class="css-textfield" size="31" maxlength="200">" .
"<input type="textfield" name="fname" value="$fname" class="css-textfield" size="31" maxlength="200">" .
'<input type="submit" name="submit" value="Edit Profile" class="css-submit">' .
'</form>';
?>
This is to explain why I pass the $var arg. I have to have a seperate variable for the value displayed ($email), and the value being updated ($new_email). I'd ultimatley like to be able to change the $email variable to the $new_email variable inside the function. It's less work. Everything else works fine except:
$var = $new_var
inside the function. I can't get $email to echo $var's contents. Can I even do that?
Re: Function Help
Posted: Fri Aug 01, 2003 2:32 pm
by nielsene
Try this:
chages:
1. Add '&' before $var in the function header
2. change either $id->$userid in header OR
$userid->$id in the query
Code: Select all
<?php
function update_db ($table, $col, &$var, $new_var, $id) {
$mysql_query = "UPDATE $table SET $col = '$new_var' WHERE userid = '$d'";
$mysql_query_result = mysql_query($mysql_query);
$var = $new_var;
}
?>
Posted: Fri Aug 01, 2003 2:36 pm
by php_wiz_kid
I tryed the &$var, but I just got an error.
Posted: Fri Aug 01, 2003 6:58 pm
by nielsene
what error? what version of php?