0

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>
Saud
  • 480
  • 1
  • 9
  • 26
  • 3
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 18 '17 at 21:07
  • 3
    Nothing is displayed because you have no errors yet. There is nothing to be echo'd for success. – Jay Blanchard Aug 18 '17 at 21:08
  • I can't see any `
    ` declaration in your output page. Are you sure you open the correct page? Try remove all php code from `IndexEventInputForm.php` and just open the page with *hello world* into it.
    – vadim_hr Aug 18 '17 at 21:59
  • 1
    That is exactly what I'd expect to see if you had error reporting turned off, and had encountered an error. For example, if MAMP is using PHP7, then your use of the obsolete mysql_* functions would trigger an error, very early, and virtually nothing would be output. Then the HTML would be tidied up by Safari to leave the basically empty document you see. (Presumably you have the Grammarly plugin installed in Safari, which would explain the odd "grammarly" classes?) What do you see if you Develop->Show Page Source in Safari rather than using the DOM inspector? – Matt Gibson Aug 18 '17 at 22:00
  • (Oh, and make sure to [turn error reporting on](https://codingexplained.com/dev-ops/mac/enabling-php-error-reporting-mamp) while you're developing.) – Matt Gibson Aug 18 '17 at 22:03
  • Using sf_admin's advice and replacing all mysql_* with mysqli_* gave me the following output: "Can't use EventInputForm: ". – Saud Aug 18 '17 at 22:14

2 Answers2

1

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);

}
sf_admin
  • 577
  • 4
  • 11
1

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

Fahadi Muhumuza
  • 131
  • 1
  • 5
  • It worked :) Most notable difference I see is that you add the DB_NAME inside the link variable. What if I want to connect to one more table - will I need to define a link variable every time? – Saud Aug 18 '17 at 22:21
  • Remember a single database can contain multiple tables so you dont need to define the link again. May be if your trying too connect to multiple databases under the same DB_USER then you can go on with that for examle $db1 = mysqli_select_db($link, $database_name1); $db2 = mysqli_select_db($link, $database_name2); And u make sure you specify the $db1 or $db2 when your trying to query like using mysqli_query($db1, $sql); – Fahadi Muhumuza Aug 18 '17 at 23:05