将MySQL数据发布到PHP列中

时间:2015-06-16 00:06:31

标签: php html mysql

我正在为一个基本上我的朋友和我使用的项目工作。也许将其用于其他游戏。 SpeedRunning!我已经制作并能够使用此信息将数据发布到MySQL

<?php include_once('include/action_page.php');?>
<!DOCTYPE HTML>
<meta charset="UTF-8">
<html>
<head>
<title>Roleplayer's Tavern Home</title>
<link href="/style/style.css" rel="stylesheet" type="text/css">
<script src="/include/jquery-1.11.2.min.js"></script>
<?php include_once('/include/rpt_site_no_script.php');?>

</head>
<body onload="">
<div class="page_container" name="page_container">
    <div id="page_header">
        <!-- The title of the webpage -->
        <div style="max-width:250px; overflow:hidden">
          <span id="header_title"><img src="style/logo.png"     style="width:225px; height:67px;"/></span>
          </div>

    </div>
    <!-- Left side bar -->
    <div id="page_container_left">
        <h3 id="page_content_header">Submit your data!</h3>

<?php include('include/submit_data.php');?>

    </div>
    <div id="page_container_right"
        style="overflow-y: auto; max-height: 100%">
        <h3 id="page_content_header">Donations for website?</h3>
        <?php 

        ?>
    </div>
    <!-- Main Content -->
    <div id="page_content_container_main_page">
        <div class="page_content_container">
            <h2 id="page_content_container_header">Leaderboard WOO WOO</h2>
            <hr>


            <p id="page_content_container_content">


<h2>Players That Have Beaten Mad Pack 2</h2>

    <?php include('include/leaderboard.php');?>
        </div>
    </div>
    <br>
    <!-- Footer -->
<div id="page_footer">
<ol id="footer_list">
    <li>Copyright &copy; Roleplayer's Tavern 2015-2016 - All Rights     Reserved</li>
    <li style="font-size:12px">Your IP address <?php echo         $_SERVER['REMOTE_ADDR']; ?> will be logged for security reasons.</li>
</ol>
</div>


</div>
<script type="text/javascript">
var element=document.getElementsByName('page_container')[0];
var applyTo=document.getElementById('page_container_left');
var applyTo2=document.getElementById('page_container_right');
applyTo.style.height = (element.offsetHeight - 2) + "px";
applyTo2.style.height = (element.offsetHeight - 2) + "px";

window.onresize = function(event) {
    var element=document.getElementsByName('page_container')[0];
    var applyTo=document.getElementById('page_container_left');
    var applyTo2=document.getElementById('page_container_right');
    applyTo.style.height = (element.offsetHeight - 2) + "px";
    applyTo2.style.height = (element.offsetHeight - 2) + "px";
}
</script>

</body>
</html>

因为这是HTML格式。

Action_page.php是提交信息的页面

<?php 
$servername = "localhost";
$username = "USERNAME";
$password = "PASSWORD";
$mysqlDatabaseName = "SpeedRun";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $mysqlDatabaseName);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

$sql = "INSERT INTO MineCraftRecords (MineCraftName, LevelSeed, Day, Time)
VALUES ('$_POST[MinecraftName]', '$_POST[LevelSeed]', '$_POST[Day]',     '$_POST[Time]')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
echo "Turtle Mode Activated";
$conn->close();
?>
<meta http-equiv="Location" content="https://rptavern.org/SpeedRun/">

并且我无法让页面加载提供给leaderboard.php的信息,因为这是我到目前为止所做的。

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

$servername = "localhost";
$username = "USERNAME";
$password = "PASSWORD";
$mysqlDatabaseName = "SpeedRun";

$query="SELECT * FROM MineCraftRecords";$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;while ($i < $num) {CODE$i++;}
$variable=mysql_result($result,$i,"fieldname");
$field1-name=mysql_result($result,$i,"MineCraftName");
$field2-name=mysql_result($result,$i,"LevelSeed");
$field3-name=mysql_result($result,$i,"Day");
$field4-name=mysql_result($result,$i,"Time");
$field5-name=mysql_result($result,$i,"id");


// Create connection
$conn = mysqli_connect($servername, $username, $password,     $mysqlDatabaseName);  
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}   
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}  
echo "Turtle Mode Activated";
?>

我基本上试图以此网站http://www.speedrun.com/mc为基础,但不是技术性的。只是为了显示SpeedRunning时间并将TIME列为列表的顶部。

任何帮助都很棒,因为我非常擅长创造这样的东西。我会花时间阅读大家提出的意见和建议:D

2 个答案:

答案 0 :(得分:1)

我建议清理你的代码。可以做的一件事是您可以将dB连接细节分成单独的文件。这将优化您的代码,并确保在项目增长后更改细节。

答案 1 :(得分:0)

如果你考虑使用PDO这里有一个代码示例可以帮助你。我不确定mySQLi是否与PDO相似,但无论如何都要看一眼。它非常易于使用,并且可以在尽可能多的页面中实现!

HTML / PHP(在表格中显示数据):

<script language="JavaScript" type="text/javascript">
function checkDelete(){
    return confirm('Are you sure?');
}
</script>
</head>

<body>
    <?php
    ini_set("display_errors", 1);
    ini_set("display_startup_errors", 1);
    error_reporting(-1);
    require_once("../DAL/db_functions.php");

    //Run query on branch table
    readQuery("M_Branch");


//If there are any details in branch table continue

    if($numRecords === 0){
        echo "<p>No Branches Found!</p>";
    }   
    else{
        $arrRows = NULL;

        //Create table and headings
        echo "<table id='mavis' border='1' width='100%'>";
        echo "<tr>";
        echo "<th>Branch Code</th>";
        echo "<th>Branch Name</th>";
        echo "<th>Manager</th>";
        echo "<th>Branch Address</th>";
        echo "<th>Suburb</th>";
        echo "<th>State</th>";
        echo "<th>Post Code</th>";
        echo "<th>Phone Number</th>";
        echo "<th>Fax Number</th>";
        echo "<th></th>";
        echo "</tr>";

        while($arrRows = $stmt->fetch(PDO::FETCH_ASSOC)){
            echo "<tr>";
            echo "<td>".$arrRows['Branch_Code']."</td>";
            echo "<td>".$arrRows['Branch_name']."</td>";
            echo "<td>".$arrRows['Manager']."</td>";
            echo "<td>".$arrRows['Branch_Address']."</td>";
            echo "<td>".$arrRows['Suburb']."</td>";
            echo "<td>".$arrRows['State']."</td>";
            echo "<td>".$arrRows['Post_code']."</td>";
            echo "<td>".$arrRows['Phone']."</td>";
            echo "<td>".$arrRows['Fax']."</td>";

//Cannot delete already created records - Foreign key constraint fails
//If phpMyadmin were to delete one then other tables will incur problems            
            echo "<td><a href='edit_branch.php?ID=$arrRows[Branch_Code]'>Edit</a>";

            echo "<br /><a href='../BLL/delete_confirm.php?TYPE=Branch&amp;ID=$arrRows[Branch_Code]' onClick='return checkDelete()'>Delete</a></td></tr>";

        }
        echo "</table>";
        echo "<form action='../DAL/add_branch.php' method='post'>";
        echo "<input type='submit' value='Add a New Branch' />";
        echo "</form>";

        echo "<p></P><P>$numRecords Records Returned</P>";
    }
?>
</body> 

这是我的连接和readQuery函数,位于../ Db_functions.php

我创建了可重用的函数,可以与多个数据库表一起使用。

//Database connection Variables
$localhost = "localhost";
$user = "root";
$password = "root";
$db = "Mavis";
$dsn = "mysql:host=$localhost;dbname=$db";

//Declare Global Variables
$dbConnection = NULL;
$stmt = NULL;
$numRecords = NULL;



//This connect database function can be used to connect anywhere
function connect(){

    //These are variables from the other file (dblibary) - global allows access to these variables
    global $user, $password, $dsn, $dbConnection; //Required to access the global variables.

    try{
        $dbConnection = new PDO($dsn, $user, $password); 
        $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $error){
        //display error message if connection doesnt work
         echo "The following error occured: " . $error->getMessage();

    }

}

阅读查询:

function readQuery($table){
        global $numRecords, $dbConnection, $stmt;
        connect();

        $sqlStr = "SELECT * FROM " . $table.";";

        try{
            $stmt = $dbConnection->query($sqlStr);

            if($stmt === false){
                die("Error executing the qquery: $sqlStr");   
            }
        }
        catch(PDOException $error){
            echo "An Error occured: " . $error->getMessage();   
        }
        $numRecords = $stmt->rowCount();

        //Close the DB connection
        $dbConnection = NULL;
    }