使用循环mysqli

时间:2017-09-20 17:09:28

标签: php mysqli

你好请原谅我,如果我没有问这个问题。我有以下代码。

      <?php

      //Connect to mysql server
    include ("Data.php");
    if (!$con) {
    die ("connection error". mysqli_connect_error());
    }

$sql1 = "SELECT * FROM PMList where AssetNum= '$AssetNum' and Plant= '$Plant';";
    $result = mysqli_query($con, $sql1) or die(mysqli_error($con));

if ($result->num_rows > 0)
$count = 0;
$Task = 1;
while($row = mysqli_fetch_array($result))
{
            $Task++;
            echo "<td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task$Task'];
            echo " </font></td>";
            echo "<td bgcolor='#D8D8D8'><input type='radio' name='Task$Task' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task$Task' value='No'>No";
            echo "<tr border='0'>";
            }
    while ($count++ < 16) {
}
        $con->close();
?>

我要做的是将$ task值添加到echo $行[&#39; Task $ Task&#39;];这样值变为task1然后task2,task3等。最多可以有15个任务。我不知道我在哪里搞砸了。任何帮助都会很棒。现在已经坚持了一段时间。提前谢谢!!

现在好了,这就是我几乎正确显示的内容。

$sql1 = "SELECT * FROM PMList where AssetNum= '$AssetNum' and Plant= '$Plant';";
    $result = mysqli_query($con, $sql1) or die(mysqli_error($con));

if ($result->num_rows > 0)
$count = 0;
$Task = 1;

while($row = mysqli_fetch_assoc($result))
do
{
            echo "<td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task'.$Task];
            echo " </font></td>";
            echo "<td bgcolor='#D8D8D8'><input type='radio' name='Task$Task' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task$Task' value='No'>No";
            echo "<tr border='0'>";
            $Task++;
            }
    while ($count++ <= 13);
        $con->close();
?>

现在它正在循环,但是如果任务是空的,我不需要回显单选按钮并停止它结束的位置。 capture。谢谢你们,你们真棒!如何添加if语句以查看$ row [&#39; Task&#39;。$ Task];是null然后停止循环?

2 个答案:

答案 0 :(得分:0)

您应该使用"使用变量生成字符串或使用.点进行连接,

    echo $row["Task$Task"];

  echo $row['Task'.$Task];

答案 1 :(得分:0)

注意(这只是例如,你应该检查sql注入倾向 如何使用PDO'sSanitize php input

<?php

//Connect to mysql server
include ("Data.php");
if (!$con) {
die ("connection error". mysqli_connect_error());
}

$sql1 = "SELECT * FROM PMList where AssetNum= '$AssetNum' and Plant= '$Plant';";
    $result = mysqli_query($con, $sql1) or die(mysqli_error($con));

if ($result->num_rows > 0)
$count = 0;
$Task = 1;
$rows = mysqli_fetch_array($result)
foreach($rows as $row )
{
            $Task++;
            echo "<td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row["Task".$row["id"]];
            echo " </font></td>";
            echo "<td bgcolor='#D8D8D8'><input type='radio' name='$row["Task".$row["id"]]' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='$row["Task".$row["id"]]' value='No'>No";
            echo "<tr border='0'>";
            }
        $con->close();
?>

<?php

//Connect to mysql server
include ("Data.php");
if (!$con) {
die ("connection error". mysqli_connect_error());
}

$sql1 = "SELECT * FROM PMList where AssetNum= '$AssetNum' and Plant= '$Plant';";
    $result = mysqli_query($con, $sql1) or die(mysqli_error($con));

if ($result->num_rows > 0)
$count = 0;
$Task = 1;
while($row = mysqli_fetch_assoc($result))
{
            $Task++;
            echo "<td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row["Task".$row["id"]];
            echo " </font></td>";
            echo "<td bgcolor='#D8D8D8'><input type='radio' name='$row["Task".$row["id"]]' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='$row["Task".$row["id"]]' value='No'>No";
            echo "<tr border='0'>";
            }
        $con->close();
?>