0

I have an array ($updateInputs) with 6 elements, 6 / 2 which each 3 belong to a single row in a database table. What I'm trying to do is put a query in a while loop so that every iteration, the index will be correctly placed for the next 3 elements. $i starts at 0 and gets + 1 every loop. I'm trying to do the following but it just crashes my website:

$tempMult = $i*3;
$updatedInputs = $_REQUEST['UPDATEDtoy'];
$query = "UPDATE TOY 
        SET TOYID='$updatedInputs[0 + $tempMult]', TOYName='$updatedInputs[1 + $tempMult]', TOYprice='$updatedInputs[2 + $tempMult]',
        TOYmanufacturer='$updatedInputs[3 + $tempMult]'
        WHERE TOYID='$exactID[$i]'";

Is there another way to go about this? I just need it so that the second loop, the index will start at 3->4->5. I assume it isn't possible to do operations inside queries? Any help is appreciated

EDIT (full version): this is what gets returned when I use

print_r($updatedInputs);
$tempMult = $i*6;
echo "$tempMult <br>";

I get :

Array ( [0] => foo1 [1] => moo1 ) Array ( [0] => foo1 [1] => foo2 [2] => foo3 [3] => foo4 [4] => foo5 [5] => foo6 [6] => moo1 [7] => moo2 [8] => moo3 [9] => moo4 [10] => moo5 [11] => moo6 ) 0

Array ( [0] => foo1 [1] => foo2 [2] => foo3 [3] => foo4 [4] => foo5 [5] => foo6 [6] => moo1 [7] => moo2 [8] => moo3 [9] => moo4 [10] => moo5 [11] => moo6 ) 6

as you can see, 0 and 6 is correct (in the case of 12 elements). It gets printed twice because there are 2 loops

EDIT2 (temporary NOOB solution):

I'm just going to use this as a temporary noobie solution until I see an answer:

$a = 0 + $i*6;
$b = 1 + $i*6;
$c = 2 + $i*6;
$d = 3 + $i*6;
$e = 4 + $i*6;
$f = 5 + $i*6; 

$updatedInputs = $_REQUEST['UPDATEDtoy'];
$query = "UPDATE TOY 
SET TOYID='$updatedInputs[$a]', TOYName='$updatedInputs[$b]' ...
WHERE TOYID='$exactID[$i]'";
$result = mysqli_query($link, $query);
9
  • "crashes my website" unlikely, do you check for errors?
    – user557846
    Commented Jun 12, 2013 at 2:36
  • are you sure $updatedInputs has index 0 + $tempMult ? what are you getting when you echo the $query and var_dump($updatedInputs ) ? Commented Jun 12, 2013 at 2:40
  • @Dagon yes everything works fine until i put + $tempMult. I get Server Error Commented Jun 12, 2013 at 3:11
  • @NullPoiиteя yes I am sure it starts at zero. When i print_r and var_dump $updateInputs, I get Array([0]=>.. [1]=>..and all my elements in the array Commented Jun 12, 2013 at 3:13
  • can you add what exactly are you getting .. in question .. so i can help you Commented Jun 12, 2013 at 3:14

1 Answer 1

-1

Use PDO. See the examples in the link. You just need to pass the already computed variables to PDO and it will automatically insert them into your query.

2
  • I'm not going to post an answer that will get him hacked via SQL injection (which is exactly what the original code is setup for). Commented Jun 12, 2013 at 2:44
  • 1
    i am just disagree that using mysq_* is not safe .. those are as safe as pdo Commented Jun 12, 2013 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.