need help with inserting data through form!!!

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

need help with inserting data through form!!!

Post 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
Last edited by Benjamin on Thu May 07, 2009 9:18 pm, edited 1 time in total.
Reason: Changed code type from text to html, php.
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

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

Post 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.
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

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

Post 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
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

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

Post 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.
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

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

Post 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
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

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

Post 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.
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

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

Post 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
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

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

Post 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)
Post Reply