Jump to content

PHP Beginner- Simple Form Problem


MattC

Recommended Posts

Hello,
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:

[b]insert.php[/b]

[code]

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

[b]test.php[/b]

[code]
<?php


    //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>";

        mysql_close();
        }

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

?>
[/code]

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.
Link to comment
Share on other sites

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 = $[b][!--coloro:red--][span style=\"color:red\"][!--/coloro--]_[!--colorc--][/span][!--/colorc--][/b]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')";
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.