0

after I managed to connect my website form to my database, I decided to try to transfer over my files to my work computer.

Initially I only had one error: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in...

However now I get an extra mysqli_fetch_row() error the same as above but the error is on a different line.

Additionally I also get the error: Undefined index: fill which I never got before. Are there any mistakes in my code? The form still works and can connect to my database.

<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>
PHP Scrub
  • 171
  • 3
  • 17
  • 1
    You've mixing object and functional notation. That's asking for problems. Also, I've seen that error message a couple of times today already. Please search a little before asking a question. – GolezTrol Oct 16 '15 at 23:18
  • 2
    Possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource (or mysqli\_result), boolean given](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole) – Elias Nicolas Oct 16 '15 at 23:18

2 Answers2

0

You're getting that error because you use $_POST['fill'] without checking whether it's set first. It will only be set when the form is submitted, not when the form is first displayed. You need to put all the code that processes the form input into:

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

BTW, you can do that entire update in a single query.

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'
Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

Like GolezTrol said from his comment. You're mixing object and functional notation.

Although this might not work exactly how you need it to because I don't have all the information. I have written you something I think is close to what you're looking for.

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