我可以通過AJAX一個複選框值0/1更新MySQL數據庫,但我不知道如何與多個複選框爲此,MySQL數據庫更新使用多個複選框
我的代碼:的index.php
<?php
$query=mysql_connect("localhost","root","root");
mysql_select_db("gpio",$query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Checkbox Switches DevGrow.com</title>
<script type="text/javascript"src="jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#switch1').click(function(){
var myonoffswitch=$('#switch1').val();
if ($("#switch1:checked").length == 0)
{
var a="1";
}
else
{
var a="0";
}
$.ajax({
type: "POST",
url: "ajax.php",
data: "value="+a ,
success: function(html){
$("#display").html(html).show();
}
});
});
});
</script>
</head>
<body>
<input type="checkbox" name="switch1" id="switch1"
<?php
$query3=mysql_query("select pinDescription from pindescription where pinID=1");
$query4=mysql_fetch_array($query3);
if($query4['pinDescription']=="0")
{
echo "checked";
}
?> >
</body>
</html>
ajax.php
<?php
$query=mysql_connect("localhost","root","root");
mysql_select_db("gpio",$query);
if(isset($_POST['value']))
{
$value=$_POST['value'];
mysql_query("update pindescription set pinDescription='$value' where pinID='1'");
}
?>
上述代碼的工作只爲一個複選框,以什麼爲8或10複選框做。
<input type="checkbox" name="switch1" id="switch1"
<?php
$query3=mysql_query("select pinDescription from pindescription where pinID=1");
$query4=mysql_fetch_array($query3);
if($query4['pinDescription']=="0")
{
echo "checked";
}
?> >
<input type="checkbox" name="switch2" id="switch2"
<?php
$query3=mysql_query("select pinDescription from pindescription where pinID=2");
$query4=mysql_fetch_array($query3);
if($query4['pinDescription']=="0")
{
echo "checked";
}
?> >
<input type="checkbox" name="switch3" id="switch3"
<?php
$query3=mysql_query("select pinDescription from pindescription where pinID=3");
$query4=mysql_fetch_array($query3);
if($query4['pinDescription']=="0")
{
echo "checked";
}
?> >
如何在switch1 switch2 switch3的腳本中進行更改。
你的sql很容易被mySql注入。使用PDO。 –
更不用說老語法 – madalinivascu
@bub,你能告訴我如何處理PDO嗎? – nish