2017-04-05 99 views
0

我正在爲某個項目製作表格。問題是當用戶使用該字段輸入他們的數據然後提交時,輸入不保存在數據庫中。 當點擊提交直接頁面顯示空白頁面時甚至連接測試也不顯示。 我爲其他項目使用幾乎相似的代碼,除此之外它工作。下面 是我的代碼:數據未插入數據庫

<?php 

    //check connection 
    require 'config.php'; 
    if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
    } 
    echo "Connected successfully"; 

    //asas (table name) 
     $id = $_POST["Sid"]; $ic = $_POST["Sic"]; 
     $name = $_POST["Snp"]; $jant = $_POST["J1"]; 
     $trum = $_POST["Chr"];$tbim = $_POST["Chp"]; 
     $mel = $_POST["Sem"]; $arum = $_POST["Ar"]; 
     $asum = $_POST["As"]; 

    //institusi 
     $thp = $_POST["T1"]; $uni = $_POST["Sis"]; 
     $bid = $_POST["tpe"];$Aint = $_POST["Ai"]; 

//industri 
     $bip = $_POST["bid"];$bik = $_POST["B1"]; 
     $tem = $_POST["te"];$mula = $_POST["tm"]; 
     $tamm = $_POST["tt"]; $res = $_POST["fileToUpload1"]; 
     $tran = $_POST["fileToUpload2"];$keb = $_POST["fileToUpload3"]; 


    $link = mysqli_connect($h,$u,$p,$db); 

    if('id' != '$Sid'){ 
    $asas = "insert into asas Values ('$id','$ic','$name','$jant','$trum','$tbim','$mel','$arum','$asum')"; 
    $inst = "insert into institusi Values ('$thp','$uni','$bid','$Aint')"; 
    $indr = "insert into industri Values ('$bip','$bik','$tem','$mula','$tamm','$res','$tran','$keb')"; 

     mysqli_query($link,$asas); 
     mysqli_query($link,$inst); 
     mysqli_query($link,$indr); 
     mysqli_close($link); 
    } 
     else 
    { 
    echo "failed" 
    } 
    ?> 
    <b>Register complete</b> 

誰能告訴我什麼是錯誤或者一些解決方案。由於

+0

[PHP。錯誤。登錄](http://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings/5438125#5438125) – mkaatman

+0

也許你的表單提交後不會進入這個頁面。 –

+1

@Nexz你的代碼容易受到SQL注入攻擊。考慮使用Prepared Statements。 http://php.net/manual/en/mysqli.prepare.php –

回答

1

只是使用或mysqli_query後死亡

mysqli_query($link,$asas)or die ('Unable to execute query. '. mysqli_error($link)); 

你會了解什麼是真正的問題

+0

謝謝,我會稍後再試。 – Nexz

1

我以爲你是在插入查詢問題,請檢查:

$sql = "INSERT INTO MyGuests (firstname, lastname, email) 
VALUES ('John', 'Doe', '[email protected]')"; 

寫下這種。

謝謝

+0

謝謝,我會稍後根據你的建議嘗試。 – Nexz

+0

您的歡迎:) – Aaron

1
there are few issues with the code like variable id was used without $  
and need to use die method with mysqli_query() function to check for 
errors, please check below improved codes, it may help you - 

<?php 
    //check connection 
    require 'config.php'; 
    if (isset($_POST)) { 
//asas (table name) 
$id = $_POST["Sid"]; 
$ic = $_POST["Sic"]; 
$name = $_POST["Snp"]; 
$jant = $_POST["J1"]; 
$trum = $_POST["Chr"]; 
$tbim = $_POST["Chp"]; 
$mel = $_POST["Sem"]; 
$arum = $_POST["Ar"]; 
$asum = $_POST["As"]; 
//institusi 
$thp = $_POST["T1"]; 
$uni = $_POST["Sis"]; 
$bid = $_POST["tpe"]; 
$Aint = $_POST["Ai"]; 
//industri 
$bip = $_POST["bid"]; 
$bik = $_POST["B1"]; 
$tem = $_POST["te"]; 
$mula = $_POST["tm"]; 
$tamm = $_POST["tt"]; 
$res = $_POST["fileToUpload1"]; 
$tran = $_POST["fileToUpload2"]; 
$keb = $_POST["fileToUpload3"]; 
} 
$link = mysqli_connect($h, $u, $p, $db); 
if (!$link) { 
die("Connection failed: " . mysqli_connect_error()); 
} 
// if('id' != '$Sid'){ 
if ($id != '$Sid') { 
$asas = "insert into asas Values 
('$id','$ic','$name','$jant','$trum','$tbim','$mel','$arum','$asum')"; 
$inst = "insert into institusi Values ('$thp','$uni','$bid','$Aint')"; 
$indr = "insert into industri Values 
('$bip','$bik','$tem','$mula','$tamm','$res','$tran','$keb')"; 
if (mysqli_query($link, $asas)) { 
    echo "records inserted"; 
} else { 
    echo "failed".mysqli_error($link) ; 
} 
if (mysqli_query($link, $inst)) { 
    echo "records inserted"; 
} else { 
    echo "failed".mysqli_error($link) ; 
} 
if (mysqli_query($link, $indr)) { 
    echo "records inserted"; 
} else { 
    echo "failed".mysqli_error($link) ; 
} 
} 
mysqli_close($link); 
?> 
<b>Register complete</b> 
+0

謝謝,代碼工作。但是,我仍然不知道爲什麼它沒有插入數據。 – Nexz

+0

有一些小的錯誤,像id變量沒有使用$符號,如果('id' !='$ Sid')和分號錯過某處。 –