提前谢谢。我查了一下论坛。
php程序允许用户登录并输入信息到mySQL数据库表Names
。
它检查所有字段是否已填写,
密码匹配,
用户尚不存在。
这是几个协同工作的程序之一
除一个方面外,所有代码都有效。
它不会插入数据库表Names
。
问题。可能问题是我没有设置PERMISSIONS来允许写这个表或数据库吗?
问题。这些文件位于何处?
的MacBookPro。 OSX10.8.3。
PHP版本5.3.15
故障排除 1.我连接到mysql数据库 2.我能够手动将数据插入字段。 3.以下所有代码均有效;适当的回声取决于填写或不填写的字段。 4.代码不会回复任何错误消息 5.我的代码中应该包含哪些内容? 死亡(mysql_error()); 6.为什么输入数据INSERT INTO?
register.php
<?php
require 'core.include.php'; // it has the loggedin() function
require 'connect.include.php';
//require 'register.success.php';
if (!loggedin()) {
// check if each form field is filled and correctly submitted
if(isset($_POST['username']) &&
isset($_POST['password']) &&
isset($_POST['password_again']) &&
isset($_POST['firstname']) &&
isset($_POST['surname'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$password_again = $_POST['password_again'];
$password_hash = md5($password);
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
// check if ALL fields are filled in
if
(!empty($username)&&
!empty($password)&&
!empty($password_again)&&
!empty($firstname)&&
!empty($surname)) {
if(strlen($username)>30||strlen($firstname)>45||strlen($surname)>45) {
echo 'please adhere to maxlengths of fields';
}else{
if($password!=$password_again) {
echo 'Passwords do not match.';
}else{
// start the registration process
// check if the user already exists in the database
$query = "SELECT `username` FROM `Names` WHERE `username` = '$username'";
//if the above $query returns a row than the user already exists
$query_run = mysql_query($query);
// I am unclear about what the above standard function actually accomplishes
if (mysql_num_rows($query_run)==1) {
echo "The username '.$username.' already exists";
}else{
//start the registration process
$query = "INSERT INTO `Names` VALUES
(' ',
'".mysql_real_escape_string($username)."',
'".mysql_real_escape_string($password_hash)."',
'".mysql_real_escape_string($firstname)."',
'".mysql_real_escape_string($surname)."')";
if ($query_run = mysql_query($query)) {
// if this query is successful: mysql_query($query))
// we locate the user to a page so they do not keep over registering
// echo 'registered';
header('Location:register_success.php');
} else {
echo 'Sorry, we could not register you at this time.';
// this is the problem.
// this message always returns when I attempt to register a new user.
}
}
}
}
//// echo 'Okay.'; // testing
//// echo "The username '.$username.' already exists"; // testing
// note. below inline php code will use the variables
//so that if the user types in 4 of 5 fields they will
//not have to retype in all of the fields again.
} else{
echo 'All fields are required.';
}
}
?>
Register Form:
<br><br>
<form action = "registration.php" method ="POST">
Username:<br> <input type ="text" name ="username" maxlength="32" value = "<?php
if (isset($username)) {echo $username;} ?>"><br><br>
Password:<br> <input type ="password" name ="password" > <br><br>
Password Again: <br> <input type ="password" name ="password_again" > <br><br>
Firstname:<br> <input type ="text" name ="firstname" maxlength="45"value="<?php
if (isset($firstname)) {echo $firstname;} ?>"><br><br>
Surname:<br> <input type ="text" name ="surname" maxlength="45" value = "<?php
if (isset($surname)) {echo $surname;} ?>"><br><br>
<input type ="submit" value ="register"><br><br>
</form>
<?php
//echo 'register';
}else if (loggedin ()) {
echo 'you are already registered and logged in.';
}
?>
答案 0 :(得分:0)
为了调试此问题,在代码中,mysql_query
调用返回FALSE后,使用mysql_error()
函数检索MySQL错误消息。
例如:
} else {
echo 'Sorry, we could not register you at this time.';
// this is the problem.
// this message always returns when I attempt to register a new user.
// for debugging: echo mysql_error(); or
die('error: '. mysql_error());
错误消息应指示是否存在语法问题,您插入的值的数量是否与(隐式)列列表中的列数相匹配,行是否违反约束,用户是否具有不足的权限问题,等
使用INSERT语句,最佳做法是明确列出接收值的列,例如
INSERT INTO foo (fee, fi, fo, fum) VALUES ('e','i','o','u');
如果添加了新列,或者列的顺序发生了更改,那么您的语句将不会在以后“损坏”。
如果是权限/权限问题,那么您需要GRANT
对您正在连接的mysql用户的相应权限。例如:
GRANT INSERT ON mydatabase.`Names` TO myser@myhost;
权限存储在mysql数据库中,user
,db
和tables_privs
表中。权限可以在全局,所有数据库(... ON *.*
),数据库级别(... ON mydatabase.*
)或单个表级别(... ON mydatabase.mytable
)上授予