douzangdang2225 2016-07-08 16:39
浏览 43
已采纳

从外部php文件连接mysqli_query命令

I have been trying to use mysqli_query to connect to database define in an external file but am getting an repeated error as undefined $conn

index.php

<?php
require 'connect.in.php';
include 'loginform.php';

?>

connect.in.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbase = "users";

$conn = mysqli_connect($servername,$username,$password);

if($conn ||!mysqli_select_db($dbase)){

    die('error message');
}
?>

loginform.php

<?php
    if(isset($_POST['username']) && isset($_POST['password'])){
    $username = 'username';
    $password = 'password';
    if(!empty($username) && !empty($password)){
        $query = "SELECT 'id' FROM 'user_list' WHERE 'username'= '$username' AND 'password' = '$password'";
        if(mysqli_query($conn, $query)){
            echo "correct";
        }
        else{
            echo "false";
        }
    }
}

?>
<form method="post">
Username:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" value="Login">

</form>

also is it possible to incorporate CSS and Bootstrap in this php files say for loginform.php?

  • 写回答

1条回答 默认 最新

  • douzuanze0486 2016-07-08 16:47
    关注

    Don't forget to tell mysqli_connect() function what database you are using. I hate to use W3Schools but take a look here, the 4th parameter is the DB you want to use.

    connect.in.php

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbase = "users";
    
    $conn = mysqli_connect($servername,$username,$password, $dbase);
    
    if($conn ||!mysqli_select_db($dbase)){
    
        die('error message');
    }
    ?>
    

    Generally you would want all your CSS and JS files in the <head> element of your index page or your master page. Essentially you could just add the files in your loginform.php file at the top but i would suggest not doing so and place them in your index.php file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能