1

I have this code:

$id1=array($aa1,$aa2,$aa3,$aa4,$aa5,$aa6,$aa7);
$rank1=array($a1,$a4,$a7,$a10,$a13,$a16,$a19);
require_once("connection.php");
for($i=0;$i<7;$i++){    
   $sql = "update live_tracking set swim_rank = '"$rank1[$i]."' where id = '".$id1[$i]."'";
}

I want to update multiple rows of my mysql database but using the code above only the first index is being updated. Any suggestions?

6
  • you need to execute within the loop. Commented Feb 5, 2014 at 9:19
  • where you are executing the query..this is only query.. Commented Feb 5, 2014 at 9:19
  • how? do you have samples? Commented Feb 5, 2014 at 9:20
  • this is my connection.php require("constants.php"); $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } Commented Feb 5, 2014 at 9:20
  • Because you have where id=$id you should have where id > $min AND id < $max Commented Feb 5, 2014 at 9:21

4 Answers 4

1

You can execute one query only by using this :

$sql = "update live_tracking set swim_rank = '".$rank1[$i]."' WHERE id IN (";
for($i=0;$i<7;$i++){    
   $sql .= $id1[$i].",";
}

$sql .= ")";

$query = mysql_query($sql) or die("error : ".mysql_error());

if($query){
   echo "success";
} 
Sign up to request clarification or add additional context in comments.

2 Comments

how can I execute multiple query?
if you inclue mysql_query() inside for loop as proposed by other answers, you will have multiple queries. But if you use the above answer, you will have one query statement so one query to execute.
0

Execute the query within the loop itself.

    for($i=0;$i<7;$i++)
    {    
    mysql_query("update live_tracking set swim_rank = '".$rank1[$i]."' where id = '".$id1[$i]."'");
    }

3 Comments

echo the query and execute it in database to check whether there is fields corresponding to it
how can I do that? I am just new to php
are you using phpmyadmin?
0

I can see an error in the sql

$sql = "update live_tracking set swim_rank = '"$rank1[$i]."' where id = '".$id1[$i]."'";

The above should be

$sql = "update live_tracking set swim_rank = '".$rank1[$i]."' where id = '".$id1[$i]."'";

You were missing . in '".$rank1[$i]."'

1 Comment

did u use mysql_query($sql) inside the loop u need to add this as well.
0

It seems that you are on right track. but you need to execute query in the loop. like this:

$id1=array($aa1,$aa2,$aa3,$aa4,$aa5,$aa6,$aa7);
$rank1=array($a1,$a4,$a7,$a10,$a13,$a16,$a19);
require_once("connection.php");
for($i=0;$i<7;$i++){    
   $sql = "update live_tracking set swim_rank = '".$rank1[$i]."' where id = '".$id1[$i]."'";
   if(!mysql_query($sql)){
      echo "not updated".$id1[$i]; exit();  
   }


}

5 Comments

not updated1. That is what being echoed. How can I fix this. This is for my thesis.
you can echo $sql variable and run the output query in your mysql server
how? can you elaborate more?
how can I run my query in my sql server? Do you have examples?
are you using phpmyadmin?

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.