Saud
Saud

Reputation: 490

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>

Upvotes: 0

Views: 358

Answers (2)

Fahadi Muhumuza
Fahadi Muhumuza

Reputation: 161

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

Upvotes: 1

sf_admin
sf_admin

Reputation: 577

I notice a few problems:

  • You're echoing the variable $name, but there is no such variable defined in your script.

  • You're using mysql_* instead of mysqli_* functions.

To address these two items and help you see your results see an example of what you can test with below. **** Please note: prepared statements should be used in production.

if (!isset($_POST['Title'], $_POST['indsend']))
    echo "Submit form below! \n";
else {

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

    // sanitize user input
    $title = $mysqli->real_escape_string($_POST['Title']);

    // execute query
    $mysqli->query("INSERT INTO EventInfo (Title) VALUES ('$title')");

    printf ("New Record has id %d.\n", $mysqli->insert_id);

}

Upvotes: 1

Related Questions