Problem
I'm trying to connect a user input form in a MySQL database using PHP. I'm using MAMP to connect to the database locally. When I run the code, nothing is displayed. The code below is saved as IndexEventInputForm.php
in a folder called EventInputForm. In the url, I have http://localhost:8888/EventInputForm/IndexEventInputForm.php. Inspecting the element I get:
Code
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<?php
//===============
// MySQL Settings
//===============
define('DB_NAME', 'EventInputForm');
define('DB_USER', 'root');
define('DB_PASSWORD', '12345678');
define('DB_HOST', 'localhost');
//====================
// Database connection
//====================
//Connect to server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
//Test if connection to database works
if(!$link) {
die('Could not connect to : ' . mysql_error());
}
//Connect to database
$db_selected = mysql_select_db(DB_NAME, $link);
//Test if connection to selected database works
if(!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
echo 'Connected successfully';
/*
if($conn->connect_error){
die("Failed");
}
$conn.insertInto
echo "Success";
*/
//Send input form data to MySQL database
$Title = $_POST['Title'];
$sql = "INSERT INTO EventInfo (Title) VALUES ('$Title')";
//Checks if the field exists
if(!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
?>
<body>
<?php
echo $name;
?>
<form action="/EventInputForm/IndexEventInputForm.php" method="POST">
<input type="text" name="Title"/>
<input type="submit" value="indsend">
</form>
</body>
</html>