【问题标题】:Update mysql database fields with array php使用数组 php 更新 mysql 数据库字段
【发布时间】:2013-02-19 01:35:06
【问题描述】:

我正在尝试在一次提交中实现多次更新。我有一个包含许多行的表,并且希望能够通过切换到下一个插入框来更新每一行中的一个字段。

我的代码是这样的:- //开始一个表 回声' ';

//start header of table
echo '<tr>

<td width="60" align="center"><strong>Lab Item ID</strong></td>
<td width="60" align="center"><strong>Test Suite Name</strong></td>
<td width="60" align="center"><strong>Test Name</strong></td>
<td width="50" align="center"><strong>Result</strong></td>
</tr>';

//loop through all results
while ($row=mysql_fetch_object($sql)) {
    //print out table contents and add id into an array and email into an array
    echo '<tr>
    <td align="center"><input name="id[]" value='.$row->lab_item_id.' readonly> </td>
    <td align="center">'.$row->test_suite_name.'</td>
    <td align="center">'.$row->test_name.'</td>
    <td><input name="test_result[]" type="text" value="'.$row->test_result.'"></td>
    </tr>';
}

//submit the form

echo'<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>';
//if form has been pressed proccess it
if($_POST["Submit"])
{

//get data from form
//$name = $_POST['name'];

//$_POST['check_number'] and $_POST['check_date'] are parallel arrays

foreach( $_POST['id'] as $id ) {
     $tresult = trim($_POST['test_result']);
      $query = "UPDATE tbl_lab_item SET test_result='$tresult' WHERE lab_item_id = '$id'";
      //execute query

}  
 print_r($_POST);
 var_dump($tresult);
//redirect user
$_SESSION['success'] = 'Updated';
//header("location:index.php");
}
?>

当我打印 $_POST 数组时,一切正常,但变量为 Null。我知道我不能在多个数组上做一个 foreach (至少我不认为我可以)所以还有其他一些我想念的技巧吗?我不能走得太远,因为 $_Post 打印中包含正确的数据。

顺便说一句,整个事情都是由查询生成的,所以我永远不知道我需要更新多少条记录。

我一直在查看这个(和其他)论坛,但似乎无法找到解决方案。我以为我理解数组,但现在我开始怀疑了!

edit - 这是 $tresult 变量不起作用。

非常感谢, 杰森

编辑 2 月 21 日星期四(英国时间 05:41) 谢谢大家的意见。我现在已经解决了这个问题,您的集体建议有所帮助。最终破解的代码是:-

//get data from form
$id1 = $_POST['id'];
$test_result1 = $_POST['test_result'];
foreach ($id1 as $key => $value){
$query = "UPDATE tbl_lab_item SET test_result='$test_result1[$key]' WHERE lab_item_id=$value ";
//execute query

研究填充了哪些变量等以及填充了什么是关键。回到第一原则,不是吗?

大家干杯。 J

【问题讨论】:

  • 哪个the variable?你的代码中有很多变量。
  • 抱歉,说得好。这是我试图在更新查询中更新的 test_result。

标签: php mysql arrays


【解决方案1】:

实际上,您可以通过执行更简单(基本)形式的 for 循环来完成它:

//get data from form
//$name = $_POST['name'];

//$_POST['check_number'] and $_POST['check_date'] are parallel arrays

$numberOfData = count($_POST['id']);

for($index = 0; $index < $numberOfData; $index++)
{
     $id = $_POST['id'][$index];
     $tresult = trim($_POST['test_result'][$index]);
      $query = "UPDATE tbl_lab_item SET test_result='$tresult' WHERE lab_item_id = '$id'";
       //execute query

}
 print_r($_POST);

我希望这会有所帮助。

干杯

【讨论】:

  • 谢谢你。我可以看到其中的逻辑,但它仍然没有向数据库中插入任何内容。 $tresult 的 vardump 给出了最后一个值,所以看起来它正在运行。
  • 嗯...很可能它可能与查询本身有关。您是否在 SQL 编辑器/执行器上尝试过?要考虑的是,lab_item_id 是字符串还是数字。如果它是一个数字,则不应有一对单引号将其括起来。另一方面,如果它是一个字符串,您可能还想像对待 test_result 一样使用 trim。
  • 输入的结果永远只是一个数字,我试过带引号和不带引号,尽管在两种情况下都可以测试。我会检查 SQL,但如果没记错的话,我昨天确实在 phpmyadmin 中尝试过,它确实有效。老实说,我尝试了很多东西,它们都开始变得模糊 :)
【解决方案2】:

像这样更改查询:

  $query = "UPDATE tbl_lab_item SET test_result=$tresult WHERE lab_item_id = $id";

通过添加单引号 ' ' 你告诉它读取 is 作为一个字符串而不是获取 var 的值。

编辑

用以下内容替换你的 foreach 循环并告诉我:

$id1 = $_POST['id'];
$test_result1 = $_POST['test_result'];

foreach( $id1 as $key => $value ) {
  $query = "UPDATE tbl_lab_item SET test_result='$test_result1[$key]' WHERE lab_item_id = '$key' ";

}   

【讨论】:

  • 嗨,谢谢。我已经尝试过了,但它也不起作用。当我转储变量 $tresult 时,它为 NULL,因此出于某种原因我没有收集内容。当我查看 $_POST 数组时,我需要的一切都在那里,只是不会进入 $tresult 变量。
  • 嗨。您可以尝试 echo $_POST['test_result']; foreach 循环内的第一行,看看它显示的结果是否正确?
  • 在答案中进一步查看上面的编辑。我添加了要测试的新代码
  • 您好 t0s,谢谢您,我试过了,但写入数据库仍然没有乐趣。我将 vardump 更改为更新的变量,但它显示了最后一个循环的正确数据。我现在尝试回显 $_POST['test_result']
  • 好的,我尝试了两件事。首先 $_POST['test_result'] 似乎没有输出任何东西,但是 $testresult1 的 var_dump 给出了最后输入的值。 $key 的 var_dump 表明它正在按预期循环。
【解决方案3】:

问题是您告诉 PHP 将输入字段构建为数组,但稍后将其视为字符串:

<td><input name="test_result[]" type="text" value="'.$row->test_result.'"></td>
                            ^^--- array

 $tresult = trim($_POST['test_result']);
                 ^^^^^^^^^^^^^^^^^^^^^--- no array key, so you're assigning the entire array

  $query = "UPDATE tbl_lab_item SET test_result='$tresult'
                                                ^^^^^^^^^^--- array in string context

trim() 需要一个字符串,但你传入一个数组,所以你会得到一个 PHP NULL 和一个警告。然后那个 null 会被填充到你的 SQL 语句中,这就是你的问题。

【讨论】:

  • 太好了,谢谢。我知道我一定一直在做类似的事情,但看不到是什么。有没有办法解决它?我尝试在 foreach 循环中使用数组,但不知道正确的语法,我应该以某种方式在循环之外进行吗?
猜你喜欢
  • 2015-02-20
  • 2014-05-08
  • 2014-04-04
  • 2014-01-04
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
相关资源
最近更新 更多