php - 在与 html 相同的文件中导出 php 以使用 css(PHP 问题!)

标签 php html mysql css

我叫 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();
 ?> 

最佳答案

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

你应该做什么: 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> 

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

关于php - 在与 html 相同的文件中导出 php 以使用 css(PHP 问题!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37102320/

相关文章:

php - 如何剥离数据 :image part from a base64 string of any image type in PHP

javascript - 如何使用 HTML 表单简单地将 javascript 数据发布到 PHP

javascript - Angular.js 上 ng-repeat 指令中的 if 语句

Mysql sum group by performance 9M记录

php - Laravel 子域路由和资源 Controller

php - $e 在 PHP 异常中

javascript - 如何预览线条工具?

html - 如何以固定位置和 100% 高度将 2 个 div 并排放置

带有链接按钮的 HTML 表中的 PHP MySQL DELETE 行

python - 我怎样才能确保innodb线程安全