【问题标题】:Exporting php in a same file as html to use css (PHP issue!)在与 html 相同的文件中导出 php 以使用 css(PHP 问题!)
【发布时间】:2016-05-09 01:36:33
【问题描述】:

我的名字是 Luis,我正在尝试用 html、css 和 php 创建一个文件,该文件从数据库中获取信息并输出。但它必须与 html 位于同一页面中,以便我可以使用 CSS 进行编辑...谢谢大家!

<html>
<head>

</head>



<body>


<script>

</script>
<form action="<?php include 'file.php';?>" method="POST">
<b>Busque Jรก</b>
<input type="text" name="prestador" placeholder="prestador">
<input type="text" name="cidade" placeholder="cidade">
<input type="submit">
</form>

</body>


</html>

PHP 中的文件:

<?php


$servername = "localhost";
$username = "username";
$password = "";
$dbname = "test";
$namer = $_POST['prestador'];
$cities = $_POST['cidade'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
 } 

$sql = "SELECT name,address,city FROM tb_prestadores WHERE name = '$namer'   AND city = '$cities'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "name: " . $row['name']. " Endereço - : " . $row['address']." Cidade :".$row['city']."<br>";
    }
} else {
    echo "It is going to else";
}
 $conn->close();
 ?> 

【问题讨论】:

  • 参考网络基础知识。将文件包含为action 值并不意味着该文件在您按下按钮时执行。
  • 首先,恐怕这不是 php 的工作方式...您希望操作指向 file.php &lt;form action="./file.php"...&gt; 您正在执行的操作是打印文件的输出。 php 例如&lt;form action="&lt;?php include 'file.php';?&gt;" 变为 &lt;form action="It is going to else"

标签: php html mysql css


【解决方案1】:

您错误地包含/插入了文件。

你应该做什么: 1) 复制您的 html 代码并将其粘贴到您的 php 代码的底部,即在结束 '?>' 标记之后。您的代码应如下所示:

    <?php


    $servername = "localhost";
    $username = "username";
    $password = "";
    $dbname = "test";
    $namer = $_POST['prestador'];
    $cities = $_POST['cidade'];
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
     } 

    $sql = "SELECT name,address,city FROM tb_prestadores WHERE name = '$namer'   AND city = '$cities'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "name: " . $row['name']. " Endereço - : " . $row['address']." Cidade :".$row['city']."<br>";
        }
    } else {
        echo "It is going to else";
    }
     $conn->close();
     ?> 

    <html>
<head>
<!-- include your css here-->
</head>



<body>


<script>

</script>
<form action="" method="POST">
<b>Busque Jรก</b>
<input type="text" name="prestador" placeholder="prestador">
<input type="text" name="cidade" placeholder="cidade">
<input type="submit">
</form>

</body>


</html> 

顺便说一句,这不是编写应用程序的“最佳”方式。一旦掌握了 php 和 html 基础知识,请查看Smarty 或其他一些模板语言以及MVC。祝你好运!

【讨论】:

    【解决方案2】:

    首先,您的&lt;?php include 'file.php';?&gt; 被错误放置,不应出现在formaction 标记中,而应该在它之前,如下所示:

    <html>
     <head></head>
     <body>
      <?php include 'file.php';?>
      <form action="" method="POST">
       <b>Busque Jรก</b>
       <input type="text" name="prestador" placeholder="prestador">
       <input type="text" name="cidade" placeholder="cidade">
       <input type="submit">
      </form>
     </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多