我刚刚开始将php文件包含到我的代码中,因此不必复制和粘贴大量内容。我曾经将数据库连接放在同一个文件中,但是自从将它放在另一个文件中以来,它就不会运行sql语句。
有效的原始内容
$username = "user";
$password = "pass";
$database = "db";
$hostname = "localhost";
$conn = new mysqli($hostname,$username,$password,$database) or die(mysqli_error());
$salt = 'salt';
$test = rand();
$pw_hash = md5($salt.$test);
$isContractor = 0;
if($_POST['userType'] == "Contractor") $isContractor = 1;
$sql = "INSERT INTO `UserHash` (`HashID`, `UserID`, `IsContractor`) VALUES ('".$pw_hash."', null, ".$isContractor.")";
$query = mysqli_query($conn, $sql);
if(!$query){
die("Error Found " . mysqli_error($conn));
}else{
echo '<div class="alert alert-success" role="alert">User Hash Created: ' . $pw_hash . '</div>';
}
单独的文件:
/Company/API/DatabaseConnection.php
$username = "user";
$password = "pass";
$database = "db";
$hostname = "localhost";
$conn = new mysqli($hostname,$username,$password,$database) or die(mysqli_error());
Hash.php
include "../Company/API/DatabaseConnection.php";
$salt = 'B7u3C0w';
$test = rand();
$pw_hash = md5($salt.$test);
$isContractor = 0;
if($_POST['userType'] == "Contractor") $isContractor = 1;
$sql = "INSERT INTO `UserHash` (`HashID`, `UserID`, `IsContractor`) VALUES ('".$pw_hash."', null, ".$isContractor.")";
$query = mysqli_query($conn, $sql);
if(!$query){
die("Error Found " . mysqli_error($conn));
}else{
echo '<div class="alert alert-success" role="alert">User Hash Created: ' . $pw_hash . '</div>';
}
这将返回来自!$ query的“发现错误”,但没有不会打印出错误。
答案 0 :(得分:0)
文件未提取正确的目录。
我不使用../ [文件名],我只需要/ [文件名]
更正后的代码为:
include "/Company/API/DatabaseConnection.php";