Problem inserting form values into mysql
| From: | Marnix Ottens | Date: | Mon, 06 Aug 2001 06:06:32 +0000 |
| Subject: | Problem inserting form values into mysql | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-8672@lists.php.net to get a copy of this message | ||
Hi,
I've made a script where I want the results of an order form to be inserted
into a MySQL database. Connecting to the database goed fine (tested it) but
my check if the values are inserted into the database gives the error
message I created "Problem, the information cannot be recorded". I hope
someone can spot the error in the script.
Please see the script below, I faked server address and user/pass and stuff.
Here a customer enters his name and address, after already selecting to buy
a new car (BMW). This is just an example.
<html>
<head>
<title>Form input & processor</title>
</head>
<body>
<?
if(!IsSet($stage))
{
?>
<p>Please fill in the form below</p>
<form method="post" action="<? print("$PHP_SELF"); ?>">
<table><tr>
<tr><td>Last name:</td> <td><input type="text"
name="lname"
size="30"></td></tr>
<tr><td>First name: <td><input type="text" name="fname"
size="20"></td></tr>
<tr><td>Street: <td><input type="text" name="street"
size="50"></td></tr>
<tr><td>Number: <td><input type="int" name="number"
size="4"></td></tr>
<tr><td>Car: <td><input type="text" name="car"
size="20"
value="BMW"></td></tr>
<input type="hidden" name="stage" value="1">
<tr><td><input type="submit"></td></tr>
</form>
<?
}
else
{
/* connecting to database */
mysql_connect("address.myserver.com", "myusername", "mypassword") or
die("Problem, can't connect to database");
mysql_select_db("databasename");
/* insert values to database */
$query = "INSERT INTO tablename (lname, fname, street, number, car) VALUES
('$lname', '$fname' '$street', '$number',
'$car')";
$result = mysql_query($query);
if($result == 0)
print("Problem, the information cannot be recorded");
else
print("You have succesfully placed an order for a car");
}
?>
</body>
</html>