Click to See Complete Forum and Search --> : Getting variables from HTML forms


juicemousezero
09-09-2005, 01:32 PM
I'm trying to make a little PHP guestbook but for some reason I can't get my insert statement to work... I'm guessing that I'm just leaving some importent step out somewhere. This is the code for the page:


<html>

<?
//form isn't submitted yet
if (!isset($_POST['submit']))
{
?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

Name: <input type="text" size="40" name="name"><br>
Website: <input type="text" size="38" name="website"><br>
<br>
Comments:<br>
<TEXTAREA NAME="comments" ROWS=6 COLS=40></TEXTAREA><br>
<br>
<input type="Submit" value="Submit">
</form>

<?php
//executes when the form is submitted
}
else
{

$username="name";
$password="password";
$database="db";

//get info from text boxes
$name = empty($_POST['name']) ? die ("Enter a name") : mysql_escape_string($_POST['name']);

$website = empty($_POST['website']);

$comments = empty($_POST['comments']) ? die ("enter some comments") : mysql_escape_string($_POST['comments']);

//connect and insert data
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to connect to $database!!!");
$query = "INSERT INTO guestbook (name, website, comments) VALUES ('$name', '$website', '$comments')";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

echo "New record inserted with ID ".mysql_insert_id();

mysql_close();
}
?>

</html>


Everything checks out so I'm not getting any errors, but the info simply isn't inserted. Can anyone spot anything? Any help would be appreciated. Thanks. :)

HaganeNoKokoro
09-09-2005, 02:03 PM
$website = empty($_POST['website']); This is the only thing I can spot, although it shouldn't stop a row from being inserted. Shouldn't it be$website = empty($_POST['website']) ? die ("Enter a website ") : mysql_escape_string($_POST['website']); or, if the website is non-manditory, $website = mysql_escape_string($_POST['website']);

NogDog
09-09-2005, 02:28 PM
<input type="Submit" value="Submit" name="submit">

HaganeNoKokoro
09-09-2005, 02:33 PM
Ooh, didn't even see that one! Well done, Nog!

juicemousezero
09-09-2005, 04:14 PM
Huh.. Yeah I knew must have been leaving out some tiny step in getting the stuff from the form to the PHP code, but I didn't know what. Thanks a ton guys. :D