-1

上周我设法设置了一个页面,单击复选框将其数据直接保存到数据库中,我现在唯一的问题是如何将新值保存到数据库中的右列

我有 3 个具有不同值和名称的复选框,它们通过 ajax 发布到另一个页面。我不确定如何进行此操作。

这是我的代码片段:

<script type="text/javascript">

$(function() {

  $("input[type='checkbox']").on('click', function() {

    var $this = $(this);
    var isChecked = $this.prop('checked');
    var checkVal = isChecked ? $this.attr('id') : $this.attr("value");
    var userid = $this.attr('name');

    $.ajax({
      type: "GET",
      url: 'request.php?uname=' + checkVal +'&id=' + userid,
      success: function(data) {

        //Success 
        if(data == 1){  
          alert('Data was saved in db!');
        }

        //Failure 
        if(data == 0){
          alert('Data was NOT saved in db!');
        }
      }
    });
  });
});

html

<form id="frm" name="frm">
  <td><input  name="29" type="checkbox"          id="Paid"  value="Waiting on Payment" checked="checked"  /></td>
  <td><input  name="30" type="checkbox"      id="Repaired"  value="Waiting on Repairs"  /></td>
  <td><input  name="31" type="checkbox"  id="With Student"  value="Awaiting Pickup"  /></td>
</form>

这也是数据库结构:

名称:测试

有薪酬的 修理 回来

我发现自己无法做的是如何告诉 Mysql 查询将新值保存在哪一列,因为 GET 函数仅在另一页上提取值,而我无法将它们分开,以便支付支票只会保存在支付栏等..

这是请求页面,以便获取值的页面:

$link = mysql_connect('localhost', 'root', '');
if (!$link) {
   // error happened
   print(0);
}
mysql_select_db('test');

// sanitize the value
$value = mysql_real_escape_string($_GET['uname']);
$id = mysql_real_escape_string($_GET['id']);
// start the query
$sql = "INSERT INTO `test` VALUES ( NULL, 2, 3, '$value')";

// check if the query was executed
if(mysql_query($sql, $link)){
   // everything is Ok, the data was inserted
   print(1);    
} else {
   // error happened
   print(0);
}
4

1 回答 1

0

What you need, is to define the columns in your INSERT, like this:

INSERT INTO `test` (col1, col2, col3, ...) VALUES ( NULL, 2, 3, '$value')

Where 'col1', 'col2', etc are your columns. That way you can define the columns you need. In VALUES() you'll only put the values for the previously defined columns. For example:

INSERT INTO `person` (name) VALUES ('tom')
于 2012-05-08T10:10:11.823 回答