Answer the question
In order to leave comments, you need to log in
Update query not working after mysqli_fetch_assoc?
// $link = mysqli_connect($host, $user, $pass, $db_name);
function setNewColor($link) {
$query = "SELECT * FROM colors";
$result = mysqli_query($link, $query);
for ($data = []; $row = mysqli_fetch_assoc($result); $data[] = $row);
mysqli_free_result($result);
for ($i=0;$i<count($data);$i++) {
if ($data[$i]['state'] == 1) {
//print_r($data[$i]);
$id = $data[$i]['id'];
$query = "UPDATE colors SET state = 0 WHERE id=$id";
$result = mysqli_query($link, $query);
//Если state = 1 у последнего цвета, меняем state у первого, иначе меняем state у i++ (следующего).
if ($i == count($data)-1) {
$id = $data[0]['id'];
$query = "UPDATE colors SET state = 1 WHERE id = $id";
$result = mysqli_query($link, $query);
} else {
$id = $data[$i++]['id'];
$query = "UPDATE colors SET state = 1 WHERE id = $id";
$result = mysqli_query($link, $query);
}
break;
}
}
}
function setNewColor($link) {
$query = "SELECT * FROM colors";
$result = mysqli_query($link, $query);
for ($data = []; $row = mysqli_fetch_assoc($result); $data[] = $row);
mysqli_free_result($result);
$i;
$id;
for ($i=0;$i<count($data);$i++) {
if ($data[$i]['state'] == 1) {
//print_r($data[$i]);
$id = $data[$i]['id'];
break;
}
}
$query = "UPDATE colors SET state = 0 WHERE id=$id";
$result = mysqli_query($link, $query);
//Если state = 1 у последнего цвета, меняем state у первого, иначе меняем state у i++ (следующего).
if ($i == count($data)-1) {
$id = $data[0]['id'];
$query = "UPDATE colors SET state = 1 WHERE id = $id";
$result = mysqli_query($link, $query);
} else {
$id = $data[$i++]['id'];
$query = "UPDATE colors SET state = 1 WHERE id = $id";
$result = mysqli_query($link, $query);
}
}
Answer the question
In order to leave comments, you need to log in
$id = $data[$i++]['id'];
$id = $data[++$i]['id'];
$id = $data[$i+1]['id'];
Where is the fix?
I wrote a bunch of meaningless sentences and did not write the main thing.
Questions like "Mine doesn't work" should be asked in this format
Here is the result of the select:
the code with the row select and the output of the result
Here is the result of the update of the same line:
the code with the update and the output of the result
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question