MattC Posted May 15, 2006 Share Posted May 15, 2006 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 forminclude "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. Quote Link to comment Share on other sites More sharing options...
mb81 Posted May 15, 2006 Share Posted May 15, 2006 Check your isset() function, it says $POST, not $_POST; Quote Link to comment Share on other sites More sharing options...
.josh Posted May 15, 2006 Share Posted May 15, 2006 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_SELFalso 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')"; Quote Link to comment Share on other sites More sharing options...
MattC Posted May 15, 2006 Author Share Posted May 15, 2006 Okay, the $_Post problem proved to be it. I need to be more thorough next time. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted May 15, 2006 Share Posted May 15, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.