简体   繁体   中英

PHP sign up with form, data entered into database

I have an admin portion of my website and would like to have the functionality of allowing admins to sign up and then be able to login. Right now I'm working on the Sign up form. When I submit my username and password, the web browser just shows a blank screen and nothing is added into my database. Here is my code. Any ideas why this is happening, and not working?

<?
$con = mysql_connect("localhost", "root", "");
if(!$con)
    {
    die('Could not connect: ' .mysql_error());
    }
mysql_select_db("bics_place", $con);

$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 
$encrypted_mypassword=md5($mypassword);

$sql = "INSERT INTO admin
            (username, password)
            VALUES
            ('myusername','encrypted_mypassword')";

if(!mysql_query($sql, $con))
    {
    die('Error: ' .mysql_Error());
    }
echo "1 record added";

mysql_close($con);

?>

I tested your code locally, it works. A couple things to check.

It's possible your server isn't configured to allow short tags. Try changing '<?' in line 1 to '<?php' .

If you must use short tags (you are working with someone else' code who uses them), you'll need to edit your php.ini file to allow it. I tend to avoid them for compatibility reasons.

short_open_tag=On

Should that not resolve your issue, add error_reporting(E_ALL); to the beginning of your file and it should show you the error. Remove it once you have your code working.

Also, you will need to edit your $sql variable. Put $ in front of your variable names.

$sql = "INSERT INTO admin
        (username, password)
        VALUES
        ('$myusername','$encrypted_mypassword')";

Otherwise, what does your server error log tell you?

Not sure why you wouldn't see any errors but your SQL is incorrect.

You have:

$sql = "INSERT INTO admin (username, password) VALUES ('myusername','encrypted_mypassword')";

Which should be:

$sql = "INSERT INTO admin (username, password) VALUES ('$myusername','$encrypted_mypassword')";

You're missing the $ in front of your variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM