PHP循环不会工作

时间:2012-01-28 15:52:52

标签: php mysql foreach while-loop

我试图从带有数组的表单中获取值我需要从bd获取id来更新,但它不起作用,也许你给我一个想法,我会发布代码,所以你可能会理解。提前感谢您的帮助!

<?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'>";
}

?>

表格就在这里

<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 "&#32;<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>  

2 个答案:

答案 0 :(得分:1)

您的while循环每次都会覆盖$row。你不是故意这样做吗?

$gosma = array();
while ($row=mysql_fetch_array($res)) {
    $gosma[] = $row["idcategorias"];
}

在插入查询之前,您应该始终转义:

$sql = "UPDATE categorias SET ordem='".mysql_real_escape_string($value)."' WHERE idcategorias='".mysql_real_escape_string($gosma)."'";

你应该将mysql_error放在die函数中(并且不要在名称中使用dot)

$res = mysql_query($sql) or die ("Erro ao alterar: ".mysql_error());

你在这个循环中试图做什么?

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

你不想要这样的东西吗?

foreach ($_POST['ordem'] as $key => $value) {   
    $sql = "UPDATE categorias SET ordem='".mysql_real_escape_string($value)."' WHERE idcategorias='".mysql_real_escape_string($key)."'";
    $res = mysql_query($sql) or die ("Erro ao alterar: ".mysql_error());
}


修改

<form action="altera_ordem_cat.php" method="post">
  <table>
<?php
include "conecta.php";
include "verifica.php";
$sql = "SELECT idcategorias, ordem FROM categorias ORDER BY ordem";
$res = mysql_query($sql) or die (mysql_error());

while ($linha = mysql_fetch_array($res)) {
?>
  <tr>
    <td><input name='ordem[<?php echo $linha[0]; ?>]' type='text' id='textfield' size='2' maxlength='2' value='<?php echo $linha[1]; ?>'/></td>
    <td></td> 
</tr>
<?php
}
?>
  </table>
<!-- ........ -->

之后:

<?php

include "conecta.php";
include "verifica.php";

foreach ($_POST['ordem'] as $key => $value) {   
  $sql = "UPDATE categorias SET ordem='".mysql_real_escape_string($value)."' ".
         "WHERE idcategorias='".mysql_real_escape_string($key)."'";
  $res = mysql_query($sql) or die ("Erro ao alterar: ".mysql_error());
}

答案 1 :(得分:0)

$gosma[] = $row["idcategorias"]; instead of $gosma = $row["idcategorias"];