1

I am creating an HTML form that will take user input, such as first name, and use a PHP file to then post to the MySQL database. HTML code is as follows:

<!DOCTYPE HTML>
<html>
<body>
<form action="contactform.php" method="POST">
<p>First Name: <input type="text" name="F_Name" /></p>
<input type="submit" value="Submit" />
</form>
</body>
</html>

The contactform.php looks like:

<?php
$DB_NAME = "mydbname";
$DB_USER = "mydbuser";
$DB_PASSWORD = "mydbpw";
$DB_HOST = "localhost";

$link = new mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME);

if ($link->connect_error) {
    die('Could not connect: ' . $link->connect_error);
}

$F_Name = $_POST['F_Name'];

$sql = "INSERT INTO myTableName ('F_Name') VALUES ('$F_Name')";

if ($link->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $link->error;
}

$link->close();
?>

After clicking submit (from the HTML code) I receive a 404 error. Any advice on why this may be and how to fix it?

Thanks for your assistance!

supreme
  • 11
  • 1
  • 404 means the page isn't there. Nothing to do with the code in `contactform.php`, the file just isn't there. – David Apr 13 '17 at 20:02
  • are you sure the contactform.php file path is correct –  Apr 13 '17 at 20:02
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 13 '17 at 20:04
  • You are also using incorrect quotes in your query so when the PHp does execute it will generate an error – John Conde Apr 13 '17 at 20:04
  • In queries quotes are for strings; backticks are for columns/tables/databases. – chris85 Apr 13 '17 at 20:05
  • contactform.php is not in the same directory as your form file – Jay Blanchard Apr 13 '17 at 20:06

0 Answers0