不了解mysqli_fetch_array

时间:2014-08-24 10:19:26

标签: php arrays mysqli

所以我想我不明白这一点我是编码的新手。我已经搜索过,无法找到一个好的enogh解释,让它在我的位置上工作。我需要从fetch命令填充表,然后使用信息输入将每个结果更新到新列中。这是我的代码:

这填补了表格:     

    echo "<table border='1'>
    <tr>
    <th>Envelope</th>
    <th>Budget</th>
    <th>Amount</th>
    </tr>";

    while($row = mysqli_fetch_array($result)) {

    echo "<tr>";
    echo "<td>" . $row['envelopename'] . "</td>";
    echo "<td>" . $row['envelopebudget'] . "</td>";
    ?><td><input type="text" name="budgetamount"></td><?php;
    echo "</tr>";
    }

    echo "</table>";
?>  
    <input type="submit">

这会将budgetname的输入写入列budgetname:

$paycheckname = mysqli_real_escape_string($con, $_POST['paycheckname']);
$budgetamount = mysqli_real_escape_string($con, $_POST['budgetamount']);
$envelopename = mysqli_real_escape_string($con, $_POST['envelopename']);
}

$sql="UPDATE envelopes SET $paycheckname='$budgetamount' WHERE envelopename ='$envelopename'";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}

现在它只是没有写任何东西。我使用echo var_dump($envelopename)."<br>"; and echo var_dump($budgetamount)."<br>";来尝试看看它在做什么,但$ envelopename总是空白的。感谢您的任何帮助,您可以提供。

2 个答案:

答案 0 :(得分:0)

只有<input><textarea><select><button>以表格

提交给服务器

如果要重新提交一些静态值,请创建一些隐藏的输入

while($row = mysqli_fetch_array($result)) {

echo "<input type='hidden' value='$row[envelopename]' name='envelopename'/>";
echo "<input type='hidden' value='$row[envelopebudget]' name='envelopebudget'/>";
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount"></td><?php;
echo "</tr>";
}

echo "</table>";

但是你的下一个问题是你要在循环中创建元素,这样你就可以拥有多个同名的输入

<tr>
    <input type='hidden' value='envName1' name='envelopename'/>
    <input type='hidden' value='envBudget1' name='envelopename'/>
    <td>envName1</td>
    <td>envBudget1</td>
    <td><input type="text" name="budgetamount"></td>
</tr>
<tr>
    <input type='hidden' value='envName2' name='envelopename'/>
    <input type='hidden' value='envBudget2' name='envelopename'/>
    <td>envName2</td>
    <td>envBudget2</td>
    <td><input type="text" name="budgetamount"></td>
</tr>

所以你必须提交数组

while($row = mysqli_fetch_array($result)) {

echo "<input type='hidden' value='$row[envelopename]' name='envelopename[]'/>";
echo "<input type='hidden' value='$row[envelopebudget]' name='envelopebudget[]'/>";
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount[]"></td><?php;
echo "</tr>";
}

echo "</table>";

并在服务器端处理为数组

 foreach ($_POST['budgetamount'] as $budgetamount){
       echo $budgetamount. '<br>';
  }

答案 1 :(得分:0)

假设呈现的表单如下所示:

<tr>
    <input type='hidden' value='envName1' name='envelopename[]'/>
    <input type='hidden' value='envBudget1' name='envelopebudget[]'/>
    <td>envName1</td>
    <td>envBudget1</td>
    <td><input type="text" name="budgetamount[]"></td>
</tr>
<tr>
    <input type='hidden' value='envName2' name='envelopename[]'/>
    <input type='hidden' value='envBudget2' name='envelopebudget[]'/>
    <td>envName2</td>
    <td>envBudget2</td>
    <td><input type="text" name="budgetamount[]"></td>
</tr>
<tr>
    <input type='hidden' value='envName3' name='envelopename[]'/>
    <input type='hidden' value='envBudget3' name='envelopebudget[]'/>
    <td>envName3</td>
    <td>envBudget3</td>
    <td><input type="text" name="budgetamount[]"></td>
</tr>

当用户点击提交时,到达$_POST的{​​{1}}将如下所示:

newpaycheck.php

所以你可以这样做:

$_POST
['envelopename']{
   [0]=>'envName1',
   [1]=>'envName2',
   [2]=>'envName3'
},['envelopebudget']{
   [0]=>'envBudget1',
   [1]=>'envBudget2',
   [2]=>'envBudget3'
},['budgetamount']{
   [0]=>'someValueEnteredByUser',
   [1]=>'anotherValueEnteredByUser',
   [2]=>'yetAnotherValueEnteredByUser'
}