-1

I have the following PHP v.5.6.29

<?php
// Connect to the database
include_once("php_includes/db_connect.php");

error_reporting(E_ALL); 
ini_set("display_errors", 1);

// Gather the posted data into local variables
$m = $_POST['MRN'];
$l = $_POST['LastName'];
$f = $_POST['FirstName'];
$s = $_POST['SSN'];

// Get user IP Address
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));

// Form data error handling
if($m == "" || $l == "" || $f == "" || $s == ""){
    echo "The form submission is missing data.";   
} else {
    // End form data error handling
    $sql = "INSERT INTO nys_demographics (mrn, pt_last_name, pt_first_name, pt_ssn, ip_address, record_insert_dtime)
    VALUES('$m', '$l','$f','$s','$ip',now()";
    mysqli_query($db_connect, $sql);
    echo "insert success";
}

header("refresh:3; url=demographics.php");
?>

I get the success message but then there is nothing in the db table. Not sure what to do from here.

MCP_infiltrator
  • 3,961
  • 10
  • 45
  • 82

3 Answers3

1

First print your success message only if insert actually was a success which you get from Boolean returned

if (mysqli_query($db_connect, $sql)) { 
  echo "insert success";
} else {
  printf("Error: %s\n", mysqli_error($db_connect));
}
mkungla
  • 3,390
  • 1
  • 26
  • 37
  • Got an error in my SQL syntax...guess it would have been nice if I closed the parens in the VALUES statement...caffeine needed. – MCP_infiltrator Apr 08 '17 at 03:08
  • Can happen :) , Thts why it's better to use PDO and prepared statements [some reading](http://php.net/manual/en/mysqlinfo.api.choosing.php) – mkungla Apr 08 '17 at 03:15
0

You can check to see if the query was successful by putting it in an if as per the PHP docs located here.

Kee225
  • 9
  • 4
0

I've modified the code to include a boolean conditional check. This should work assuming you are also using mysqli_connect_errno in db_connect.php file.

<?php
    // Connect to the database
    include_once("php_includes/db_connect.php");

    error_reporting(E_ALL); 
    ini_set("display_errors", 1);

    // Gather the posted data into local variables
    $m = $_POST['MRN'];
    $l = $_POST['LastName'];
    $f = $_POST['FirstName'];
    $s = $_POST['SSN'];

    // Get user IP Address
    $ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));

    // Form data error handling
    if($m == "" || $l == "" || $f == "" || $s == ""){
        echo "The form submission is missing data.";   
    } else {
        // End form data error handling
        $sql = "INSERT INTO nys_demographics (mrn, pt_last_name, pt_first_name, pt_ssn, ip_address, record_insert_dtime)
        VALUES('$m', '$l','$f','$s','$ip',now()";
        if( mysqli_query($db_connect, $sql) ) { echo "insert success"; } 
        else {  echo "error with insert"; }        
    }

    header("refresh:3; url=demographics.php");
    ?>
Anwar
  • 24
  • 4