使用循环mysqli的php语句有问题 [英] Having a issue with php statement using loop mysqli

查看:39
本文介绍了使用循环mysqli的php语句有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,如果我没有问这个权利,请原谅我.我有以下代码.

Hello Please forgive me if I'm not asking this right. I have the following code.

      <?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 $ row ['Task $ Task'];中.这样值就变成task1,然后是task2,task3等等.最多可以执行15个任务.我很接近,只是不确定我在哪里搞砸.任何帮助将是巨大的.现在被卡在这上面了一段时间.预先谢谢你!

What I am trying to do is add the $task value to the echo $row['Task$Task']; So that the value turns to task1 then task2 , task3 ect. There can be up to 15 tasks. I'm close just not sure on where I'm messing up. Any help would be great. been stuck on this one for awhile now. Thank you in advance!!

好的,这就是我几乎正确显示的内容.

OK now this is what I have its displaying almost correctly.

$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();
?>

现在它正在循环,但是,如果任务为空,则无需回显单选按钮并在其结束处停止. 捕获.谢谢大家的超赞!如何添加if语句,可以查看$ row ['Task'.$ Task];为null,然后停止循环吗?

Now it is looping however, If the task is empty I need to not echo the radio buttons and stop where it ends. capture. Thank you guys your all awesome!! How do I add a if statement thst can see if the $row['Task'.$Task]; is null then stop the loop?

推荐答案

注意(这仅是示例,容易发生sql注入,应检查 如何使用 PDO

Note(This is just for example and prone to sql injection you should check how to Use PDO's and Sanitize 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();
?>

这篇关于使用循环mysqli的php语句有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆