php和sql“插入”不填充sql数据库
提前谢谢。我查过论坛了。 php程序允许用户登录并在mySQL数据库表名中输入信息。 它检查所有字段是否都已填写, 密码匹配, 用户不存在。 这是几个协同工作的程序之一 除了一个方面,所有代码都可以工作。 它不会将名称插入到数据库表中 几个月前我遇到了一个类似的问题 问题。问题可能是我没有设置允许写入此表或数据库的权限 问题。要执行此操作,此文件位于何处? Macbookpro。OSX10.8.3。 PHP版本5.3.15 故障排除 1.我连接到mysql数据库。 2.我可以手动将数据插入字段。 3.以下所有代码均有效;适当的回音取决于填充或不填充的字段。 4.代码不会返回任何错误消息。 5.我的代码中应该包括哪些内容?? 迪厄误差; 6.为什么数据不能插入到中php和sql“插入”不填充sql数据库,php,mysql,insert-into,Php,Mysql,Insert Into,提前谢谢。我查过论坛了。 php程序允许用户登录并在mySQL数据库表名中输入信息。 它检查所有字段是否都已填写, 密码匹配, 用户不存在。 这是几个协同工作的程序之一 除了一个方面,所有代码都可以工作。 它不会将名称插入到数据库表中 几个月前我遇到了一个类似的问题 问题。问题可能是我没有设置允许写入此表或数据库的权限 问题。要执行此操作,此文件位于何处? Macbookpro。OSX10.8.3。 PHP版本5.3.15 故障排除 1.我连接到mysql数据库。 2.我可以手动将数据插入字段。
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.';
}
?>
为了调试这个问题,在代码中,在mysql\u查询调用返回FALSE后,使用mysql\u 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());
GRANT INSERT ON mydatabase.`Names` TO myser@myhost;
错误消息应该指出这是否是语法问题,插入的值数是否与隐式列列表中的列数匹配,行是否违反约束,用户是否没有足够的权限,等等
对于INSERT语句,最好的做法是显式列出接收值的列,例如
INSERT INTO foo (fee, fi, fo, fum) VALUES ('e','i','o','u');
如果添加了一个新列,或者更改了列的顺序,那么您的语句就不会被破坏
如果是权限/特权问题,那么您需要向您连接的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());
GRANT INSERT ON mydatabase.`Names` TO myser@myhost;
特权存储在mysql数据库、用户表、db表和privs表中。可以在所有数据库上全局授予权限。。。在*上,在数据库级别。。。在mydatabase.*上或在单个表级别上。。。在mydatabase.mytable上,不检查错误。使用mysql\u error查看出了什么问题。或者从command line.off主题运行它,但是:如果您试图学习在php中使用mysql,那么不要使用mysql函数。它们已被弃用,并且存在一些严重问题。请参阅:将您的mysql更新为mysqli。。5.5.0不推荐使用mysql_uu命令。您的表“名称”列是什么?这些东西排对了吗?如果有帮助的话,你可以改成。。在“名称”中插入用户名、密码、名字、姓氏值;谢谢进步我插入了你建议的代码。返回:“很抱歉,我们现在无法为您注册。”第1行“id”列的整数值不正确。听起来像是错误。表中我的列:名称为id自动递增、用户名、密码、名字、姓氏。我希望“id”自动递增为标识符字段。这就是为什么我用它来表示第一个值的空值。我理解您的第二个建议:列和值的显式代码。这是一个基于网络的注册。我在关于这个主题的第一篇文章中使用了php代码。很抱歉,这是一个非常好的教程系列,我只想通过它了解更多当前的编码策略。代码似乎直接且合乎逻辑,现在我们看到了我不知道如何处理的错误,请继续执行此线程。@iHaveAQuestion:您可以在“自动增量”列中插入NULL或零,也可以在insert语句中省略“自动增量”列。e、 g.插入到foo-id中,bar值为空'a',或插入到foo-bar值'a'。:谢谢!成功了!你很直观,这个逻辑简单的修正正是我所希望的。我的联系人为其他人排除故障。我为代码中的第一个值插入了“NULL”,并收到以下错误:抱歉,我们此时无法为您注册。错误:不正确的整数值:第1行“id”列的“NULL”;NULL不是“NULL”,正如您上面编码的那样$query=INSERT INTO NAME VALUES NULL、.mysql\u real\u escape\u string$username、.mysql\u real\u escape\u string$password\u hash、.mysql\u real\u escape\u string$firstname、.mysql\u real\u escape\u string$name、;我如何将您的回答标记为“正确答案”?我正在看你们对我的一般性问题的其他回答。再次感谢。