不执行PHP脚本中的循环

时间:2017-08-31 18:04:22

标签: php html

我有一个表单,我想在表单输入标记中显示invoice_id。并且发票ID是从php脚本计算的,然后由一个变量进一步传递给java脚本,以获得输入的值等于传递的变量。但我不知道我在哪里犯了错误,我得到的值是在PHP代码中的else条件中定义的,不知道为什么php代码循环不会执行。我的桌子也有两排。 php代码是:

 <?php
$conn = new mysqli("localhost","root","","tssolutions");

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sqlinvoice = "SELECT `invoiceid` FROM `transactiona` ORDER by `invoiceid` DESC LIMIT 1";
$resultinvoice = $conn->query($sqlinvoice);
    echo "<br><br><br><br><br><br><br><br><br><br><br><br>";
if ($resultinvoice->num_rows != 0) {
    // output data of each row
    while($row = $resultinvoice->fetch_assoc()) {
        echo $row['invoiceid'];
    $row['invoiceid'] +=1;
echo $row['invoiceid'];

        echo "      <script>        var invoid = ".$row['invoiceid']."      </script>";
    }
}
 else {
echo "      <script>        var invoid = '1';       </script>";
}
$conn->close();
?>

HTML代码是:

<form action="./manipulate/invoice.php" method="post" accept-charset="UTF-8">
<div style="width:15%;float:left">
Invoice #
<br>
<input style="width:98%;margin-left:1%;margin:right:1%" type="text" placeholder="Invoice ID" id="lmn">
</div>
</form>
<script>
    $(document).ready(function () {
        $("#lmn").val(invoid);
});
</script>

1 个答案:

答案 0 :(得分:0)

mysql_fetch_object()返回一个对象,而不是一个对象数组。所以我要做的就是添加$ row = array();顺便说一句,感谢帮助我找到解决方案的所有人。

if ($resultinvoice->num_rows > 0) {
    // output data of each row
    $row = array();
    while($row = $resultinvoice->fetch_assoc()) {
    $row['invoiceid'] +=1;

        echo "      <script>        var invoid = ".$row['invoiceid']."      </script>";
    }
}
 else {
echo "      <script>        var invoid = '1';       </script>";
}