简体   繁体   中英

Data retrieved From MySQL not showing when using PHP

I am working on this program that gets some inputs from the user and then uses that input to retrieve data from a few tables in a MySQL database. My issue is that it seems like I am not getting anything from the MySQL tables, and it would be great if you could help.

dbConn.php

<?php
$ser = "localhost";
$user = "root";
$pass = "pass";
$db = "dbvisual";

$conn = mysqli_connect($ser, $user, $pass, $db) or die("connection failed");
echo "connection success";

?>

form.php

<?php 

if(isset($_Post['submit'])){    // Checking to see if the form has been submitted
    $battery = (int) $_POST['battery']; // Battery Input element
    $cycle = (int) $_POST['cycle'];   // Cycle input element
    $xVar = $_POST['X'];    // X variable
    $yVar = $_POST['Y'];    // Y variable

    // Trying to get the x variable based on what the user said
    $mySqlQueryX = "SELECT cap 
                    FROM cycle 
                    WHERE cycleID = $cycle";

    $feedbackX = mysqli_query($conn, $mySqlQueryX);
    echo "<h1>$feedbackX</h1>";

}
?>

index.php

<!-- Connecting to the database -->
<?php
include 'dbConn.php';
?>

<!-- The Styling of the website -->
<style>
    <?php include 'main.css'; ?>
</style>

<!-- The Skeleton of the website -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php include "form.php" ?>
    <!-- The entire website -->
    <div id="body">
        <!-- The input half -->
        <div id="inputs">
            <!-- The input form -->
            <form action="index.php" method="POST">
                <div id="form">
                    <!-- Labels -->
                    <div id="label">
                        <label for="">Which Batteries?</label>
                        <label for="">What Cycles?</label>
                        <label for="">What's X?</label>
                        <label for="">What's Y?</label>
                        <label for="">Discharge?</label>
                        <label for="">Charge?</label>
                    </div>
                    <!-- User Info -->
                    <div id="info">
                        <input type="text" placeholder="Batteries" required 
                        oninvalid="this.setCustomValidity('No Batteries Were Entered')"
                        oninput="setCustomValidity('')" name="battery">

                        <input type="text" placeholder="Cycles" required 
                        oninvalid="this.setCustomValidity('No Cycles Were Entered')"
                        oninput="setCustomValidity('')" name="cycle">

                        <select name="X">
                            <option value="cap">Capacity</option>
                            <option value="speCap">Specific Capacity</option>
                            <option value="voltage">Voltage</option>
                        </select>

                        <select name="Y">
                            <option value="cap">Capacity</option>
                            <option value="speCap">Specific Capacity</option>
                            <option value="voltage">Voltage</option>
                        </select>

                        <input type="checkbox" name="discharge" checked>
                        <input type="checkbox" name="charge" checked>
                    </div>
                </div>
                <!-- Submit Button -->
                <div>
                    <button type="submit" name="submit">Submit</button>
                </div>
            </form>
        </div>
        <!-- The graph half -->
        <div id="graph">
            <p>hello</p>
        </div>
    </div>
</body>
</html>

When I try to "echo" what I am supposed to get from the database, nothing shows up and I am guessing the issue could be related to mysqli_query() having problems with $conn.

Thank you all!

You need to change your form.php file this is what I propose

 <?php
 if(isset($_POST['submit'])){
    $battery = (int) $_POST['battery']; // Battery Input element
    $cycle = (int) $_POST['cycle'];   // Cycle input element
    $xVar = $_POST['X'];    // X variable
    $yVar = $_POST['Y'];
$con = mysqli_connect("localhost","root","passer","arduino");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit();
} 
$mySqlQueryX = "SELECT cap 
                    FROM cycle 
                    WHERE cycleID = '".$cycle."' ";
$result = mysqli_query($con, $mySqlQueryX);

// Fetch all
print_r(mysqli_fetch_all($result, MYSQLI_ASSOC)); 

// Free result set
mysqli_free_result($result);

mysqli_close($con);
}

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