简体   繁体   English

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

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

dbConn.php 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

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

Thank you all!谢谢你们!

You need to change your form.php file this is what I propose您需要更改您的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.

相关问题 在更新后直接检索记录时,PHP页面未显示来自MySQL的更新数据 - PHP page not showing updated data from MySQL when record is retrieved directly after being updated 显示使用php从mysql检索的内容的特殊字符 - Special characters showing up on content retrieved from mysql using php 如何使用php从mysql数据库中分页检索到的数据 - how to paginate the retrieved data from mysql database by using php 如何使用php从mysql检索数据并在javascript上显示? - How to retrieved data from mysql using php and show it on javascript? 如何使用php从mysql数据库中分页检索到的数据? - how to paginate the retrieved data from mysql database by using php? 从数据库中检索时,PHP显示奇怪的条目 - PHP-showing weird entries when retrieved from database PHP MYSQL如果没有检索到数据,则从数据库中获取数据并回显消息 - PHP MYSQL Get data from database and echo message if no data retrieved 如何使用php以特定顺序显示从mysql db检索到的数据 - How do I display data retrieved from mysql db in a specific order using php 查询包含变量时未检索PHP / Javascript + MySQL数据 - PHP/Javascript + MySQL data is not being retrieved when query has variables 如何使用 php 将检索到的 MySQL 数据格式化为 JSON 格式 - How to format the retrieved MySQL data in JSON format using php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM