problem with code about adding 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
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

problem with code about adding to database

Post by kedora19 »

i need some help with my code, for some reason it's not working. I'm not the greatest at php so if i'm doing it completely wrong just tell me nicely

anyways here's my code

Code: Select all

 
<?php
session_start();
if (!isset($_SESSION['user']))
{
 die ("Access Denied");
}
 
?>
<?php
                include '../login/dbc.php';
  
                //first open connection to your database
  
                $id = mysql_real_escape_string($_GET['id']); //catch the id number from url, store it and escape slashes from possible attack.
                $takeuser = mysql_query("SELECT * FROM users WHERE id='$id' LIMIT 1"); //Right now script check id number from url against the database
                if (mysql_num_rows($takeuser) < 1) { //checks if we have that id number in our database
                    echo "User doesn't exist!"; //inform the user that we found 0 result
                    } else { //or if we found some
                while ($row=mysql_fetch_array($takeuser)) { //Taking the result set
                    $sql="INSERT INTO friends (username, friendname)
                    VALUES
                    ('$_SESSION[user]',' . $row['id'] . '";
                    } //closing the loop
                    } //closing the "if" statement
  
 
                    mysql_close($link); //and then we close connection
?>
 
the problem i'm having is the insert part. i'm trying to put it in the friends table. as you can see it takes the id from the url and puts it to the database. then i have it write to the database the the friendname and your name for later recovery.

please help me with this, ANY help would be nice

Kedora19
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: problem with code about adding to database

Post by it2051229 »

that's because you did not "execute" your insert sql statement.. you only stored it in a variable...
mdk999
Forum Newbie
Posts: 22
Joined: Fri May 08, 2009 3:21 pm

Re: problem with code about adding to database

Post by mdk999 »

try thi s..

Code: Select all

 
$sql="INSERT INTO `friends` (`username`, `friendname`) VALUES ('{$_SESSION[user]}','{$row['id']}'";
mysql_query($sql);
 
Last edited by Benjamin on Fri May 08, 2009 7:07 pm, edited 1 time in total.
Reason: Changed code type from text to php.
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Re: problem with code about adding to database

Post by kedora19 »

mdk999 wrote:try thi s..

Code: Select all

 
$sql="INSERT INTO `friends` (`username`, `friendname`) VALUES ('{$_SESSION[user]}','{$row['id']}'";
mysql_query($sql);
 
so is this what you were thinking

Code: Select all

 
<?php
session_start();
if (!isset($_SESSION['user']))
{
 die ("Access Denied");
}
 
?>
<?php
                include '../login/dbc.php';
  
                //first open connection to your database
  
                $id = mysql_real_escape_string($_GET['id']); //catch the id number from url, store it and escape slashes from possible attack.
                $takeuser = mysql_query("SELECT * FROM users WHERE id='$id' LIMIT 1"); //Right now script check id number from url against the database
                if (mysql_num_rows($takeuser) < 1) { //checks if we have that id number in our database
                    echo "User doesn't exist!"; //inform the user that we found 0 result
                    } else { //or if we found some
                while ($row=mysql_fetch_array($takeuser)) { //Taking the result set
                    $sql="INSERT INTO `friends` (`username`, `friendname`) VALUES ('{$_SESSION[user]}','{$row['id']}'";
                    mysql_query($sql);
                    } //closing the loop
                    } //closing the "if" statement
  
 
                    mysql_close($link); //and then we close connection
?>
 
cause that doesn't work, if that wasn't what you were thinking then what were you
Last edited by Benjamin on Fri May 08, 2009 9:30 pm, edited 1 time in total.
Reason: Changed code type from text to php.
mdk999
Forum Newbie
Posts: 22
Joined: Fri May 08, 2009 3:21 pm

Re: problem with code about adding to database

Post by mdk999 »

Sorry I missed a closing paren..

Code: Select all

 
$sql="INSERT INTO `friends` (`username`, `friendname`) VALUES ('{$_SESSION[user]}','{$row['id']}')";
mysql_query($sql);
 
 
Last edited by Benjamin on Fri May 08, 2009 7:24 pm, edited 1 time in total.
Reason: Changed code type from text to php.
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Re: problem with code about adding to database

Post by kedora19 »

mdk999 wrote:Sorry I missed a closing paren..

Code: Select all

 
$sql="INSERT INTO `friends` (`username`, `friendname`) VALUES ('{$_SESSION[user]}','{$row['id']}')";
mysql_query($sql);
 
 
thanks it worked... it's funny to see how such a little problem can make such a big mess
mdk999
Forum Newbie
Posts: 22
Joined: Fri May 08, 2009 3:21 pm

Re: problem with code about adding to database

Post by mdk999 »

welcome to the world of specifics. Happy coding ;)
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Re: problem with code about adding to database

Post by kedora19 »

mdk999 wrote:welcome to the world of specifics. Happy coding ;)
thanks
Post Reply