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!
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.
<?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
$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?