Skip to content

Instantly share code, notes, and snippets.

@schneidr
Last active December 24, 2015 21:29
Show Gist options
  • Save schneidr/6865801 to your computer and use it in GitHub Desktop.
Save schneidr/6865801 to your computer and use it in GitHub Desktop.

Nearly every line in your code is just nonsense.

somthing.com?udid=(me1,456)&&udid=(me2,457)

This will not work. You are using the same variable name twice, overwriting the first so only the second will be available in your PHP. If you want to use multiple variables you have to give every one a name or use an array:

somthing.com?udid[]=(me1,456)&&udid[]=(me2,457)

But this won't help you much, you have to split everything again. It will be easier if you call your script like this:

somthing.com?name[]=me1&id[]=456&name[]=me2&id[]=457

Now you will have to arrays in $_GET:

$_GET['name'] = Array('me1', 'm2')
$_GET['id'] = Array('456', '457')

Now you can use these array in your foreach, but I'd rather use a for loop:

for ($i = 0; $i < sizeof($_GET['name']); $i++) {
    $name = $_GET['name'][$i];
    $id = $_GET['id'][$i];
    // now build your query and execute it
    $query = "INSERT INTO udid (udid_device_owner, device_udid) VALUES ('$name', $id)";
}

You COULD use the mysql_query and mysql_real_escape_string here, but the mysql_ functions are becoming deprecated and are going to be removed in future versions of PHP. Your code will stop working then. If you are learning do it right and start with mysqli_* or with PDO objects.

An example using PDO and prepared statements would look like this:

// assuming $pdo is a valid PDO object
$stmt = $pdo->prepare("INSERT INTO udid (udid_device_owner, device_udid) VALUES (:name, :id)");
$stmt->bindParam(":name", $name, PDO::PARAM_STR);
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
for ($i = 0; $i < sizeof($_GET['name']); $i++) {
    $name = $_GET['name'][$i];
    $id = $_GET['id'][$i];
    $stmt->execute();
}

This will keep you safe from SQL injections as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment