2016-07-07 61 views
1

選擇查詢的MySQL我已經花費超過24小時,試圖運行更新或選擇查詢,但選擇查詢完成,更新後插入查詢或插入查詢時submite「displayid」運行更新或PHP

從未做過後插入查詢

code ##

if($_POST["displayid"]==TRUE) { 

    $sqlid = "SELECT * FROM doc1 WHERE idnum ='$pidnum' AND stats='$ok'"; 
    $result = mysqli_query($conn, $sqlid); 
    if (mysqli_num_rows($result) > 0) { 
     $sqlup = "UPDATE doc1 SET m_phone='$pmphone', seen='$dataseen' WHERE idnum ='$pidnum'"; 
     mysqli_query($conn, $sqlup); 
     $found=1; 
    } 
    else { 
     $found=0; 
     $sqlfail="INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date) VALUES('$pfname','$plname','$ptname','$pfuname','$pidnum','$pm_phone','$todaydate')"; 
     $conn->query($sqlfail); 
    } 

} 
+1

試試'如果(isset($ _ POST) 「displayid」])'代替'如果($ _ POST [ 「displayid」] = = TRUE)'。瞭解準備好的語句以防止SQL注入 – Jens

+0

試試這個$ conn-> query($ sqlfail);到這個mysqli_query($ conn,$ sqlfail); – JYoThI

+0

我使用連接作爲$ conn = new mysqli($ servername,$ username,$ password,$ dbname); – soma

回答

0

首先你更新查詢是錯誤的。 檢查錯誤請加

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

更新代碼

if ($_POST["displayid"] == TRUE) { 

    $sqlid = "SELECT * FROM doc1 WHERE idnum ='$pidnum' AND stats='$ok'"; 
    $result = mysqli_query($conn, $sqlid); 
    if (mysqli_num_rows($result) > 0) { 
     $sqlup = "UPDATE doc1 SET m_phone='$pm_phone', seen='$dataseen' WHERE idnum ='$pidnum'"; 
     mysqli_query($conn, $sqlup); 
     $found = 1; 
    } else { 
     $found = 0; 
     $sqlfail = "INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date) 
VALUES ('$pfname','$plname','$ptname','$pfuname','$pidnum','$pm_phone','$todaydate')"; 
     $conn->query($sqlfail); 
    } 
} 
+0

它工作嗎? – urfusion

+0

您的更新代碼我現在工作,但插入仍然無法工作 – soma

+0

插入可能無法正常工作,因爲您通過'select'查詢得到值 – urfusion

0

$ conn對象從哪裏來?試試這個..

<?php 

if($_POST["displayid"]) 
{ 

$sqlid = "SELECT * FROM doc1 WHERE idnum ='$pidnum' AND stats='$ok'"; 
$result = mysqli_query($conn, $sqlid); 

if (mysqli_num_rows($result) > 0) 
{ 

    $sqlup= "UPDATE doc1 SET m_phone='$pm_phone' AND seen='$dataseen' WHERE idnum ='$pidnum'"; 
    mysqli_query($conn, $sqlup); 

    $found=1; 

} 
else 
{ 

$found=0; 

$sqlfail="INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date) 
VALUES('$pfname','$plname','$ptname','$pfuname','$pidnum','$pm_phone','$todaydate')"; 
mysqli_query($conn, $sqlfail); 


} 

} 
+0

在upadte代碼錯誤是使用「,」即時的「AND」 - 但插入代碼不工作 – soma