为用户开发一个非常简单的UPDATE查询页面来更改他们的帐户密码,但是在建立MySQLi连接时遇到了一些障碍(或者看起来似乎如此)。我是这个编程系列的新手,这是我第一次尝试执行动态查询,所以希望你们中的一个人能够轻松地发现这一点,并且你会非常友好地提供一些急需的sage建议。 / p>
以下是相关网页:http://www.parochialathleticleague.org/accounts.html
在执行表单的PHP脚本时,我最初只接收空白屏幕。我仔细检查了我的代码并做了我能想到的一切来诊断问题。在最终向require命令添加“OR die”函数后,我现在收到这条消息:
警告:require(1)[function.require]:无法打开流:没有这样的文件或目录>在第10行的/home/pal/public_html/accounts.php
致命错误:require()[function.require]:打开所需的'1'失败 (include_path ='。:/ usr / local / php52 / pear')in 第10行/home/pal/public_html/accounts.php
我很难过。这是脚本代码:
<?php
// Show errors:
ini_set('display_errors', 1);
// Adjust error reporting:
error_reporting(E_ALL);
// Connect to the database:
require ('../mysqli_connect.php') OR die('Error : ' . mysql_error());
// Validate the school:
if (empty($_POST['school'])) {
echo "You forgot to enter your school.<br>";
$validate = 'false';
} else {
$school = mysqli_real_escape_string($db, trim($_POST['school']));
$validate = 'true';
}
// Validate the existing password:
if (empty($_POST['pass'])) {
echo "You forgot to enter your existing password.<br>";
$validate = 'false';
} else {
$pass = mysqli_real_escape_string($db, trim($_POST['pass']));
$validate = 'true';
}
// Validate the new password:
if (empty($_POST['new_pass'])) {
echo "You forgot to enter your new password.<br>";
$validate = 'false';
} elseif (empty($_POST['confirm_pass'])) {
echo "You forgot to confirm your new password.<br>";
$validate = 'false';
} elseif ($_POST['new_pass'] != $_POST['confirm_pass']) {
echo "Sorry, your new password was typed incorrectly.<br>";
$validate = 'false';
} else {
$new_pass = mysqli_real_escape_string($db, trim($_POST['new_pass']));
$validate = 'true';
}
// If all conditions are met, process the form:
if ($validate != 'false') {
// Validate the school/password combination from the database:
$q = "SELECT school_id FROM user_schools WHERE (school_name='$school' AND pass=SHA1('$pass') )";
$r = @mysqli_query($db, $q);
$num = @mysqli_num_rows($r);
if ($num == 1) {
// Get the school_id:
$row = mysqli_fetch_array($r, MYSQLI_NUM);
// Perform an UPDATE query to modify the password:
$q = "UPDATE user_schools SET pass=SHA1('$new_pass') WHERE school_id=$row[0]";
$r = @mysqli_query($db, $q);
if (mysqli_affected_rows($db) == 1) {
header("Location: confirm_accounts.html");
} else {
echo "Your password could not be changed due to a system error. Apologies for the inconvenience. If this problem continues, please contact us directly.";
}
}
}
mysqli_close($db);
exit();
?>
最后,这是连接脚本中的代码(当然,省略了帐户值):
<?php
// Set the database access information as constants:
DEFINE ('DB_USER', '***');
DEFINE ('DB_PASSWORD', '***');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', '***');
// Make the connection:
$db = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' .mysqli_connect_error() );
// Set the encoding:
mysqli_set_charset($db, 'utf8');
我在过去几个小时里一直在努力解决这个问题。谷歌无法解决它。在这里查看档案无法解决它。
以下是我确实知道的事情:
不知道从哪里开始。任何帮助都将非常感谢!非常感谢提前。
编辑:@YourCommonSense - 根据您的建议,这是修改后的脚本。仍然得到空白屏幕。我是否错误地遵循了您的建议?
<?php
// Show errors:
ini_set('display_errors', 1);
// Adjust error reporting:
error_reporting(E_ALL);
// Connect to the database:
require ('../mysqli_connect.php');
// Validate the school:
if (empty($_POST['school'])) {
echo "You forgot to enter your school.<br>";
$validate = 'false';
} else {
$school = mysqli_real_escape_string($db, trim($_POST['school']));
$validate = 'true';
}
// Validate the existing password:
if (empty($_POST['pass'])) {
echo "You forgot to enter your existing password.<br>";
$validate = 'false';
} else {
$pass = mysqli_real_escape_string($db, trim($_POST['pass']));
$validate = 'true';
}
// Validate the new password:
if (empty($_POST['new_pass'])) {
echo "You forgot to enter your new password.<br>";
$validate = 'false';
} elseif (empty($_POST['confirm_pass'])) {
echo "You forgot to confirm your new password.<br>";
$validate = 'false';
} elseif ($_POST['new_pass'] != $_POST['confirm_pass']) {
echo "Sorry, your new password was typed incorrectly.<br>";
$validate = 'false';
} else {
$new_pass = mysqli_real_escape_string($db, trim($_POST['new_pass']));
$validate = 'true';
}
// If all conditions are met, process the form:
if ($validate != 'false') {
// Validate the school/password combination from the database:
$q = "SELECT school_id FROM user_schools WHERE (school_name='$school' AND pass=SHA1('$pass') )";
$r = mysqli_query($db, $q);
if (!$r) {
throw new Exception($mysqli->error." [$query]");
}
$num = mysqli_num_rows($r);
if ($num == 1) {
// Get the school_id:
$row = mysqli_fetch_array($r, MYSQLI_NUM);
// Perform an UPDATE query to modify the password:
$q = "UPDATE user_schools SET pass=SHA1('$new_pass') WHERE school_id=$row[0]";
$r = mysqli_query($db, $q);
if (!$r) {
throw new Exception($mysqli->error." [$query]");
}
if (mysqli_affected_rows($db) == 1) {
header("Location: confirm_accounts.html");
} else {
echo "Your password could not be changed due to a system error. Apologies for the inconvenience. If this problem continues, please contact us directly.";
}
}
}
mysqli_close($db);
exit();
?>
答案 0 :(得分:2)
您的代码和“解决方案”存在两个最大的问题:
@
运营商。你有-1投票给你的问题。 @
运算符 邪恶本身。 IT负责您看到的空白页。1
。 首先,你的包含是好的,所以,不管它。
要从mysqli收到错误,请按照以下说明操作:
不是随机添加“或死”,而是需要更强大,更有用的错误报告解决方案。
如果你在整个应用程序代码中使用mysqli_query()而没有将它封装到某个帮助器类中,trigger_error()
是一个引发PHP错误的好方法,因为它还会告诉你文件和行号发生错误的地方
$res = mysqli_query($mysqli,$query) or trigger_error(mysqli_error($mysqli)."[$query]");
在所有脚本中
从那时起,您将收到原因的通知,为什么没有创建对象。
(如果您对此or
语法I've explained it here感到好奇 - 它还解释了为什么您在错误消息中有(1)
然而,如果您将查询封装到某个类中,则触发错误的文件和行将无用,因为它们将指向调用本身,而不是导致某些问题的应用程序代码。因此,当封装运行mysqli命令时,必须使用另一种方法:
$result = $mysqli->query($sql);
if (!$result) {
throw new Exception($mysqli->error." [$query]");
}
因为Exception将为您提供堆栈跟踪,它将引导您调用错误查询的位置。
请注意,您必须能够查看PHP错误。在实际站点上,您必须查看错误日志,因此,必须设置
error_reporting(E_ALL);
ini_set('display_errors',0);
ini_set('log_errors',1);
在本地开发服务器上,可以在屏幕上出错:
error_reporting(E_ALL);
ini_set('display_errors',1);
当然你永远不应该在你的陈述前使用错误抑制操作符(@)。