This problem is driving me crazy and I have no idea how to fix it. I have an Android program to gather all names and phone numbers into an array (basic key-value pairs) and then send that array over to a PHP script. The PHP script then parses the values received in a $_POST and inserts them into a MySQL table. Everything works fine except that the data doesn't always come as posted. For instance, some numbers get paired with names they don't belong to. Here's a snippet of my PHP:
$ar = 0;
foreach($_POST as $entry){
echo $ar.". ".$_POST[$ar]."\n";
$namenum = explode(',', $entry);
$names[$ar] = $namenum[1];
$numbers[$ar] = $namenum[0];
echo $ar.". ".$numbers[$ar]."--->".$names[$ar]."\n";
$ar += 1;
}
$namenum = NULL;
//$ar = NULL;
The above snippet is right at the beginning of the script and there's nothing to tamper with $_POST. The echo within the loop is to test this exact thing and that's how I discovered the problem. The weirdest part is that most of the time, the data received is exactly as sent. But then at times I suddenly notice a couple of numbers getting either mixed up with wrong names or disappearing altogether. I must also add that each time I have tested the program, I've done it on the same device, using the same contact list. Everything remains the same between test runs and yet results vary at random. The gremlins seem to be appearing in transit! Is this a known issue with $_POST or sending arrays in general? If so, what alternatives do I have? I can think of JSON or text files but would that ensure better data fidelity?
Aucun commentaire:
Enregistrer un commentaire