Add variables to database!

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
snarkiest
Forum Commoner
Posts: 30
Joined: Mon May 04, 2009 10:06 am
Location: Latvia
Contact:

Add variables to database!

Post 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)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Add variables to database!

Post 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.
snarkiest
Forum Commoner
Posts: 30
Joined: Mon May 04, 2009 10:06 am
Location: Latvia
Contact:

Re: Add variables to database!

Post by snarkiest »

Thank you. Works now! (:
Post Reply