how to post arrays to php
solved, see my FINAL ANSWER post for complete details on what worked for me
*original question and edits below:
looking for a way to set multiple sql items by a posted array.
i'm pretty sure I'm using php5.
here is how i set a single one, but i don't know much about arrays in php
mysql_query
("
UPDATE comics
SET xml = UpdateXML(xml,'comic/pageNumber', '<pageNumber>'.$pageNumber.'</pageNumber>')
WHERE id = $id
") or die(Err(mysql_error()));
i would like to Post an idArray and a newPageNumberArray from flash
where comic with id of idArray[0] would set its pageNumber to newPageNumberArray[0] and so on.
the a开发者_开发技巧rrays would be of equal length to each other. but could be of any length, depending on the number of altered page numbers in the admin tool.
for now i could just make a separate php request per item, but i think it would be much cleaner to send them all out in one php request.
*edit
Thanks to Patrick i feel closer... but i still don't have it quite right.
flash sends these arrays
idArr: 34,24
pageNumArr: 1,2
in php i have this function
function changePageNumbers($con, $idArr, $pageNumArr){
selectDataBase($con);
echo '&startForLoop=true';
for($i=0;$i<count($idArr);$i++)
{
$thisPageNum = mysql_real_escape_string($pageNumArr[$i]);
$thisId = mysql_real_escape_string($idArr[$i]);
echo '&thisPageNum='.$thisPageNum;
echo '&thisId='.$thisId;
mysql_query
("
UPDATE comics
SET xml = UpdateXML(xml,'comic/pageNumber', '<pageNumber>".$thisPageNum."</pageNumber>')
WHERE `id` = ".$thisId."
LIMIT 1;
") or die(Err(mysql_error()));
echo '&querySent=true';
}
echo '&endForLoop=true';
}
it returns this
thisPageNum=2&endForLoop=true&thisId=2&startForLoop=true&querySent=true
which seems out of order (maybe that's normal?)
more importantly it only lists thisPageNum and thisId one time, when the array has 2.
and still more importantly it lists thisId = 2. the id's that are sent are 34 and 24
*edit2 it works when i run it in html and manually set the arrays in php
$idArr = array(34,24);
$pageNumArr = array(2, 1);
which means that passing my arrays through $_POST from flash, is failing in some way. I'm certain they are well constructed arrays in flash. perhaps on their way through $_POST they become Comma Separated Values.
running more tests.
set the data in an array like:
$uploaded_data = array(
0 => array(
'xml' => '..blah...blah',
'id' => 231
),
1 => array(
'xml' => 'bleh...bleh..',
'id' => 232
)
);
EDIT updated array data from $_POST this is the way the data would look from a normal form POST
$_POST = array(
'xml' => array(
'..blah...blah',
'bleh...bleh..'
),
'id' => array(
231,
232
),
'submit' => 'Submit'
);
and iterate through the array and do an insert per each interior array
foreach($uploaded_data as $data)
{
$update_query = sprintf("UPDATE comics
SET xml = UpdateXML('%s','comic/pageNumber', '<pageNumber>'.$pageNumber.'</pageNumber>')
WHERE id = '%s'
LIMIT 1;",
mysql_real_escape_string($data['xml']),
mysql_real_escape_string($data['id'])
);
$update_result = mysql_query($update_query);
}
EDIT 2 updated looping through $_POST data
for($i=0;$i<count($_POST['xml']);$i++)
{
if(!array_key_exists($i, $_POST['id']))
{
// if there is not a value for the id's at this count, break
$i = count($_POST['xml']);
continue;
}
$update_query = sprintf("UPDATE comics
SET xml = UpdateXML('%s','comic/pageNumber', '<pageNumber>'.$pageNumber.'</pageNumber>')
WHERE id = '%s'
LIMIT 1;",
mysql_real_escape_string($_POST['xml'][$i]),
mysql_real_escape_string($_POST['id'][$i])
);
$update_result = mysql_query($update_query);
}
IMPORTANT:
ALWAYS set a limit on UPDATE
queries. An error may occur and accidentally update the entire database rather than a single row.
IMPORTANT 2
Make sure you always sanitize your db inputs as well, (mysql_real_escape_string()
)
You can send them all in one request and create a loop in PHP to update them one by one. As far as I know there is no way to do it in one statement, or you will have to create complex case constructs. That might lead to some better database performance, but it's a hell of lot more work for someone with little SQL exprience, so I should do it in PHP.
Don't forget to start a transaction. :)
loop:
foreach($_POST['variableofyourchoice'] as $id)
{
// Execute the query using $id
}
FINAL ANSWER
The only way i could get the Post to go through correctly is using CSV's and PHP explode(); as GolezTrol suggested
and Patrick demonstrated the process of stepping through arrays in PHP.
so this is a combination of the assistance provided:
My final code after some testing.
To run a test w/o flash
$_POST["idArr"] = "34,24";
$_POST["pageNumArr"] = "2,1";
To turn CSV's (Comma Separated Values) into arrays
$idArr = explode(",", $_POST["idArr"]);
$pageNumArr = explode(",", $_POST["pageNumArr"]);
Stepping through arrays and sending sql querys
for($i=0;$i<count($idArr);$i++)
{
$thisPageNum = mysql_real_escape_string($pageNumArr[$i]);
$thisId = mysql_real_escape_string($idArr[$i]);
mysql_query
("
UPDATE comics
SET xml = UpdateXML(xml,'comic/pageNumber', '<pageNumber>".$thisPageNum."</pageNumber>')
WHERE `id` = ".$thisId."
LIMIT 1;
") or die(Err(mysql_error()));
}
"Correct Answer"... goes to GolezTrol
since there can be only one, i figure GolezTrol answered the question as stated in the title, while Patrick helped address underlying issues i would have had immediately afterward.
thank you both for your assistance and patience.
精彩评论