Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
you need to execute within the loop. –  Alan Kael Ball Feb 5 at 9:19
    
where you are executing the query..this is only query.. –  Jenz Feb 5 at 9:19
    
how? do you have samples? –  DexterAnthony Feb 5 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(); } –  DexterAnthony Feb 5 at 9:20
    
Because you have where id=$id you should have where id > $min AND id < $max –  meLove Feb 5 at 9:21

4 Answers 4

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]."'");
    }
share|improve this answer
    
It says empty query. I just copied your code. –  DexterAnthony Feb 5 at 9:27
    
echo the query and execute it in database to check whether there is fields corresponding to it –  Jenz Feb 5 at 9:28
    
how can I do that? I am just new to php –  DexterAnthony Feb 5 at 10:43
    
are you using phpmyadmin? –  Jenz Feb 5 at 10:47

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]."'

share|improve this answer
    
I edited it but only the first index is updated. –  DexterAnthony Feb 5 at 9:23
    
did u use mysql_query($sql) inside the loop u need to add this as well. –  Abhik Chakraborty Feb 5 at 9:24

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();  
   }


}
share|improve this answer
    
not updated1. That is what being echoed. How can I fix this. This is for my thesis. –  DexterAnthony Feb 5 at 9:38
    
you can echo $sql variable and run the output query in your mysql server –  Awlad Liton Feb 5 at 10:27
    
how? can you elaborate more? –  DexterAnthony Feb 5 at 10:35
    
how can I run my query in my sql server? Do you have examples? –  DexterAnthony Feb 5 at 10:36
    
are you using phpmyadmin? –  Awlad Liton Feb 5 at 10:48

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";
} 
share|improve this answer
    
how can I execute multiple query? –  DexterAnthony Feb 5 at 9:54
    
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. –  mansoulx Feb 5 at 10:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.