Hi, I’m new to PHP so please bear with me. I’m trying to send user input from a HTML form to a MySQL database. The database has one table with four fields (varchar(15), varchar(15), varchar(10), longtext). The HTML form looks like:
Every time I run the above I get the following error:
Parse error: syntax error, unexpected T_STRING, expecting ']' in /…../…../…/…/…/…../html/Z.php on line 12
Nothing is added to the database. How can I rectify this? I would be very grateful for all help.
Code:
<html> <body>
<form name="index" action="Z.php" method="get">
Type your first name:
<input type="text" name="1stname"><br><br>
Type your last name:
<input type="text" name="2ndname"><br><br>
Select type of car:
<select name="kindofcar">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select><br><br>
Write your comments here:
<TEXTAREA NAME="comments" COLS=40 ROWS=6></TEXTAREA><br><br>
<input type="submit" value="Submit">
</form>
</body> </html>
Z.php forms looks like:
<html> <body>
<?php
$con = mysql_connect("mydatabase","username","password");
if(!$con)
{ die('Could not connect: ' . mysql_error());
}
mysql_select_db("nameofdb", $con);
$sql="INSERT INTO NameofTable(FirstName, LastName, TypeOfCar,
Comments)
VALUES('$_POST[1stname]','$_POST[2ndname]','$_POST[kindofcar]',
'$_POST[comments]')";
if(!mysql_query($sql,$con))
{ die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
</body> </html>
Every time I run the above I get the following error:
Parse error: syntax error, unexpected T_STRING, expecting ']' in /…../…../…/…/…/…../html/Z.php on line 12
Nothing is added to the database. How can I rectify this? I would be very grateful for all help.