无法使用PHP和JQuery Mobile将数据插入数据库

时间:2013-04-21 17:02:02

标签: php mysql jquery-mobile

我可以在此之前多次将数据插入数据库。我正在使用web矩阵和XAMPP来开发。我在处理端口时遇到问题但是在更改了某些内容后它变得没问题,但数据库不再接收数据了。我开发的另一个使用PHP的系统运行良好,但问题出在jQuery 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 个答案:

答案 0 :(得分: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日志,以了解失败的可能原因。