2013-04-21 97 views
1

在此之前,我可以在數據庫中插入數據。我正在使用Web矩陣和XAMPP進行開發。我在處理端口時遇到了問題,但是在改變了一些東西之後它就會變好,但數據庫不再接收數據。我使用PHP開發的其他系統運行良好,但問題出在jQuery手機上。無法使用PHP和J Query Mobile將數據插入到數據庫中

這是index.html文件

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <meta name="apple-mobile-web-app-capable" content="yes" /> 
    <meta name="apple-mobile-web-app-status-bar-style" content="black" /> 
    <title> 
    </title> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
    <link rel="stylesheet" href="my.css" /> 
    <script src="http://code.jquery.com/jquery-1.7.2.min.js"> 
    </script> 
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"> 
    </script> 
    <script src="my.js"> 
    </script> 
    <!-- User-generated css --> 
    <style> 
    </style> 
    <!-- User-generated js --> 
    <script> 
     try { 

    $(function() { 

}); 

} catch (error) { 
    console.error("Your javascript has an error: " + error); 
} 
    </script> 
</head> 
<body> 
    <!-- Home --> 
    <div data-role="page" id="page1"> 
     <div data-theme="a" data-role="header" data-position="fixed"> 
      <h3> 
       Student Uitm 
      </h3> 
     </div> 
     <div data-role="content"> 
      <form action="http://localhost/student/save2db.php" method="post"> 
       <label>Nama</label><input type="text" name="Nama"> 
       <label>No Kad Pengenalan</label><input type="text" maxlength="12" name="icno"> 
       <label>Jantina</label><select name="jantina"> 
       <option value="L">Lelaki</option> 
       <option value="P">Perempuan</option> 
      </select> 
       <input name="sbt" type="submit" value="save" data-inline="true"> 
      </form> 
     </div> 
     <div data-theme="a" data-role="footer" data-position="fixed"> 
      <h3> 
       Footer 
      </h3> 
     </div> 
    </div> 
</body> 
</html> 

,這是save2db.php文件:

<?php 
$con=mysqli_connect("localhost","root"," ","student"); 
    // Check connection 
if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$sql="INSERT INTO student1 (Nama, icno, jantina) 
VALUES 
('$_POST[Nama]','$_POST[icno]','$_POST[jantina]')"; 

if (!mysqli_query($con,$sql)) 
{ 
    die('Error: ' . mysqli_error($con)); 
    } 
echo "1 record added"; 

mysqli_close($con); 
?> 
+1

您很容易受到[SQL注入攻擊](http://bobby-tables.com)的影響。在嘗試修復此代碼之前,您最好了解有關防止這些攻擊的信息。 – 2013-04-21 17:03:05

+0

$ _POST [Nama]應該是$ _POST [「Nama」]等等+您應該在查詢中使用它們之前對輸入進行消毒... – RafH 2013-04-21 17:03:57

+0

@rafh:-1。沒有。數組鍵不能用雙引號字符串引用,除非用'{}'包圍數組引用。例如'「$ arr [x]」'沒問題。 '「{$ arr ['x']}」'可以,但是'「$ arr ['x']」'是** NOT **。 – 2013-04-21 17:06:04

回答

1

的問題:

  • 您檢查數據庫連接,但即使發生故障,您仍然可以通過腳本運行
  • 您不是荷蘭國際集團謹慎足以與接收到的數據
  • 你不記錄錯誤
  • 內部問題的症狀泄露給用戶

這裏有一個可能替代你的腳本照顧的這些問題:

<?php 
$con = mysqli_connect("localhost","root"," ","student"); 
// Check connection 
if (mysqli_connect_errno()) { 
    trigger_error("Failed to connect to MySQL: " . mysqli_connect_error(), 
        E_USER_ERROR); 
    die("unable to complete request"); 
} else if (!(isset($_POST['Nama']) && 
     isset($_POST['icno']) && isset($_POST['jantina'])) { 
    trigger_error("incomplete POST data", E_USER_ERROR); 
    die("unable to complete request"); 
} else { 
    $nama = mysqli_real_escape_string($con, $_POST['Nama']); 
    $icno = mysqli_real_escape_string($con $_POST['icno']); 
    $jantina = mysqli_real_escape_string($con,$_POST['jantina']); 
    $sql = "INSERT INTO student1 (Nama, icno, jantina) VALUES ('$nama','$icno','$jantina')"; 
    if (!mysqli_query($con,$sql)) { 
     trigger_error("query failed :" . mysqli_error($con), E_USER_ERROR); 
     die("unable to complete request"); 
    } 
    echo "1 record added"; 
    mysqli_close($con); 
} 

完成上述修改後,請在更新失敗後檢查php日誌以瞭解失敗的可能原因。

+0

感謝您給出正確的腳本,並在檢查php_error_log時列出此錯誤 PHP解析錯誤:語法錯誤,意外的'{'在第8行C:\ xampp \ htdocs \ student \ save2db.php – sakin 2013-04-22 08:46:27

+0

抱歉, t似乎發現了這個問題(儘管我發現了我自己的一些錯誤)。 – didierc 2013-04-22 11:09:42

相關問題