Search code examples
phpregistration

Errors while registrate as user --php


I am going to create a registration in php but it shows me the following errors:

string(59) "insert into users (username,email,password)values('','','')" Duplicate entry '' for key 'email'

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

mysql_select_db("university", $con);


$res = mysql_query("INSERT INTO users (username, password, email) VALUES('{$_POST['uname']}', '{$_POST['pwd']}', '{$_POST['email']}')");
if (!$res) {

    echo "MYSQL ERROR ->" . mysql_error();
} else {
    echo '<script type="text/javascript">alert(\'Regjistrimi u krye me sukses!\')</script>';
}
?>

This i the form in html. I cant see where it may be wrong

<div id="containt" align="right">
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" id="registration_form"> 

        <table border="0">
    <tbody>

    <tr>
    <td><label for="name">Your Name*: </label> </td>
    <td><input id="name" maxlength="45" name="name" type="text" /> </td>

    </tr>

    <tr>
    <td><label for="email">Email*:</label> </td>
    <td><input id="email" maxlength="45" name="email" type="text" /></td>
    </tr>

    <tr>
    <td><label for="username">Username*:</label> </td>
    <td><input id="username" maxlength="45" name="uname" type="text" /> </td>
    </tr>

    <tr>
    <td><label for="password">Password*:</label></td>
    <td><input id="password" maxlength="45" name="pwd" type="password" /></td>
    </tr>

    <tr>
    <td align="right"><input name="Submit" type="submit" value="Regjistro" /></td>
    </tr>

    </tbody></table>

    </form>
    </div>

Solution

  • It looks like you are trying to do this all from one php file? The best way would be to break it up into a php file to display your form and a php file to handle the submit logic, named submit.php or something similar. And then in your form tag's action attribute, you would put "./submit.php"

    -or-

    If you are set on doing everything in the same page, wrap all of your submit logic inside of an if...

    if(isset($_POST['uname'])){
        // write to the database inside of here
    }
    

    that way, you are only writing to the database if there is something in your $_POST array.