2017-04-08 61 views
-1

我有以下的PHP v.5.6.29PHP形式給出了成功的消息,但沒有在DB

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

我得到了成功的消息,但再沒有什麼在db表。不知道從這裏做什麼。

回答

1

先打印僅當插入實際上是你從Boolean獲得成功成功消息返回

if (mysqli_query($db_connect, $sql)) { 
    echo "insert success"; 
} else { 
    printf("Error: %s\n", mysqli_error($db_connect)); 
} 
+0

在我的SQL語法中出現了一個錯誤...如果我關閉了VALUES語句中的parens ......需要咖啡因, –

+0

可能會發生:),爲什麼使用PDO和準備好的語句[某些閱讀]更好(http://php.net/manual/en/mysqlinfo.api.choosing.php) –

0

根據位於here的PHP文檔,您可以通過將查詢放入if來查看查詢是否成功。

0

我已經修改了代碼,包括布爾條件檢查。這應該可以工作,假設你也在db_connect.php文件中使用mysqli_connect_errno。

<?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"); 
    ?> 
相關問題