Page 1 of 1

problem with code about adding to database

Posted: Fri May 08, 2009 6:44 pm
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

Re: problem with code about adding to database

Posted: Fri May 08, 2009 6:59 pm
by it2051229
that's because you did not "execute" your insert sql statement.. you only stored it in a variable...

Re: problem with code about adding to database

Posted: Fri May 08, 2009 7:00 pm
by mdk999
try thi s..

Code: Select all

 
$sql="INSERT INTO `friends` (`username`, `friendname`) VALUES ('{$_SESSION[user]}','{$row['id']}'";
mysql_query($sql);
 

Re: problem with code about adding to database

Posted: Fri May 08, 2009 7:17 pm
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

Re: problem with code about adding to database

Posted: Fri May 08, 2009 7:21 pm
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);
 
 

Re: problem with code about adding to database

Posted: Fri May 08, 2009 7:25 pm
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

Re: problem with code about adding to database

Posted: Fri May 08, 2009 7:28 pm
by mdk999
welcome to the world of specifics. Happy coding ;)

Re: problem with code about adding to database

Posted: Fri May 08, 2009 9:05 pm
by kedora19
mdk999 wrote:welcome to the world of specifics. Happy coding ;)
thanks