Search code examples
phpmysqldatabasemamp

Nothing displayed with PHP post request


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:

enter image description here

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>

Solution

  • It can be better this way.

       <?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 = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);    
    
          // Test if connection succeeded
        if(mysqli_connect_errno()) {
        die("Database connection failed: " . 
             mysqli_connect_error() . 
             " (" . mysqli_connect_errno() . ")"
        );
      }
    
            //Send input form data to MySQL database 
    if(isset($_POST['submit'])){
        $Title = $_POST['Title'];
    
        $sql = "INSERT INTO EventInfo (Title) VALUES ('{$Title}')";    
    
           $result = mysqli_query($link, $sql);
         if ($result) {
          // Success
          echo "Title has been created!";
        } else {
          // Failure
          echo "Failed";
        }
        }
    
        ?>
     <!DOCTYPE html>
        <html lang="en">
    
        <head>
    
        </head>
    
        <body>
    
            <form action="IndexEventInputForm.php" method="POST">
                <input type="text" name="Title"/>
    
                <input type="submit" name="submit" value="indsend">
            </form>
    
        </body>
    
        </html>
    
        <?php
       // Close database connection
        if (isset($link)) {
          mysqli_close($link);
        }
       ?>
    

    Note: I have removed some unnecessary echo and fields for simplicity