繁体   English   中英

使用 PHP 时从 MySQL 检索的数据未显示

[英]Data retrieved From MySQL not showing when using PHP

我正在开发这个程序,它从用户那里获取一些输入,然后使用该输入从 MySQL 数据库中的几个表中检索数据。 我的问题是,我似乎没有从 MySQL 表中得到任何东西,如果您能提供帮助,那就太好了。

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";

?>

表格.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>

当我尝试“回显”我应该从数据库中获取的内容时,什么都没有出现,我猜这个问题可能与 mysqli_query() 的 $conn 有问题有关。

谢谢你们!

您需要更改您的form.php文件,这就是我的建议

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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