在此之前,我可以在数据库中插入数据。我正在使用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);
?>
您很容易受到[SQL注入攻击](http://bobby-tables.com)的影响。在尝试修复此代码之前,您最好了解有关防止这些攻击的信息。 – 2013-04-21 17:03:05
$ _POST [Nama]应该是$ _POST [“Nama”]等等+您应该在查询中使用它们之前对输入进行消毒... – RafH 2013-04-21 17:03:57
@rafh:-1。没有。数组键不能用双引号字符串引用,除非用'{}'包围数组引用。例如'“$ arr [x]”'没问题。 '“{$ arr ['x']}”'可以,但是'“$ arr ['x']”'是** NOT **。 – 2013-04-21 17:06:04