【问题标题】:Update data through checkbox using jquery使用jquery通过复选框更新数据
【发布时间】:2014-03-22 13:50:08
【问题描述】:

我无法让它工作。我需要更新 mysql 中选中复选框的列的值。当我单击按钮时,它应该更新选中复选框的值。这是我的 editLayout.php 代码:

<form action="updateLayout.php" method="POST">
<input name="update" type="SUBMIT" value="Update" id="update">
<?php       
$x = 'seats';

$linkID = @ mysql_connect("localhost", "root", "Newpass123#") or die("Could not connect to MySQL server");
@ mysql_select_db("seatmapping") or die("Could not select database");

/* Create and execute query. */
$query = "SELECT * from $x order by rowId, columnId desc";
$result = mysql_query($query);
$prevRowId = null;
$seatColor = null;
$tableRow = false;

//echo $result;
echo "<table class='map'>";
while (list($rowId, $columnId, $status, $name, $seatid) = mysql_fetch_row($result))
{
if ($prevRowId != $rowId) {
    if ($rowId != 'A') {
        echo "</tr></table></td>";
        echo "\n</tr>";
    }
    $prevRowId = $rowId;
    echo "\n<tr><td align='center'><table><tr>";
} else {
    $tableRow = false;
}
if ($status == 0) {
    $seatColor = "#A6E22E";
} 
else if ($status == 1){
    $seatColor = "#D34836";
}
else if ($status == 2){
    $seatColor = "#00A0D1";
}

echo "\n<td bgcolor='$seatColor'>";
echo $seatid;
echo "<input type='checkbox' name='seats[]' id='seats' value=".$seatid.">  </checkbox>";
echo "</td>";
}
echo "</tr></table></td>";
echo "</tr>";
echo "</form>";
echo "</table>";

/* Close connection to database server. */
mysql_close();
?>

这是我驻留在不同页面 (functions.js) 上的 jquery 代码。我已经在标题中包含了这个:

jQuery(function($) {
$("form input[id='update']").click(function() {

    var count_checked = $("[name='seats[]']:checked").length;
    if(count_checked == 0) {
        alert("Please select product(s) to update.");
        return false;
    }
    if(count_checked == 1) {
        return confirm("Are you sure you want to update these product?");
    } else {
        return confirm("Are you sure you want to update these products?");
      }
});
});

这是我的 updateLayout.php.:

 <?php
$db = mysql_connect("localhost", "root", "Newpass123#");
if(!$db) { echo mysql_error(); }
$select_db = mysql_select_db("seatmapping");
if(!$select_db) { echo mysql_error(); }

if(isset($_POST['update'])) {
$id_array = $_POST['seats'];
$id_count = count($_POST['seats']);

for($i=0; $i < $id_count; $i++) {
    $id = $id_array[$i];
    $query = mysql_query("Update `seats` set `status`='2' where  `seatid`='$seatid'");
    if(!$query) { die(mysql_error()); }
}
header("Location: editLayout.php");
}
?>

我正在使用 jquery 1.11.0。我知道这里有很多 sql 注入,而且我仍在使用 mysql,但我计划在我让它工作后全部更改。任何形式的帮助表示赞赏。

提前致谢。

【问题讨论】:

  • 请解释到底是什么不工作。
  • -1 用于将@ 用于那里的功能。
  • 当我单击提交按钮时,选中复选框的值应该会更改 $status=2 的值。并将表单提交到updateLayout.php。抱歉信息不足。我是 php 编程的新手..
  • 把updateLayout.php中的location header去掉,这样可以看看更新过程中是否有错误。打印出mysql查询,检查查询是否正确...
  • @ReinderWit 我已经尝试过你所说的。我删除了位置标题。并打印出 $status。没有进行任何更改,它们仍然是 1 和 0。更新后应该等于2吧?

标签: javascript php jquery mysql checkbox


【解决方案1】:

您的更新查询未使用正确的更新值。你使用'$seatid',它没有在任何地方声明。你应该使用'$id'。

改变

for($i=0; $i < $id_count; $i++) {
    $id = $id_array[$i];
    $query = mysql_query("Update `seats` set `status`='2' where  `seatid`='$seatid'");
    if(!$query) { die(mysql_error()); }
}

进入

for($i=0; $i < $id_count; $i++) {
    $id = $id_array[$i];
    $query = mysql_query("Update `seats` set `status`='2' where  `seatid`='$id'");
    if(!$query) { die(mysql_error()); }
}

【讨论】:

  • 哇!你真是个天才,它奏效了。我怎么可能没有注意到这一点。我是新手哈哈。我希望我能支持你的回答,但我需要 15 名声望。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多