I am trying to get values from an form with arrays them I need to get the ids from the bd to update, but it isnt working, maybe u give me an idea, I’ll post the code, so u may understand. Thank you in advance for your help!
<?php
include "conecta.php";
include "verifica.php";
$sql1 = "SELECT idcategorias FROM categorias";
$res = mysql_query($sql1);
while ($row=mysql_fetch_array($res)){
$gosma = $row["idcategorias"];
}
foreach ($_POST['ordem'] as $key => $value){
$sql = "UPDATE categorias SET ordem='$value' WHERE idcategorias='$gosma'";
$res = mysql_query($sql) or die ("Erro ao alterar") . mysql.error();
}
if($res) {
echo("<script>alert('DADOS ALTERADOS COM SUCESSO ');</script>");
echo "<meta HTTP-EQUIV='refresh' CONTENT='1;URL=lista_categorias.php'>";
}
else{
echo("<script>alert('ORDEM INALTERADA, TENTE NOVAMENTE');</script>");
echo "<meta HTTP-EQUIV='refresh' CONTENT='1;URL=altera_ordem_categoria.php'>";
}
?>
AND THE FORM IS HERE
<form action="altera_ordem_cat.php" method="post">
<?php
include "conecta.php";
include "verifica.php";
$sql = "SELECT * FROM categorias ORDER BY ordem";
$res = mysql_query($sql) or die (mysql_error());
while ($linha=mysql_fetch_array($res)) {
$linha[2] = $real;
?>
<tr>
<td><?=$linha['2']; echo " <input name='ordem[]' type='text' id='textfield' size='2' maxlength='2' value=''/>" ?></td>
<td><?=$linha['1']; ?></td>
</tr>
<?php }?>
</table>
<br/>
<table align="center">
<tr>
<td>
<input type="submit" name="alterar" type="button" value="Alterar">
</td>
</tr>
</table>
</form>
Your while cycle overwrites the
$roweach time. Don’t you mean to do this?You should always escape before inserting into a query:
You should put the mysql_error inside the die function (and don’t use dot in the name)
What is it you’re trying to do in this cycle?
Don’t you want something like this?
EDIT:
And afterwards: