Reputation: 37
This a sample code for registration a user needs to enter his/her first name, last name, username, email, password and re type his/her password. For some reason I cant insert any data into my database.
<?php
session_start();
$db = mysql_connect("localhost", "janrex", "webdev", "cuisine");
if(isset($_POST['SI']))
{
$firstname = mysql_real_escape_string($_POST['f_name']);
$lastname = mysql_real_escape_string($_POST['l_name']);
$username = mysql_real_escape_string($_POST['u_name']);
$email = mysql_real_escape_string($_POST['email']);
$pass1 = mysql_real_escape_string($_POST['pass1']);
$pass2 = mysql_real_escape_string($_POST['pass2']);
if($pass1 == $pass2)
{
$sql = mysql_query($db, "INSERT INTO users(fname, lname, uname, email, password) VALUES ('$f_name', '$l_name', '$u_name', '$email', '$pass1')");
header("location: home.php");
}
else
{
header("location: error.php");
}
}
?>
Upvotes: 0
Views: 1413
Reputation: 1
i know why it is happening because i faced the same problem. you have made an column unique(probably user id column) when you are inserting a value,a default value is being added to that column.but as the column is unique,it cannot contain two cells with same value.thats preventing you from performing query your command.
just delete the column and try.it will work
Upvotes: -1
Reputation: 111
Please try the following code and for Insert values in database
session_start();
$db = mysqli_connect("localhost", "janrex", "webdev", "cuisine");
if(isset($_POST['SI']))
{
$firstname = mysqli_real_escape_string($_POST['f_name']);
$lastname = mysqli_real_escape_string($_POST['l_name']);
$username = mysqli_real_escape_string($_POST['u_name']);
$email = mysqli_real_escape_string($_POST['email']);
$pass1 = mysqli_real_escape_string($_POST['pass1']);
$pass2 = mysqli_real_escape_string($_POST['pass2']);
if($pass1 == $pass2)
{
$sql = mysqli_query($db, "INSERT INTO users(fname, lname, uname, email, password) VALUES ('".$f_name."', '".$l_name."', '".$u_name."', '".$email."', '".$pass1."')");
header("location: home.php");
}
else
{
header("location: error.php");
}
}
Upvotes: 1
Reputation:
First of all validate your MySQL login data. Default MySQL in wamp don't has a password and username is root. Further as Milan stated don't use MySQL functions use mysqli, improved MySQL interface. Try this:
<?php
session_start();
// I prefer using oop
$mysql = new mysqli('localhost', 'root', '', 'database');
// Ofcourse replace them with you own details!
if(isset($_POST['SI'])){
$firstname = $mysql->real_escape_string($_POST['f_name']);
$lastname = $mysql->real_escape_string($_POST['l_name']);
$username = $mysql->real_escape_string($_POST['u_name']);
$email = $mysql->real_escape_string($_POST['email']);
$pass1 = $mysql->real_escape_string($_POST['pass1']);
$pass2 = $mysql->real_escape_string($_POST['pass2']);
if($pass1 == $pass2){
$sql = $mysql->query("INSERT INTO users(fname, lname, uname, email, password) VALUES (\"$firstname\", \"$lastname\", \"$username\", \"$email\", \"$pass1\")");
header("location: home.php");
}
else
header("location: error.php");
}
See if it works for you!
Upvotes: 1
Reputation:
Best way. Go to phpmyadmin 127.0.0.1/phpmyadmin or localhost/phpmyadmin (on wamp or xampp) then go to desired table and click insert tab. Then add values as required, then click Go. It will show a query copy that, paste it in your code and replace the data with desired variables.Done!
Upvotes: 1
Reputation: 910
It looks like you have multiple problems. You better use mysqli or PDO with prepared statement. Then you can use mysqli_real_scape_string API. Following code is an example of using mysqli with placeholder and prepared statement.
<?php
session_start();
if(isset($_POST['SI']))
{
$_user = 'root';
$_password= 'root';
$_db = 'localtest'
$_host = 'localhost';
$_port = 3306;
$link = new mysqli($_host, $_user, $_password, $_db);
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$firstname = $_POST['f_name'];
$lastname = $_POST['l_name'];
$username = $_POST['u_name'];
$email = $_POST['email'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
if($pass1 == $pass2)
{
$stmt = $link-> prepare("INSERT INTO users(fname, lname, uname, email, password) VALUES (?, ?, ?, ?, ?)");
/* Bind parameters, s - string, b - blob, i - int, etc */
$stmt -> bind_param("sssss", $firstname, $lastname, $username, $email, $pass1);
$stmt -> execute();
$link->close();
header("location: home.php");
}
else
{
header("location: error.php");
}
}
Upvotes: 0