Page 1 of 1

Add variables to database!

Posted: Tue Jun 02, 2009 4:06 pm
by snarkiest

Code: Select all

 
if(isset($_POST['doaddach'])) {
      $username = 'snarkiest';
      
      $user_id = mysql_query("SELECT id FROM ach_users WHERE username='$username' ",$connect);
      $achievement_id = mysql_escape_string($_POST['addach']);
      $result = mysql_query("
                INSERT ach_achievements (`user_id`, `achievement_id`)
                VALUES ('$user_id','$achievement_id') 
      ",$connect);
      echo $topview_ach;
      echo "<table width='949' class='toutborder' cellspacing='2' cellpadding='2'><tr width='100%'><td class='tinborder' align='center' width='10%'>Message: Achievement added.</td></tr></table>";
      echo $bottom;
}
So I have a problem with running this.
If I click doaddach, it inserts in database record where achievement_id = mysql_escape_string($_POST['addach']); until now everything is correct, but when it has to insert value $user_id it don't insert the correct value, it just inserts 0. But it should insert number 1 because "snarkiest" id is number 1.

Why so? When in database sql window I run this:
SELECT id FROM ach_users WHERE username='snarkiest'
It gives out the correct number. (In this case number 1)

Re: Add variables to database!

Posted: Tue Jun 02, 2009 4:24 pm
by requinix
mysql_query returns a resource. You have to use another function like mysql_fetch_array to get the data out of it.

Look at the second example on the page for mysql_query.

Re: Add variables to database!

Posted: Tue Jun 02, 2009 4:49 pm
by snarkiest
Thank you. Works now! (: