I’m getting confused with a SIMPLE php function that supposed to work BUT I simply couldn’t figure out what went wrong…
function add_point($table_field, $point_value, $receiver) {
global $q_add_point;
$q_add_point = "UPDATE points SET '$table_field' = ".$point_value." WHERE username = ".$receiver."";
if (mysql_query($q_add_point)) {
return true;
}
else { return false; }
echo $table_field."<br />".$point_value."<br />".$receiver; // just to see the variables
} // end function new point
if (add_point('contact', 15, $asker_username)) {
echo "<h1>ACTIVITY POINT UPDATED!</h1>";
} // end add point OK
else {
echo "ERRROR : ".mysql_error();
echo $q_add_point;
}
Why are the passed variables ($table_field, $point_value AND $receiver) ALL EMPTY??? I tried to echo them both in function and then outside the function (making them global) but they are all empty… :mad:
Plz point me to my mistake, I can’t see even a syntax error anymore…
And how do you know? It might seem a dumb question, but you should see those variables echoed (at least if you put that echo statement at the beginning), so checking some dumb things could be a good start
I tested your code with the echo at the top of the function, and this is the result:
contact
15
ERRROR : Can't connect to local MySQL server through socket '/...' (2)UPDATE points SET 'contact' = 15 WHERE username =
So the variables aren’t empty. Only the last one, because you send a variable there and in the code you posted you don’t give it a value anywhere.
Everything works just fine as far as I’m concerned.
Beats me. As I posted just before you, it works fine for me.
Are you sure there isn’t some other call in your code to that function where you use all variables instead of fixed values, and those variables don’t have a value?
That confused me even MORE!!!
The third variable I didn’t posted in the script here but it HAS a value.
How can the script ‘work’ at your side??? Maybe it’s an Apache issue?
When your function assigns the sql string to $q_add_point you are expecting that updated value to be available from outside of the function. That isn’t working as you expect.
I suggest that you update the function so that instead of returning true, it returns the string instead. Then you can use
if (q_add_point = add_point(..., ..., ...)) {
...
}
function add_point($table_field, $point_value, $receiver) {
$q_add_point = "UPDATE points SET '$table_field' = ".$point_value." WHERE username = ".$receiver."";
if (mysql_query($q_add_point)) {
echo 'TRUE: ' . $table_field."<br />".$point_value."<br />".$receiver; // just to see the variables
} else {
echo 'FALSE: ' . $table_field."<br />".$point_value."<br />".$receiver; // just to see the variables
return $q_add_point; //so it can be echoed......
}
} // end function new point
$asker_username = 'Spike';
if (add_point('contact', 15, $asker_username)) {
echo "<h1>ACTIVITY POINT UPDATED!</h1>";
} // end add point OK
else {
echo "ERRROR : ".mysql_error();
echo $q_add_point;
}