1

Hi i have a variables that names are

$variable1='content1' , $variable2='content2' , $variable3='content3';

and i need to loop for using that contents and index for that; example;

$index=3;
for($i=0;$i<=$index;$i++){

  $sql="insert into table values("'variable'.$i,$i")on duplicate key update variable=values(variable),controlValueThatIs$i=values(controlValueThatIs$i)";
  $mysqli_query($con,$sql);
}

Is that work?

4
  • 2
    INSERT has no WHERE clause. Perhaps you want UPDATE instead? Commented Nov 23, 2015 at 10:03
  • 4
    for the sake of god don't use mysql_ anymore, it's deprecated and not safe, use mysqli_ instead. Here's a LINK, if you want to change Commented Nov 23, 2015 at 10:05
  • actually im using insert or update and mysqli_query in my original code but im trying to explain my problem in easy way so there is some code mistakes Commented Nov 23, 2015 at 10:08
  • ok, then i'm sorry :) Commented Nov 23, 2015 at 10:09

2 Answers 2

2

you can try this

$str = 'variable'.$i;

echo $$str;

Sign up to request clarification or add additional context in comments.

1 Comment

It inserts to database like '$variable1' not content of variable1 :(
2

the problem is quote (') you should use it along with whole variable name like this -

values("'variable".$i."')

Comments

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.