Function Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Function Help

Post 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.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: Function Help

Post 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;
?>
Last edited by McGruff on Thu Aug 11, 2005 12:02 am, edited 1 time in total.
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post 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?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: Function Help

Post 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;
     }


?>
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

I tryed the &$var, but I just got an error.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

what error? what version of php?
Post Reply