Hope you can help me with this. I have an array that im sending over to my php script to process via ajax. What I want to do is loop through the array and based on the value of a certain key, execute a SQL statement for that array. Most of the time it will be multiple arrays. Here is what I got so far....for this example, ill put in static values:
HTML
<form id='logForm' method='post' action='seatingProcess.php'>
<span id='"219"' changeType='SEATCHANGE' setDeskId='9-4' prevSeatNew='T'></span>
<span id='"229"' changeType='SEATCHANGE' setDeskId='2-5' prevSeatNew='T'></span>
<span id='"299"' changeType='REMOVESEAT' setDeskId='0'></span>
<div class='btn btn-primary' type='submit' id='submit'>Submit</div>
</form>
Javascript
$('#submit').click(function(){
var spans = $('#logForm span');
var data = [];
$(spans).each(function(){
var newItem = {};
var item = $(this);
newItem.id = $(item).attr('id');
newItem.deskval = $(item).attr('setDeskId');
newItem.changeType = $(item).attr('changeType');
newItem.prevSeatNew = $(item).attr('prevSeatNew');
data.push(newItem);
})
$.ajax({
type: "POST",
url: "seatingProcess.php",
data: {data:data},
success: function(msg){
console.log(msg); //testing the sql statements
},
error: function(){
alert('fail');
}
})
seatingProcess.php
<?php
if(isset($_POST['data'])){
$obj = $_POST['data'];
$doKeyChange = ['changeType'];
$addValue = 'SEATCHANGE';
$delValue = 'REMOVESEAT';
foreach($obj as $doKeyChange => $addValue) {
//Array[0] {'id' => 219, 'deskval' => 9-4, 'prevSeatNew' => T}
//Array[1] {'id' => 229, 'deskval' => 2-5, 'prevSeatNew' => T}
$userid = ['id'];
$desk_id = ['desk'];
$set_desk_id = ['deskval'];
$prevseat = ['prevSeat'];
$prevseatNew= ['prevSeatNew'];
$sqlAdd = "UPDATE roster SET seat='$set_desk_id' preDesk='$prevseatNew' WHERE id='$userid'";
print_r ($sqlAdd);
}
foreach($obj as $doKeyChange => $delValue) {
//Array[2]{'id' => 299, 'deskval' => 0}
$userid = ['id'];
$set_desk_id = ['deskval'];
$sqlAdd = "UPDATE roster SET seat='$set_desk_id' WHERE id='$userid'";
print_r ($sqlAdd);
}
}
?>
Currently, im getting errors like:
Notice: Array to string conversion
Not sure what to do, thanks in advance
QUESTION
How do I write the php so that it takes the value of the requested key and puts it into the SQL statement?
'id'is a string but['id']is an array, so$userid = ['id'];gives you an array,....i.e. take the brackets off.