php - 来自 MYSQL 数据库的结果和查询未以 HTML 格式显示

标签 php mysql database mysqli wampserver

我正在为一个非营利组织设计一个数据库,我是其中的一名志愿者。 2012年,我学习了数据库的基础知识,在WampServer 2.2中使用了MySQL、SQL和PHP。我已经知道如何用 HTML 编程。

对于我在非营利组织开展的项目,我使用的是 WampServer 2.5。当时我并没有意识到我很快就会遇到问题。

像往常一样,在 Apache 中创建网站和各种表单很容易。使用 SQL 脚本创建数据库,我在其中直接输入 WampServer 的用户界面,工作顺利;然而,通过 PHP 将数据从 HTML 表单传输到数据库一直充满困难。我得到的错误之一是 PHP 错误中弃用的 MySQL 扩展。我发现我在学习中学到的大部分知识,因为最新版本升级了 WampServer 中的模块,所以几乎没有用。

当我试图研究我的问题时,我遇到了这个教程: using-php-with-mysql-the-right-way

教程自带示例代码,可以自由使用。我已经能够成功使用大部分代码;但是,我很难用 HTML 显示我的结果。我已经通读了教程,觉得我遗漏了一些我的想法无法接受的东西。我充其量只是初学者/业余爱好者。

我不会显示输入表单的 HTML 代码,因为它只是一个没有任何特殊之处的表单。它不包含 PHP,它发布到 outputted.php

这是我的代码:

db_functions.php

    <?php
/**
* Database functions for a MySQL with PHP tutorial
* 
* @copyright Eran Galperin
* @license MIT License
* @see http://www.binpress.com/tutorial/using-php-with-mysql-the-right-way/17
*/

/**
* Connect to the database
* 
* @return bool false on failure / mysqli MySQLi object instance on success
*/
function db_connect() {

// Define connection as a static variable, to avoid connecting more than once 
static $connection;
// Try and connect to the database, if a connection has not been established yet
if(!isset($connection)) {
    // Load configuration as an array. Use the actual location of your configuration file
    // Put the configuration file outside of the document root
    $config = parse_ini_file('../../config.ini'); 
    $connection = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);
}
// If connection was not successful, handle the error
if($connection === false) {
    // Handle error - notify administrator, log to a file, show an error screen, etc.
    return mysqli_connect_error(); 
}
return $connection;
}
/**
* Query the database
*
* @param $query The query string
* @return mixed The result of the mysqli::query() function
*/
function db_query($query) {
// Connect to the database
$connection = db_connect();
// Query the database
$result = mysqli_query($connection,$query);
return $result;
}
/**
* Fetch rows from the database (SELECT query)
*
* @param $query The query string
* @return bool False on failure / array Database rows on success
*/
function db_select($query) {
$rows = array();
$result = db_query($query);
// If query failed, return `false`
if($result === false) {
    return false;
}
// If query was successful, retrieve all the rows into an array
while ($row = mysqli_fetch_assoc($result)) {
    $rows[] = $row;
}
return $rows;
}
/**
* Fetch the last error from the database
* 
* @return string Database error message
*/
function db_error() {
$connection = db_connect();
return mysqli_error($connection);
}
/**
* Quote and escape value for use in a database query
*
* @param string $value The value to be quoted and escaped
* @return string The quoted and escaped string
*/
function db_quote($value) {
$connection = db_connect();
return "'" . mysqli_real_escape_string($connection,$value) . "'";
}
?>

输出.php

<HTML>
<HEAD><TITLE>104.9 LIMEFM MEMBERSHIP DATABASE</TITLE>
<STYLE>
TEXT-DECORATION=NONE
</STYLE>
</HEAD>

<STYLE>
A {TEXT-DECORATION: NONE}
</STYLE>

<BODY BGCOLOR="#D9FFD9" TEXT="Black" LINK="Black" ALINK="Black" VLINK="Black">

<CENTER>

<TABLE WIDTH="1020" BORDER="0">

<TR><TD COLSPAN="3">&nbsp;</TR>

<TR>
<TD WIDTH="340" ALIGN="left"><A HREF="help/help_index.html" TARGET="_blank"><IMG SRC="./images/help_button.png" WIDTH="62" HEIGHT="62" ALT="Help"></A>
<TD WIDTH="350" ALIGN="center"><IMG SRC="./images/limefm(green).jpg" WIDTH="143" HEIGHT="62" ALT="104.9 LimeFM">
<TD WIDTH="350" ALIGN="right"><B>Powered by: wAmp 2.5</B></P>Brought to you by:<BR><IMG SRC="./images/hills_designing_logo(green).jpg" ALT="Hill's Designing">
</TR>

</TABLE>

</P><HR WIDTH="1000"></P>

<TABLE WIDTH="1020" BORDER="0" CELLSPACING="10">

<TR><TD COLSPAN="4" ALIGN="center" BGCOLOR="#338928"><B><FONT COLOR="white" SIZE="5">outputted.php</FONT></B></TR>

<TR><TD COLSPAN="4" ALIGN="center">&nbsp;</TR>

<TR><TD COLSPAN="4" ALIGN="center"><HR WIDTH="1000"></B></TR>

<TR><TD COLSPAN="4" ALIGN="center">&nbsp;</TR>

</TABLE>

</CENTER>

<?php   

/**
* Inserting data from input_record.html into the following tables in database limefm_members:
* 
* members,
* ship_info,
* comments.
*/

// This is working, although I am not getting any notification that it is working.

//First I call for the functions
require("db_functions.php");

$member_Id=$_POST['member_Id'];
$Title=$_POST['Title'];
$LastName=$_POST['LastName'];
$FirstName=$_POST['FirstName'];
$Po_Box=$_POST['Po_Box'];
$Street=$_POST['Street'];
$City=$_POST['City'];
$State=$_POST['State'];
$Country=$_POST['Country'];
$Del=$_POST['Del'];
$WFTD=$_POST['WFTD'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$email=$_POST['email'];
$DateJoined=$_POST['DateJoined'];
$Type=$_POST['Type'];
$RenewalDate=$_POST['RenewalDate'];
$LastContacted=$_POST['LastContacted'];
$MYOB_ID=$_POST['MYOB_ID'];
$Comment=$_POST['Comment'];

// I chose not to use syntax like: $email=db_quote($_POST['email']);

// Insert the values into the database

$result = db_query("INSERT INTO `members` (`member_id`,`title`,`lastname`,`firstname`,`po_box`,`street`,`city`,`state`,`country`,`del`,`phone`,`mobile`,`email`,`wftd`) VALUES ('','$Title','$LastName','$FirstName','$Po_Box','$Street','$City','$State','$Country','$Del','$phone','$mobile','$email','$WFTD')");

$result = db_query("INSERT INTO `ship_info` (`member_id`,`lastname`,`firstname`,`date_joined`,`type`,`renewal_date`,`last_contacted`,`myob_id`) VALUES ('','$LastName','$FirstName','$DateJoined','$Type','$RenewalDate','$LastContacted','$MYOB_ID')");

$result = db_query("INSERT INTO `comments` (`member_id`,`lastName`,`firstName`,`comment`) VALUES ('','$LastName','$FirstName','$Comment')");

echo"Attempting to display your last entered entry:</P>";


echo"Table 1: Members:</P>";

// result not displaying

$rows = db_select("SELECT * FROM members WHERE firstname='$FirstName' AND lastname='$LastName'");
if($rows === false) {
    $error = db_error();
    // Handle error - inform administrator, log to file, show error page, etc.
}

echo"Table 2: Ship_info:</P>";

// result not displaying
$rows = db_select("SELECT * FROM ship_info WHERE firstname='$FirstName' AND lastname='$LastName'");
if($rows === false) {
    $error = db_error();
    // Handle error - inform administrator, log to file, show error page, etc.
}

echo"Table 2: Comments:</P>";

// result not displaying
$rows = db_select("SELECT * FROM comments WHERE firstname='$FirstName' AND lastname='$LastName'");
if($rows === false) {
    $error = db_error();
    // Handle error - inform administrator, log to file, show error page, etc.
}

?>

<CENTER>

<TABLE WIDTH="1020" BORDER="0" CELLSPACING="10">

<TR><TD>&nbsp;</TR>

<TR><TD>&nbsp;</TR>

<TR><TD>&nbsp;</TR>

<TR><TD><HR WIDTH="1000"></TR>

<TR><TD>&nbsp;</TR>

<TR><TD ALIGN="center"><FONT SIZE="5" COLOR="red">The Success or Failure for Records being added are not being displayed.</FONT><BR>
                       <FONT SIZE="5" COLOR="black">The only current way for verifying data is through: </FONT><A HREF="../phpmyadmin/">
                       <FONT SIZE="5" COLOR="black"><B>phpMyAdmin.</B></FONT></A></TR>

<TR><TD>&nbsp;</TR>

<TR><TD><IMG SRC="./images/limefm_icon.jpg"><A HREF="input_record.html"><FONT SIZE="5">Input Next Record?</FONT></A></TR>

<TR><TD><IMG SRC="./images/limefm_icon.jpg"><A HREF="../phpmyadmin/"><FONT SIZE="5">Check Results?</FONT></A></TR>

<TR><TD><IMG SRC="./images/limefm_icon.jpg"><A HREF="index.html"><FONT SIZE="5">Return Home?</FONT></A></TR>

<TR><TD>&nbsp;</TR>

</TABLE>

</CENTER>
</BODY>
</HTML>

正如您从上面的代码中看到的,一旦变量的值被放入数据库(正在发生),我想在屏幕上显示最后输入的记录以验证输入是否成功,以便 PHP不需要管理员。我希望那些不熟悉计算机的人能够通过我创建的输入表单访问数据库。如果我无法显示 outputted.php 的结果,那么我将无法绘制根据常用查询搜索数据库所需的表格。

任何帮助将不胜感激。谢谢。

最佳答案

您还没有编写代码来显示 db_select 函数返回的结果。 db_select 函数返回数组中的数据库结果集。您需要遍历数组并在 HTML 中显示值。

<?php

echo "<P>Table 1: Members:</P>";
// result not displaying
$rows = db_select("SELECT * FROM members WHERE firstname='$FirstName' AND lastname='$LastName'");

if($rows === false) {
    $error = db_error();
    // Handle error - inform administrator, log to file, show error page, etc.
}
else
{
    foreach($rows as $v)
    {
          //Display html code
    }
}

?>

关于php - 来自 MYSQL 数据库的结果和查询未以 HTML 格式显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31635609/

相关文章:

php - postgresql 引用问题

php - 将电子邮件回复存储在 mysql 数据库中 (PHP)

PHP 强制日期为 CSV 中的文本

MySQL - 无法添加外键约束

mysql - 子查询返回千行,导致慢

database - 设计我们的数据库,表结构,关系

PHP SQL 列 = 列减去 $value

php - 如何将数据库表中的参数放入CSS样式

php - 在docker镜像中安装mysql客户端

python - 我将如何在 Django 中进行此查询?