Jump to content


PHP Beginner- Simple Form Problem

  • Please log in to reply
4 replies to this topic

#1 MattC

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 May 2006 - 03:21 AM

I've been dealing with website for quite a while, but only now am getting into PHP coding. I copied some code from Jason Gilmore's book about inserting data into a mySQL database. Basically, I have two files:


<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Product ID: <input type="text" name="productid" size="8" maxlength="8" value="" />
Name: <input type="text" name="name" size="25" maxlength="25" value="" />
Price: <input type="text" name="price" size="6" maxlength="6" value="" />
Description: <textarea name="description" rows="5" cols="30"></textarea>
<input type="submit" name="submit" value="Submit!" />



    //If submit button has been pressed
    if (isset($POST['submit']))

         //Connect to the server and select the database
        $linkID = @mysql_connect("localhost","username","password")
            or die("Could not connect to my SQL server");
        @mysql_select_db("sqltesting") or die("Could not connect to database");

        //Retrieve the posted product information.
        $productid = $_POST['productid'];
        $name = $_POST['name'];
        $price = $_POST['price'];
        $description = $POST['description'];

        //Insert the info into the prodict table
        $query = "INSERT INTO product SET productid='$productid', name='$name', price='$price', description='$description'";

        $result = mysql_query($query);

        //Display the appropriate message
        if ($result) echo "<p>Product info has been entered.</p>";
        else echo "<p>You screwed up.</p>";


//Include the insertion form
include "insert.php";


I bring up test.php in the browser, fill it out and hit Submit. This brings me right back to the form with no data entered in it at all. Even if I setup the wrong database information, I get no error messages about connecting. Am I going about this the wrong way? I've checked the code over many times and can't find anything wrong with it.

Thank you.

#2 mb81

  • Members
  • PipPipPip
  • Advanced Member
  • 120 posts

Posted 15 May 2006 - 03:27 AM

Check your isset() function, it says $POST, not $_POST;

#3 .josh

  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 15 May 2006 - 03:29 AM

if you are trying to get the form info from insert.php to test.php you need to change your from action to 'test.php' instead of $PHP_SELF

also you for got the underscore in $POST should be $_POST here (in addition to the one mentioned above):

$description = $[!--coloro:red--][span style=\"color:red\"][!--/coloro--]_[!--colorc--][/span][!--/colorc--]POST['description'];

also this:

$query = "INSERT INTO product SET productid='$productid', name='$name', price='$price', description='$description'";

should be this:

$query = "INSERT INTO product (productid, name, price, description) VALUES '$productid','$name', '$price','$description')";

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 MattC

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 May 2006 - 08:26 PM

Okay, the $_Post problem proved to be it. I need to be more thorough next time. Thanks for the help.

#5 ryanlwh

  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 15 May 2006 - 08:42 PM

Crayon, the INSERT INTO () VALUES () and INSERT INTO SET both work. the second is a variant of the INSERT query which most people do not know of. If you're building a query for UPDATE and INSERT, using hte second variant provides a benefit because you can declare a string for the SET part to be used twice.
Please use EDIT * 100...
Please use
or [php] * 1000...


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users