简体   繁体   中英

I know my table contains rows but my query returns a result of 0. How do I fix this?

This is for a school assignment and I just cannot figure out where I am going wrong. Some assistance would be greatly appreciated. My code is a mix of PHP and HTML that utilizes a form to input entries into my table in my database. The form works and entries are being entered, but when I execute the return entries function to view all the entries in my table, nothing comes back.

Here is the code I'm working with so far:

<?php

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

    //getting timezone data for registration timestamp
    $timezone = date_default_timezone_set('America/New_York');

    //define username and passowrd according to what user entered
    $userFName = $_POST['fname'];
    $userLName = $_POST['lname'];
    $userCity = $_POST['city']; 
    $userEmail = $_POST['email'];
    $regDate = date(format,timestamp);

    // Establishing Connection with Server by passing server_name, user_id and password as a parameter
    $conn = mysqli_connect("localhost", "root", "root");
    //$conn2 = mysqli_connect("localhost", "root", "root", "progAssignment2");

    //to protect mySQL injection for Security purposes
    $userFName = stripslashes($userFName);
    $userFName = mysqli_real_escape_string($conn, $userFName);      
    $userLName = stripslashes($userLName);
    $userLName = mysqli_real_escape_string($conn, $userLName);
    $userCity = stripslashes($userCity);
    $userCity = mysqli_real_escape_string($conn, $userCity);    
    $userEmail = stripslashes($userEmail);
    $userEmail = mysqli_real_escape_string($conn, $userEmail);

    //select my desired database
    $db = mysqli_select_db($conn, 'progAssignment2');   

    //if user submits form, insert new data into table and echo success
    $sql = "INSERT INTO MyGuests (firstname, lastname, city, email, reg_date) 
        VALUES ('$userFName', '$userLName', '$userCity', '$userEmail', '$regDate');";

    if (mysqli_query($conn, $sql)) {
        echo "Your form has been successfully submitted!";
    } else {
        echo "Error updating record: " . mysqli_error($conn);
    };

    //selecting data from mySQL database
    $query = "SELECT * FROM MyGuests";
    $result = mysqli_query($conn, $query);

};
?>

<!DOCTYPE html>
<html>
<head>
<title>Programming Assignment 3 - Isaiah Duncan</title>
</head>
<body>

<header></header>

<nav></nav>

<section>

    <h1>Insert Data Form</h1>

    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">

        First Name:<br>
        <input type="text" name="fname" value="" required/><br><br>

        Last Name:<br>
        <input type="text" name="lname" value="" required/><br><br>

        City:<br>
        <input type="text" name="city" value="" required/><br><br>

        Email:<br>
        <input type="text" name="email" value="" required/><br><br>

        <input type="submit" name="submit" value="Submit" />

    </form>

</section>

<section>

    <h1>Results</h1>

    <?php

    //check if (more than zero) rows are returned. if so, loop through and display
    if (mysqli_num_rows($result) > 0) {
        //output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            echo "id: " . $row["id"] . " - FirstName: " . $row["firstname"] . " - LastName: " 
                . $row["lastname"] . " - City: " . $row["city"] . " - Email: " . $row["email"] . "<br>";
        };
    }else{
        echo "0 results" . mysqli_error($conn);
    };

    ?>

    <?php echo "There are " . mysqli_num_rows($result); ?>

</section>

<?php mysqli_close($conn); ?>

</body>
</html>

From what I can understand, I am thinking that you want to see all the records, that you previously inserted when you first visit the page. If that is the case, you will need to get your $result outside of your if statement. That if statement will only execute when you submit the form. So for example, outside of the if statement (right below it), type this:

 $conn = mysqli_connect("localhost", "root", "root");
 $db = mysqli_select_db($conn, 'test');
 $query = "SELECT * FROM MyGuests";
 $result = mysqli_query($conn, $query);

That should run the above query every time you visit the page. If there are any results, it should list it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM