切换计算机并获得两个新的mysqli_fetch_row错误。

时间:2015-10-16 23:14:56

标签: php forms mysqli

在我设法将我的网站表单连接到我的数据库之后,我决定尝试将我的文件传输到我的工作计算机。

最初我只有一个错误:mysqli_fetch_row()期望参数1为mysqli_result,boolean在...中给出

但是现在我得到了一个与上面相同的额外mysqli_fetch_row()错误,但错误是在另一行。

另外我也得到错误:未定义的索引:填充我以前从未得到的。我的代码中有错误吗?表单仍然有效,可以连接到我的数据库。

<center><form action="fill.php" method="post">
        Fill
        <input type="text" id="fill"" name="fill">

    <input type="submit" id ="submit" name="submit" value="Submit here!">
</form></center>

</div>

<?php

$val1 = $_POST['fill'];


$conn = mysqli_connect('localhost', 'root', '')or 
die("Could not connect");

mysqli_select_db($conn, 'rfid');

$val2 = "SELECT * FROM card_refill WHERE refill = $val1";

$result1= $conn->query($val2);

$row = mysqli_fetch_row($result1);

$refill1 = $row[2];


$value = "SELECT *FROM card_credit ORDER BY id DESC LIMIT 1:";

$result = $conn->query($value);

$row = mysqli_fetch_row($result);

$refill = $row[2];

$money= $refill+$refill1;

echo $money;

$sql = "UPDATE card_credit SET value = '$money'";

if ($conn->query($sql) === TRUE) {

    echo "Success";
} 
else {
    echo "Warning: " . $sql . "<br>" . $conn->error;
}
mysqli_close($conn);
?>

</body>

</html>

2 个答案:

答案 0 :(得分:0)

您收到该错误是因为您使用$_POST['fill']而未检查是否先设置了该错误。它仅在提交表单时设置,而不是在首次显示表单时设置。您需要将处理表单输入的所有代码放入:

if (isset($_POST['submit'])) {
    ...
}

顺便说一句,您可以在一次查询中完成整个更新。

UPDATE card_credit AS cc
CROSS JOIN card_refill AS cr
CROSS JOIN (SELECT * FROM card_credit ORDER BY id DESC LIMIT 1) AS cc1
SET cc.value = cr.col2 + cc1.col2
WHERE cr.refill = '$val1'

答案 1 :(得分:-1)

像GolezTrol一样,他的评论说。你正在混合对象和功能表示法。

虽然这可能无法完全满足您的需求,因为我没有所有信息。我写了一些我认为接近你正在寻找的东西。

<?php 
    // Define the below connections via $username = ""; EXTRA....
    // This is best done in a separate file.

    $conn = new mysqli($servername, $username, $password, $dbname);

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

    $val1 = $_POST['fill'];
    $result1 = $conn->query("SELECT * FROM card_refill WHERE refill = '$val1' ");
    $result2 = $conn->query("SELECT * FROM card_credit ORDER BY id DESC LIMIT 1:");

    $refill1 = array(); // Pass Results1 Into Array
    while($row = $result1->fetch_assoc()) {
        $refill1[] = $row[2];
    }

    $refill = array(); // Pass Results2 Into Array
    while($row = $result2->fetch_assoc()) {
        $refill[] = $row[2];
    }

    /* Without an example of what data you are getting from your tables you will have to figure out what data you want from the arrays.
        $money= $refill+$refill1;
        echo "DEBUG: $money";
    */


    // This code will not be functional until your populate the $money value.
    $sql = "UPDATE card_credit SET value = '$money' ";

    if ($conn->query($sql) === TRUE) {
        echo nl2br("Record updated successfully"); // DEBUG
        print_r(array_values($refill1)); // DEBUG
        print_r(array_values($refill)); // DEBUG
        echo nl2br("\n"); // DEBUG
    } else { // DEBUG
        echo "Error updating record: " . $conn->error; // DEBUG
        echo nl2br("\n"); // DEBUG
    }

    $conn->close();
?>