无法使用$ _post数据更新mysql表

时间:2013-08-07 03:28:40

标签: php mysql

这是来自数据库的页面显示表,每行都有文本框,b_id更新表。

$result2 = mysqli_query($con,"SELECT * FROM zahialga WHERE bid IN ($imp)");
    while($row = mysqli_fetch_array($result2))
      {
      echo "<tr>";
      echo "<td>" . $row['b_id'] . "</td>";
      echo "<td>" . $row['materials'] . "</td>";
      echo "<td>" . $row['num'] . "</td>";
      echo "<td> <input type='text' name='sh_num[]' maxlength='10'></td>";
      echo "</tr>";
      echo "<input type='hidden' name='b_id[]' value='" . $row['bid'] . "'>";
      }
      echo "</table>";
    echo "<input type='submit' value='Илгээх'/>";

这是更新页面。

$con=mysqli_connect("localhost","root","","login");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$sh_id_query = mysqli_query($con,"SELECT sh_id FROM zahialga order by bid desc limit 1");
$sh_id1 = $sh_id_query->fetch_object()->sh_id;
$sh_id2 = $sh_id1 + 1;
$count = count(array_filter($_POST["sh_num"]));
for($i=0;$i<$count;$i++)
{
$insert = mysqli_query($con,"UPDATE table SET 
                    sh_id='{$sh_id2}', 
                    sh_num='{$_POST['sh_num'][$i]}'
WHERE bid = '{$_POST['$b_id'][$i]}'");
}

这是getiing Notice: Undefined index: $b_id错误。我错过了什么? BTW抱歉我的英语不好。

2 个答案:

答案 0 :(得分:1)

$insert = mysqli_query($con,"UPDATE table SET 
                    sh_id='{$sh_id2}', 
                    sh_num='{$_POST['sh_num'][$i]}'
WHERE bid = '{$_POST['$b_id'][$i]}'");
//                    ^ additional $ sign here

应该是

$insert = mysqli_query($con,"UPDATE table SET 
                    sh_id='{$sh_id2}', 
                    sh_num='{$_POST['sh_num'][$i]}'
WHERE bid = '{$_POST['b_id'][$i]}'");

答案 1 :(得分:1)

应该没有$ sign:

$_POST['b_id'][$i]