'How to save to create table in DB

I have over the last week managed to setup a page which on click of a check box saves its data to a database directly my only issue now is how to save the new values to the right column in the database.

I have 3 checkboxes with different values and names that are posted through ajax to the other page. I'm unsure of how to proceed with this.

Here is a snippet of my code:

<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!');
        }
      }
    });
  });
});

The 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>

Here's the database structure as well:

name: test

Paid repair Returned

What I am finding myself unable to do is how to tell the Mysql query which column to save the new value in because the GET function is only pulling in the value on the other page and I can't get it to separate them so that the paid check will only save in the paid column and so on..

Here is the request page so that page that gets the value:

$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);
}


Solution 1:[1]

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')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Katai