Page 1 of 1

need help with inserting data through form!!!

Posted: Thu May 07, 2009 6:34 pm
by kedora19
okay i have a form which has a message box, then i have a submit button. When you submit the form it goes to insert.php to know what to do with the info.

here's part of the form page code

Code: Select all

 
<div id="content">
        <form action="insert.php" method="post">
            <textarea id="message" name="message" cols="30" rows="3"></textarea><br />
            <input type="submit" name="add_message" value="Send" class="submit" />
        </form>
    </div>
 
for obviously reasons i didn't put my whole page code

here's the insert.php code

Code: Select all

 
<?php
session_start();
if (!isset($_SESSION['user']))
{
 die ("Access Denied");
}
 
?> 
<?php
$con = mysql_connect("localhost","(myusername)","(mypassword)");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("kiwirawk_cs", $con);
 
$sql="INSERT INTO Persons (username, message)
VALUES
('$_SESSION['user']','$_POST[message]')";
 
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
 
mysql_close($con)
?>
 
the thing is it looks like it would take the session username AND the message and insert it into the database. but all it puts in is the message, not the username. I need the username in there as well or my idea won't work.

if anyone can give me ANY help, please

Thanks in advance, Kedora19

Re: need help with inserting data through form!!!

Posted: Thu May 07, 2009 6:39 pm
by ldougherty
The fact that its inserting anything means your syntax is OK otherwise the query wouldn't work at all.

Try outputting the value of $_SESSION['user'] above the INSERT query so you can see what it technically should be writing.

Re: need help with inserting data through form!!!

Posted: Thu May 07, 2009 6:52 pm
by kedora19
ldougherty wrote:The fact that its inserting anything means your syntax is OK otherwise the query wouldn't work at all.

Try outputting the value of $_SESSION['user'] above the INSERT query so you can see what it technically should be writing.
WHOA!!! it doesn't show anything, so thats why it isn't working. the weird thing is that the page that the form is on the output shows the person who is logged in but as soon as i go to the insert page it doesn't work

Re: need help with inserting data through form!!!

Posted: Thu May 07, 2009 7:02 pm
by ldougherty
Thats the trick to troubleshooting your code, just output variables in different spots to see how everything is working and 99% of the time you'll be able to figure out why things aren't working.

Re: need help with inserting data through form!!!

Posted: Thu May 07, 2009 7:03 pm
by kedora19
ldougherty wrote:Thats the trick to troubleshooting your code, just output variables in different spots to see how everything is working and 99% of the time you'll be able to figure out why things aren't working.
ya the thing is how could i fix this problem

Re: need help with inserting data through form!!!

Posted: Thu May 07, 2009 7:08 pm
by ldougherty
You'll need to determine where this value is disapearing...

Try to see if the session itself exists..

If it does exist then output all the session variables that are currently held.

If it does not exist figure out where the session is being destroyed.

Re: need help with inserting data through form!!!

Posted: Thu May 07, 2009 7:10 pm
by kedora19
ldougherty wrote:You'll need to determine where this value is disapearing...

Try to see if the session itself exists..

If it does exist then output all the session variables that are currently held.

If it does not exist figure out where the session is being destroyed.
k, thanks... ALOT!!! it really helped me.
when i get a chance i'll try doing various procedures and figure out what wrong

Re: need help with inserting data through form!!!

Posted: Thu May 07, 2009 7:23 pm
by kedora19
well i got it to work, now how would i get the info from the database thats only the user (that you want it to be).

Kedora19

(if i have a ask this in another topic just tell me, i'll make a new topic)