将值插入DB不起作用?

时间:2012-03-14 05:19:50

标签: php mysql connect

嘿,伙计们之前我遇到过类似的问题,但我抓住了这个想法。现在基本上我的系统允许我的用户将数据输入到字段中,如果他们提交了数据,信息将转到数据库。现在由于某种原因数据不会出现,我会看到我存储在我的else语句中的回声,这是“echo”,稍后再试一次“;”

现在我已经回到数据库并查看了所有文件并找到了正确的名称并将它们放入查询中,但没有任何内容存储到数据库中。现在你可能会想到我的代码中最上面的文件名为connect.inc.php,这基本上是php中连接到服务器的脚本。

这是我的代码请求看看谢谢:)

<?php

  //require 'core.inc.php';
  include 'connect.inc.php';


if(isset($_POST['Username'])&& isset($_POST['Password']) && isset($_POST['PasswordAgain'])&& isset($_POST['Firstname'])&& isset($_POST['Lastname'])){

    $username = $_POST['Username'];
    $password = $_POST['Password'];

    $password_again = $_POST['PasswordAgain'];
    $Firstname = $_POST['Firstname'];

    $password_hash = md5($password);

    $Lastname = $_POST['Lastname'];

    if(!empty($username)&& !empty($password) && !empty($password_again) && !empty($Firstname) && !empty($Lastname)){



if ($password !== $password_again) {

            echo "passwords do not match";
        }
        else{

            $query = "SELECT username FROM members WHERE username = '$username'";
            $query_run = mysql_query($query);
            if(mysql_num_rows($query_run )==1){

                echo "The username ". $username ." is taken";

            }else{

                $query = "INSERT INTO members VALUES ('','Firstname','Lastname','Username','Password')";
                if ($query_run = mysql_query($query)){

                    echo "Well done";


                }else{

                    echo "Sorry we couldn't register at this time. Please try again later thank you";
                }

            }

        }
    }   
    else{

        echo "Please fill in all the details thank you ";

    }
}

?>


<form action="join.inc.php" method="post">


     Username: <input type="text" name="Username" value="<?php echo $username; ?>" /><br />
     Password: <input type="password" name="Password" /><br />
     Password Again: <input type="password" name="PasswordAgain" /><br />
     FirstName: <input type="text" name ="Firstname" value="<?php echo $Lastname; ?>"  /><br />
     LastName: <input type="text" name ="Lastname" value="<?php echo $Firstname ?>" /><br />

    <input type="submit" value="SUBMIT" />
</form>

连接脚本

7 个答案:

答案 0 :(得分:2)

我建议明确说明INSERT语句中使用的列。

INSERT INTO members (`field1`, `field2`, ...) 
VALUES ('','Firstname','Lastname','Username','Password')

此外,您尝试插入的空白值是多少?如果该字段是AUTO_INCREMENT字段,则不应将其包含在VALUES声明中。

答案 1 :(得分:1)

试试这个:

INSERT INTO members (`field1`, `field2`, ...) 
VALUES ('','$Firstname','$Lastname','$Username','$password_hash')

答案 2 :(得分:0)

你应该使用这样的东西。

$query = "insert into members (id,username) values ('','$username')";

答案 3 :(得分:0)

如果您的第一个字段是auto_increment

省略field1,如下所示

INSERT INTO members (`field2`,`field3`,...)
VALUES ('field2Val','field3Val',...);

答案 4 :(得分:0)

password !== $password_again 

应该是

password != $password_again

答案 5 :(得分:0)

尝试更改为:

$query = "INSERT INTO members VALUES ('". $Firstname."','". $Lastname. "','" .$username. "','" .$password_hash. "')";
$result = mysql_query($query);
  if (!$result){

           echo "Sorry we couldn't register at this time. Please try again later thank you";

   }else{
           echo "Well done";

   }

答案 6 :(得分:0)

启用error_reporting并实际看到错误,ID是第一个字段自动递增吗?

如果这是自动递增,您的查询将执行,但如果它不自动递增并设置为PK,则它不会插入记录并引发重复键错误。

希望这会有所帮助