不在PHP文件上显示数据

时间:2013-08-15 13:07:46

标签: php mysql

我正在尝试学习一些新东西,并且一直想学习如何用PHP和mysql建立一个网站......

我发现这个简单的教程和样本文件可以用

http://css-tricks.com/php-for-beginners-building-your-first-simple-cms/

我正在尝试添加另一个在数据库中工作的表,但是当我尝试显示它时它不起作用。这是我得到和使用的代码:

<?php

class simpleCMS {

  var $host;
  var $username;
  var $password;
  var $table;

  public function display_public() {
    $q = "SELECT * FROM laptopvoltage ORDER BY created DESC LIMIT 3";
    $r = mysql_query($q);

    if ( $r !== false && mysql_num_rows($r) > 0 ) {
      while ( $a = mysql_fetch_assoc($r) ) {
        $lvmodel = stripslashes($a['lvmodel']);
        $lvmanuf = stripslashes($a['lvmanuf']);
        $lvvolt = stripslashes($a['lvvolt']);

        $entry_display .= <<<ENTRY_DISPLAY

    <div class="post">
        <h2>
            $lvmodel
        </h2>
        <p> !!!!!!this dont show upp!!!!!! - - - - >>>>>
          $lvmanuf
        </p><----------- WHY?
        <p>
          $lvvolt
        </p>
    </div>

ENTRY_DISPLAY;
      }
    } else {
      $entry_display = <<<ENTRY_DISPLAY

    <h2> This Page Is Under Construction </h2>
    <p>
      No entries have been made on this page. 
      Please check back soon, or click the
      link below to add an entry!
    </p>

ENTRY_DISPLAY;
    }
    $entry_display .= <<<ADMIN_OPTION

    <p class="admin_link">
      <a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>
    </p>

ADMIN_OPTION;

    return $entry_display;
  }

  public function display_admin() {
    return <<<ADMIN_FORM

    <form action="{$_SERVER['PHP_SELF']}" method="post">

      <label for="lvmodel">Title:lv model</label><br />
      <input name="lvmodel" id="lvmodel" type="text" maxlength="150" />
      <div class="clear"></div>

      <label for="lvmanuf">Title:lv manu</label><br />
      <input name="lvmanuf" id="lvmanuf" type="text" maxlength="150" />
      <div class="clear"></div>
      <label for="lvvolt">Title:lvvolt</label><br />
      <input name="lvvolt" id="lvvolt" type="text" maxlength="150" />
      <div class="clear"></div>

      <input type="submit" value="Create This Entry!" />
    </form>

    <br />

    <a href="display.php">Back to Home</a>

ADMIN_FORM;
  }

  public function write($p) {
    if ( $_POST['lvmodel'] )
      $lvmodel = mysql_real_escape_string($_POST['lvmodel']);
    if ( $_POST['lvmanuf'] )
      $lvmanuf = mysql_real_escape_string($_POST['lvvolt']);
       if ( $_POST['lvvolt'] )
      $lvvolt = mysql_real_escape_string($_POST['lvvolt']);
    if ( $lvmodel && $lvmanuf && $lvvolt ) {
      $created = time();
      $sql = "INSERT INTO laptopvoltage VALUES('$lvmodel','$lvmanuf','$lvvolt','$created')";
      return mysql_query($sql);
    } else {
      return false;
    }
  }

  public function connect() {
    mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
    mysql_select_db($this->table) or die("Could not select database. " . mysql_error());

    return $this->buildDB();
  }

  private function buildDB() {
    $sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS laptopvoltage (
lvmodel     VARCHAR(150),
lvmanuf TEXT,
lvvolt  VARCHAR(150),
created     VARCHAR(100)
)
MySQL_QUERY;

    return mysql_query($sql);
  }

}

?>

它不会显示$lvmanuf。当字段显示在我的数据库中时,对此的任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

这个第一个文件只显示结果,如果你不熟悉web逻辑和设计那么病我尽力解释一下,这个第一个文件叫做index.php,每个网站和web-application都有一个文件要么调用索引。 html或index.php背后的原因是Web服务器查找名为index.html或index.php的文件并且不要误解不仅仅是这些文件类型和名称服务器可以从它开始只是这些是最常见的,因为现在我将解释第一个文件背后的代码。

你可以看到我们已经在里面设置了我们的基本html文档并添加了一个脚本,现在我们制作的脚本将使我们指定的id中加载的文件在设置的ammount秒后消失,然后在body体内我们把这段代码放到了HTML中,

<span id="messages">

<?php include "constant.php"; ?>    

</span>

这段代码包含给这个脚本的主要玩家首先带有id属性的span标签告诉我们的javascript我们想要在设定的秒数之后看不见的文本的id,接着是

<php include "constant.hpp"; ?>

它包含了我们制作的constant.php文档中的所有内容。

档案1

的index.php

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
    <title>MySQL Connection test</title>
    <script type="text/javascript">
        window.onload = function()
        {
            timedHide(document.getElementById('messages'), 10);
        }

        function timedHide(element, seconds)
        {
            if (element) {
                setTimeout(function() {
                element.style.display = 'none';
             }, seconds*1000);
         }
    }
    </script>
</head>
<body>
    <span id="messages">

    <?php include "constant.php"; ?>

    </span>
</body>
</html>

这个第二个文件我不打算解释它,因为它会使这种方式变长,但这个文件是mysql数据库的连接文件。

您需要填写的唯一部分是

$ database_ip =“”; //数据库ip地址在引号内

$ database_port =“”; //数据库端口进入引号

$ database_name =“”; //数据库名称在引号内

$ database_admin_user =“”; // admin用户名在引号内

$ database_admin_pass =“”; //管理员密码在引号内

这会将您的网站连接到数据库。

文件2

constant.php

<?php
$database_ip = ""; //database ip adress goes inside quotes
$database_port = ""; //database port goes inside quotes
$database_name = ""; //database name goes inside quotes

$database_admin_user = ""; //admin username goes inside quotes
$database_admin_pass = ""; //admin password goes inside quotes

//do not modify anything past this point unless you know php well.

$database_link = null;

$database_defaults = array("127.0.0.1","3306","MySQL","root","");
$error_defaults = array("error_no_101" => "required field *IP is empty, using default parameters!",
                        "error_no_102" => "required field *PORT is empty, using default parameters!",
                        "error_no_103" => "required field *NAME is empty, using default parameters!",
                        "error_no_104" => "required field *USER is empty, using default parameters!",
                        "error_no_105" => "required field *PASS is empty, using default parameters!");

if(empty($database_ip)){
    $database_ip = $database_defaults[0];
    echo $error_defaults["error_no_101"] . "<br/>";
}

if(empty($database_port)){
    $database_port = $database_defaults[1];
    echo $error_defaults["error_no_102"] . "<br/>";
}

if(empty($database_name)){
    $database_name = $database_defaults[2];
    echo $error_defaults["error_no_103"] . "<br/>";
}

if(empty($database_admin_user)){
    $database_admin_user = $database_defaults[3];
    echo $error_defaults["error_no_104"] . "<br/>";
}

if(empty($database_admin_pass)){
    $database_admin_pass = $database_defaults[4];
    echo $error_defaults["error_no_105"] . "<br/>";
}

$database_link = mysqli_connect($database_ip, $database_admin_user, $database_admin_pass, $database_name);

if (!$database_link) {
    die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
} else {
    echo 'Success... ' . mysqli_get_host_info($database_link) . "\n";
}

mysqli_close($database_link);

?>

我这样做是为了帮助你修复你的代码,而不是教你语言的语法。

学习php的语法我建议你去这里:

这是官方的php网站文档,它教你正确编写php的方法,

http://www.php.net/manual/en/langref.php

如果您有订阅费用,也可以尝试这个地方:

http://www.lynda.com/MySQL-tutorials/PHP-MySQL-Essential-Training/119003-2.html?srchtrk=index:1%0Alinktypeid:2%0Aq:php%0Apage:1%0As:relevance%0Asa:true%0Aproducttypeid:2

对于HTML,你可以去:

http://www.w3schools.com/html/default.asp

如果您有订阅费用,也可以尝试这个地方:

http://www.lynda.com/HTML-tutorials/HTML-Essential-Training-2012/99326-2.html