Can't insert form data into mysql database
There are two files, index.htm(form) and submit.php(code to insert form data into a database's table).
The database is created manually using HeidiSQL with appropriate table and columns.
But the problem is that after filling out the fields and clicking the submit button an empty screen appears and no data was inserted into the table of a database.
How fix?
index.htm code
Code:
<html>
<head>
<title>Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<form name="form1" action="submit.php" action="post">
<table>
<tr>
<th class="center" colspan="2">Form</th>
</tr>
<tr>
<td class="right">Field1</td><td><input type="text" name="field1" /></td>
</tr>
<tr>
<td class="right">Field2</td><td><input type="text" name="field2" /></td>
</tr>
<tr>
<td class="center" colspan="2"><input type="submit" value="Add" /></td>
</tr>
</table>
</form>
</body>
</html>
submit.php code
Code:
<?php
//Connect to mysql
$connect = mysql_connect("localhost","root","");
//If unable to connect, give me an explanation
if (!$connect)
{
die("Couldn't connect: " . mysql_error());
}
//If all is jolly, select the database using the current connection
mysql_select_db("db", $connect);
//Insert data into the table using input from the form page's fields
$sql = "INSERT INTO items (field1,field2) VALUES ('$_POST[field1]','$_POST[field2]')";
//If all fails, give me an explanation
if (!mysql_query($sql, $connect)
{
die('Error: ' . mysql_error());
}
//Otherwise congratulate me
echo "1 record added";
//Close connection
mysql_close($connect);
?>