Insert in a database table

Being a new one in php I am trying to learn simple login example from the video from “” but this code to insert data in the table named “user_info” in the database “users” is not working. But in his vide it seems it is working, may be I am doing something wrong. can anyone looked into the code below.


USERS LOGIN SYSTEM <?php if(!isset($_POST['submit'])) { ?>
    <form action="index.php" method="post">
    <table border="1">
    <td>Username</td><td><input type="text" name="username"></td>
    <td>Password</td><td><input type="password" name="password"></td>
    <td>Password Confirm</td><td><input type="password" name="passwordconf"></td>
    <td>Email</td><td><input type="text" name="email"></td>
    <td colspan="2" align="center">
    <input type="submit" value="CreateUser" name="submit"></td>
<?php } else { $username=$_POST['username']; $password = $_POST['password']; $passwordconf = $_POST['passwordconf']; $email = $_POST['email']; $errors = array(); if(!$username) { $errors[1] = "You have not entered your username.";} if(!$password) { $errors[2] = "You have not entered your password.";} if(!$passwordconf) { $errors[3] = "You have not enetered your password confirmation.";} if($password !=$passwordconf) { $errors[4] = "You password and password confirmation mismatched.";} if(!$email) { $errors[5] = "You have not enetered your email."; } if( count($errors)>0) { foreach($errors as $error){ echo "$error
"; } } else{ mysql_query("INSERT INTO 'users'.'user_info' ('username','password','email') VALUES('".$username."', '".md5($password)."','".$email."');"); } } ?> [/code]

From what i can see you arnt selecting a database, under mysql_connect
Put mysql_select_db(“put database name here”);

Where to put this one?

At the top of your script

<?php mysql_connect('localhost','root',''); mysql_select_db("YOURDATABASENAME"); ?>


Thanks for the reply.

I done the same


“users” is my database name, but sorry to say nothing happened. Shall be glad if you kindly test my code at your end where I am doing wrong!


Amendment of my previous post

I did this one

<?php mysql_connect('localhost','root',''); mysql_select_db("users"); ?>

OK, i think ive seen the problem, i didn’t read all the code last time as i thoguht it was the database not being selected, i believe your query is wrong. Keep the mysql_select_db(“users”) at the top only change the query as showen below the quote.

mysql_query("INSERT INTO 'users'.'user_info'
  VALUES('".$username."', '".md5($password)."','".$email."');");</blockquote>

You dont need to say the database now in the query so try this…

// user_info is the table now im guessing. you only need to say table names this insert not the database name and the table name. Also md5 isn’t very good it can be broken easily using a rainbow table, read up about adding a random salt to the password or change it to another hash like sha256 for example.

mysql_query("INSERT INTO user_info

  VALUES('".$username."', '".md5($password)."','".$email."');");


I did the send thing you wrote but till the data is not entered into the database, can you please take some pain for me and test this one at your end, what is the wrong I am doing. It is really a headache for me. I am new just trying to learn this language and at the very first step I could not come over this hurdle. The video I am trying is from “” the registration and login video number two (2).

Sorry for so much trouble from me.


try this ;D just edit it in to where you need it


<?php $con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("INSERT INTO user_info (username, password, email) VALUES ('".$username."', '".md5($password)."','".$email."'"); mysql_close($con); ?>


Be sure to use backticks (`) not single quotes for table and column names.

This is wrong (using single quotes)

INSERT INTO 'users'.'user_info' ('username','password','email')

This is correct (using backticks)

INSERT INTO `users`.`user_info` (`username`,`password`,`email`)

Sorry it is not working. Can u please test all my code , why it is not working.

Thanks for your reply.

Sponsor our Newsletter | Privacy Policy | Terms of Service