質問

Please help, I am new to php and trying to write a register page. No errors appear but the data doesn't appear in the database either. All of the validation works fine so I think there is something wrong when inserting into the table. Can anyone help?

<?php 

if($_SERVER['REQUEST_METHOD']=='POST')
{
require('connect_db.php');
$errors = array();


if(empty($_POST['firstname']))
    {
    $errors[]='Enter your Firstname';
    }
else 
    {
    $fn = mysqli_real_escape_string($dbc, trim($_POST['firstname']));
    }


if(empty($_POST['surname']))
    {
    $errors[]='Enter your Surname';
    }
else 
    {
    $sn = mysqli_real_escape_string($dbc, trim($_POST['surname']));
    }


if(empty($_POST['email']))
    {
    $errors[]='Enter your email address';
    }
else 
    {
    $email = mysqli_real_escape_string($dbc, trim($_POST['email']));
    }

if(!empty($_POST['pass1']))
    {
        if($_POST['pass1']!= $_POST['pass2'])
            {
                $errors[] = 'Passwords do not match';   
            }
        else
            {
                $p = mysqli_real_escape_string($dbc, trim($_POST['pass1']));
            }
    }
else
    {
        $errors[]="Enter your password";
    }

if (empty($errors))
    {
        $q = "INSERT INTO Users(Username, Firstname, Surname, Password)
            VALUES($email, $fn, $sn,  User, SHA1($p))";
        $r = mysqli_query ($dbc, $q);
        if ($r)
        {
            echo "success";
        }
    }
else 
    {
        echo '<h1>Error</h1><p id="errors">The following error/s occurred:<br/>';
        foreach ($errors as $error)
            {
                echo"$error <br/>";

            }
        echo"Please try again</p>";
        mysqli_close($dbc);
    }

}

役に立ちましたか?

解決

You're missing quotes around your string values:

INSERT INTO Users(Username, Firstname, Surname, Password)
        VALUES($email, $fn, $sn,  User, SHA1($p))

should be:

INSERT INTO Users(Username, Firstname, Surname, Password)
        VALUES('$email', '$fn', '$sn',  'User', 'SHA1($p)')

The reason you get an error is you're not actually checking for any. You need to add an else statement when you check the value of $r and in there call mysqli_error() to see your error message.

if ($r) {
    echo "success";
}
else {
    echo mysqli_error($dbc); exit// this really should be better then this.
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top