I am editing a code which supposed to update database when value is change, however it doesn't work, hope someone could help me fix it.
I am using DISTINCT to get the data and need to update a few data at the same time. it can display the value, but it can't save in database.
for example, I will using DISTINCT to get a few data which with the same date and I will change values among those data at the same time by using that code.
index.php
<script>
window.onload = function() {
$(".cal_amount").change(function() {
var auto_array = {};
//Step 1- On change use The closest() method to get the all input elements value of selected element row.
form_data = $(this).closest('tr').find('input, select');
//Step 2- On change Use map to store input elements value with name as key in the array.
var myArray = $.map(form_data, function(element) {
auto_array[element.name] = element.value;
//return {name: element.name, value: element.value};
});
form_data = $(this).closest('tr').find('input,select').serialize();
$.ajax({
data: {
action: 'update_price',
form_data: form_data,
},
url: 'updates_ok.php',
type: 'post',
beforeSend: function() {
},
success: function(data) {
if(data == 1){
alert('update sucessful')}
}
});
});
};
</script>
<script>
window.onload = function() {
$(".day_record").change(function() {
var auto_array = {};
//Step 1- On change use The closest() method to get the all input elements value of selected element row.
form_data = $(this).closest('tr').find('input, select');
//Step 2- On change Use map to store input elements value with name as key in the array.
var myArray = $.map(form_data, function(element) {
auto_array[element.name] = element.value;
//return {name: element.name, value: element.value};
});
form_data = $(this).closest('tr').find('input,select').serialize();
$.ajax({
data: {
action: 'update_data',
form_data: form_data,
},
url: 'updates_ok.php',
type: 'post',
beforeSend: function() {
},
success: function(data) {
if(data == 1){
alert('update sucessful');}
}
});
});
};
</script>
update.php
<?php
if($_POST['action'] == 'update_price'){
//parse the serialize data
parse_str($_POST['form_data'], $my_form_data);
/*echo "<pre>";
print_r($my_form_data);*/
$id = $my_form_data['id'];
$gp_name = $my_form_data['gp_name'];
$price = $my_form_data['price'];
$cost = $my_form_data['cost'];
$sql= $query = $finalquery = $sqlresult = '';
if($cost){
$sql.="cost='$cost',";
}
if($price){
$sql.="price='$price',";
}
$finalquery = rtrim($sql,',');
$query="UPDATE `gp_info` SET $finalquery where id=$id";
$sqlresult=mysql_query($query);
if($sqlresult){
$reback=1;
}else{
$reback=0;
}
echo $reback;
}
if($_POST['action'] == 'update_data'){
//parse the serialize data
parse_str($_POST['form_data'], $my_form_data);
/*echo "<pre>";
print_r($my_form_data);*/
$gp_name = $my_form_data['gp_name'];
$date = $my_form_data['date'];
$day = $my_form_data['day'];
$sql= $query = $finalquery = $sqlresult = '';
if($date){
$sql.="date='$date',";
}
if($day){
$sql.="day='$day',";
}
$finalquery = rtrim($sql,',');
$query="UPDATE `gp_info` SET $finalquery where gp_name='$gp_name' AND date='$date' AND day='$day'";
$sqlresult=mysql_query($query);
if($sqlresult){
$reback=1;
}else{
$reback=0;
}
echo $reback;
}
?>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…