error while inserting into mysql DB - php

I am facing simple error while making simple register to insert into mysql database with username and password.
Notice: Undefined variable: username in C:\xampp\htdocs\x\index.php on line 42
Notice: Undefined variable: password in C:\xampp\htdocs\x\index.php on line 42
Registerd Successfully
<!DOCTYPE html>
<html>
<head>
<title>THIS IS MY FIRST LOGIN PAGE</title>
<link rel="stylesheet" type="text/css" href="style.css"></link>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<div class="container">
<img src="men.png" alt="missing">
<form method="post" >
<div class="font-input">
<input type="text" name="name" id="name" placeholder="Enter Name">
</div class="font-input">
<div>
<input type="password" name="pass" id="pass" placeholder="Enter Password">
</div>
<div>
<input type="button" name="login" value="Login">
</div>
</form>
</div>
</body>
</html>
<?php
$conn = mysqli_connect('localhost','root','','y');
if(!$conn)
{
die('Connection failed!'.mysqli_error($conn));
}
if (isset($_POST['name'], $_POST['pass']))
{
$username = $_POST['name'];
$password = $_POST['pass'];
}
$sql = "INSERT INTO hack(myusername,mypassword) VALUES('$username', '$password')";
if(mysqli_query($conn,$sql))
{
echo "Registerd Successfully";
}
else
{
echo mysqli_error($conn);
}
?>

Try this :
We check if username and password are sent with the form, if so, we insert them into our database if there's no error.
Code :
if((isset($_POST['name'])) && (isset($_POST['pass'])))
{
$username = $_POST['name'];
$password = $_POST['pass'];
$sql = "INSERT INTO hack(myusername, mypassword) VALUES('$username', '$password')";
if(mysqli_query($conn,$sql))
{
echo "Registerd Successfully";
}
else
{
echo mysqli_error($conn);
}
}

Related

Registration works but somehow login not working

I am new to PHP and was following a login_registration script the registration process works fine and the user is added to the database, however, when I try to log in it shows error I checked php.error log on wamp server and it says this,
PHP 1. {main}() C:\wamp64\www\php\login_register_system\login.php:0
Here is my code
connect.inc.php
<?php
//$conn_error = "could not connect";
$mysql_host= "localhost";
$mysql_user = "root";
$mysql_pass ="";
$mysql_db ="a_database";
$conn = mysqli_connect($mysql_host,$mysql_user,$mysql_pass,$mysql_db);
/*if(!mysqli_connect($mysql_host,$mysql_user,$mysql_pass) && !mysqli_select_db($mysql_db)){
die($conn_error);
}
*/
if(!$conn){
die("Connection failed: ". mysqli_connect_error());
}
?>
registration.php
<!DOCTYPE html>
<html>
<head>
<meta charset ="utf-8">
<title>Registartion</title>
<link rel ="stylesheet"href="style.css"/>
</head>
<body>
<?php
require "connect.inc.php";
if(isset($_REQUEST["username"])){
$username =stripslashes($_REQUEST["username"]);
$username =mysqli_real_escape_string($conn,$username);
$email =stripslashes($_REQUEST["email"]);
$email =mysqli_real_escape_string($conn,$email);
$password =stripslashes($_REQUEST["password"]);
$password =mysqli_real_escape_string($conn,$password);
$trn_date = date("Y-m-d H:i:s");
$query ="INSERT INTO `users2` (username,password,email,trn_date) VALUES('$username','".md5($password)."','$email','$trn_date')";
$result =mysqli_query($conn,$query);
if($result){
echo "<div class='form'>
<h3>You are registered succesfully</h3><br>
Click here to <a href='login.php'>Log in</a>
</div>";
}
}else{
?>
<div class="form">
<h1>Registration</h1>
<form name="registration" action ="registration.php" method="post">
<input type="text" name ="username" placeholder ="Username" required/>
<input type="text" name ="email" placeholder ="Email" required/>
<input type="password" name ="password" placeholder ="password" required/>
<input type="submit" type="submit" value="Register"/>
</form>
</div><!--form-->
<?php } ?>
</body>
</html>
login.php
<!DOCTYPE html>
<html>
<head>
<meta charset ="utf-8">
<title>Login</title>
<link rel ="stylesheet"href="style.css"/>
</head>
<body>
<?php
//ini_set('display_errors','1');
//error_reporting(E_ALL);
require "connect.inc.php";
session_start();
if(isset($_POST["username"])){
$username = stripslashes($_REQUEST["username"]);
$username = mysqli_real_connect($conn,$username);
$username = mysqli_real_escape_string($conn,$password);
$query ="SELECT * FROM `users` WHERE username ='$username' and password ='".md5($password)."' ";
$result =mysqli_query($conn,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if($rows==1){
$_SESSION["username"] = $username;
header("Location: index.php");
}else{
echo "<div class='form'>
<h3>Username/password is incorrect</h3><br>
Click here to <a href='login.php'>Login</a>
</div>";
}
}else{
?>
<div class="form">
<h1>Login </h1>
<form action="login.php" method="post" name="login">
<input type="text" name ="username" placeholder ="Username" required/>
<input type="passowrd" name ="password" placeholder ="password" required/>
<input type="submit" type="submit" value="Login"/>
</form>
</div>
<?php } ?>
</body>
</html>
logout.php
<?php
session_start();
if(session_destroy()){
header("Location: login.php");
}
?>
index.php
<?php
include("auth.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Welcome home</title>
</head>
<body>
<div class="form">
<p>Welcome <?php echo $_SESSION["username"];?></p>
<p>This is secure area</p>
Logout
</div>
</body>
</html>
Any help will be highly appreciated. Thanks.
Do note this is just for learning the purpose. I know there are flaws but please be easy.
Just change this,
$username = stripslashes($_REQUEST["username"]);
$username = mysqli_real_connect($conn,$username);
$password =stripslashes($_REQUEST["password"]);
$password = mysqli_real_escape_string($conn,$password);
to the following,
$username = stripslashes($_REQUEST['username']);
$username = mysqli_real_escape_string($conn, $username);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($conn, $password);
I hope you have only one user related to the entered credentials in the database. Hope this helps you!
thanks i got it resolved..problem was this line $query ="SELECT * FROM users WHERE username ='$username' and password ='".md5($password)."' "; in login.php..
correct should be this
$query ="SELECT * FROM `users2` WHERE username ='$username' and password ='".md5($password)."' ";
i was checking data in users table whereas i registered in users2 table..as i had many tables that created confusion

The data is not registered to the database

I want to make a student registration form. I get no error messages, but the data is not entered into the database.
INDEX is for INDEX NUMBER
USERNAME is for USER
EMAIL is for EMAIL
I had trouble with index variable it said that it is undefined, I am not sure if I defined the variable properly.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!-- TemplateBeginEditable name="doctitle" -->
<title>Untitled Document</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head>
<body>
<?php
session_start();
$_SESSION["message"] = "";
$mysqli = new MySQLi("localhost","root","","accounts");
if ($_SERVER['REQUEST_METHOD'] == "POST"){
$username = $mysqli->real_escape_string($_POST["username"]);
$email = $mysqli->real_escape_string($_POST["email"]);
$index = ($_POST["email"]);
$_SESSION["username"] = $username;
$_SESSION["email"] = $email;
$sql = "INSERT INTO users (index, email, name) "
. "VALUES ('$index', '$email', '$username')";
//check if mysql query is successful
if ($mysqli->query($sql) === true)
{
$_SESSION[ 'message' ] = "Registration succesful! Added $username to the database!";
//redirect the user to welcome.php
header( "location: welcome.php" );
}
else{
$_SESSION["message"] = "user could not be added to the database";
}
}
else{
$_SESSION["message"] = "could not initiate session";
}
?>
<link href="//db.onlinewebfonts.com/c/a4e256ed67403c6ad5d43937ed48a77b?family=Core+Sans+N+W01+35+Light" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="form.css" type="text/css">
<div class="body-content">
<div class="module">
<h1>Create an account</h1>
<form class="form" action="form.php" method="post" enctype="multipart/form-data" autocomplete="off">
<div class="alert alert-error"></div>
<input type="text" placeholder="User Name" name="username" required />
<input type="email" placeholder="Email" name="email" required />
<br>
<input type="text" placeholder="Index Number" name="index" required /><br>
<input type="submit" value="Register" name="register" class="btn btn-block btn-primary" />
</form>
</div>
</div>
</body>
</html>
Change this line :
$sql = "INSERT INTO users (index, email, name) "
. "VALUES ('$index', '$email', '$username')";
To this :
$sql = 'INSERT INTO users (index, email, name) '
. 'VALUES ("$index", "$email", "$username")';
Hope to solve your problem.

My php code does not post to database on server but it is working on localhost

This is the code. It is working on localhost, but when I upload it to my server the PHP does not send information to MySQL.
<?php
session_start();
include('connect.php');
if(isset($_POST['signup'])){
echo "<pre>"; print_r($_POST); echo "</pre>";
$name=$_POST['name'];
$email=$_POST['email'];
$password=md5($_POST['password']);
$contact=$_POST['contact'];
$city=$_POST['city'];
$address=$_POST['address'];
$signup_sql=mysqli_query($con,"select * from signup where email='".$email."'");
//$signup_res=mysqli_query($con,$signup_sql);
//$signupresult=mysqli_fetch_array($signup_res);
$dataa = mysqli_fetch_array($signup_sql);
if(empty($signup_res)){
$sql="insert into signup (name,email,password,contact,city,address) values ('".$name."','".$email."','".$password."','".$contact."','".$city."','".$address."')";
$res=mysqli_query($con,$sql);
if($res>=1){
#header("location:login.php");
}
else{
#header("location:index.php");
}
}else{
#header("location:index.php");
}
}
?>
You're not testing the select query correctly. if(empty($signup_res)) only tests if the query got an error. But a query that doesn't match anything is not an error, it's just an empty result.
You should check the result of fetching the result:
if(empty($dataa))
And then when you perform the insert query, you're not checking its success correctly, either. $res will be either TRUE or FALSE, not a number. If you want to redirect to login.php if the insert failed, it should be:
if (!$res) {
header("location: login.php");
} else {
header("location: index.php");
}
I also recommend that while debugging you remove the redirect and display the error message when the query fails. You also shouldn't use # while debugging (and there's little reason to use # for header() calls anyway).
YiPp i fixed it by by changing code to
<?php
session_start();
header('location:login.php');
$con = mysqli_connect('localhost','id8127977_decors','Priya#1994');
if($con){
echo" connection successful";
}else{
echo " no connection";
}
mysqli_select_db($con, 'id8127977_decors');
$name=$_POST['name'];
$email=$_POST['email'];
$password=md5($_POST['password']);
$contact=$_POST['contact'];
$city=$_POST['city'];
$address=$_POST['address'];
$q = " select * from signup where email = '$email' ";
$result = mysqli_query($con, $q);
$num = mysqli_num_rows($result);
if($num == 1){
echo" duplicate data ";
}else{
$qy= "insert into signup (name,email,password,contact,city,address) values ('".$name."','".$email."','".$password."','".$contact."','".$city."','".$address."')";
mysqli_query($con, $qy);
}
?>
<!doctype html>
<html>
<head lang="en">
<meta charset="utf-8">
<title>File Upload PHP</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="js/jquery-1.11.3-jquery.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8">
<form id="form" action="ajaxupload.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name">NAME</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" required />
</div>
<div class="form-group">
<label for="email">EMAIL</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" required />
</div>
<input id="uploadImage" type="file" accept="image/*" name="image" />
<div id="preview"><img src="filed.png" /></div><br>
<input class="btn btn-success" type="submit" value="Upload">
</form>
<div id="err"></div>
</div>
</div>
</div></body></html>

Notice: Undefined index: username in F:\wamp64\www\practise\insert.php on line 16

I have done this from one of the youtube channel .. ii just copied all the code but still its not working. can you anyone can help me on this. I have facing the error at end saying NOT INSERTED.
**index.php**
<html>
<head>
<title>Data entry practise</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="wrapper">
<form action="insert.php" method="_POST">
Name: <input type="text" name="username"><br><br>
Email Address: <input type="text" name="email"><br><br>
<input type="submit" value="insert">
</form>
</div>
</body>
</html>
Now this is the inset.php code section. seems like here is some error .. please help me out
**insert.php**
<?php
$con = mysqli_connect('localhost','root','');
if(!$con)
{
echo "not connected";
}
if(!mysqli_select_db($con,'tutorial'))
{
echo 'database Note Selected';
}
$Name = $_POST['username'];
$Email = $_POST['email'];
$sql = "INSERT INTO person (Name,Email) VALUES ('$Name', '$Email')";
if(!mysqli_query($con, $sql))
{
echo "Not Inserted";
}
else
{
echo "Inserted";
}
?>
The problem is that the form method is incorrect. You have to write it without the underscore
**index.php**
<html>
<head>
<title>Data entry practise</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="wrapper">
<form action="insert.php" method="POST">
Name: <input type="text" name="username"><br><br>
Email Address: <input type="text" name="email"><br><br>
<input type="submit" value="insert">
</form>
</div>
</body>
</html>
EDIT:
I think the problem is on your mysql connection. You need to add the db on the $con variable, like this:
insert.php
$con = mysqli_connect('localhost','root','', 'tutorial');
if(!$con)
{
echo "not connected";
}
if(!mysqli_select_db($con,'tutorial'))
{
echo 'database Note Selected';
}
$Name = $_POST['username'];
$Email = $_POST['email'];
$sql = "INSERT INTO person (Name,Email) VALUES ('$Name', '$Email')";
if(!mysqli_query($con, $sql))
{
echo "Not Inserted";
}
else
{
echo "Inserted";
}
?>
I tested with your code and it is working.
One more thing, you need to change the form method to "POST", without underscore, it is important.

Trouble with recording information in sql tables from Html Forms

I have created a html register page which is really basic and requires the user to enter their First name, Last name, email, and password. However only the first and last names are being recorded in the database in phpmyadmin and the email and passwords are showing as blank cloumns. I have tried to drop and add the tables and columns again without any luck, i have changed variable names and no luck as well. Not too sure what to do.
Php code
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$password_ = $_POST['password_'];
if (isset($_POST['register'])) {
register($first_name,$last_name,$_email,$password,$conn);
}
$conn->close();
function register($first_name,$last_name,$email,$password,$conn) {
// echo $first_name . " " . $last_name . " " . $student_id . " " . $email;
$sql = "INSERT INTO `register` (`first_name`, `last_name`, `email`, `password_`) VALUES ('$first_name', '$last_name', '$email', '$password_')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
html code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Case</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<form role="form" action="register.php" method="POST">
<div class="form-group">
<label>First Name:</label>
<input type="text" class="form-control" id="first_name" name="first_name"required>
</div>
<div class="form-group">
<label>Last Name:</label>
<input type="text" class="form-control" id="last_name" name="last_name"required>
</div>
<div class="form-group">
<label>Email address:</label>
<input type="varchar" class="form-control" name="e_mail" id="email"required>
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" class="form-control" id="password" name="password"required>
</div>
<div class="form-group">
<label>Confirm Password:</label>
<input type="password" class="form-control" id="confirm_password"required >
<script>
var password = document.getElementById("password")
, confirm_password = document.getElementById("confirm_password");
function validatePassword(){
if(password.value != confirm_password.value) {
confirm_password.setCustomValidity("Passwords Don't Match");
} else {
confirm_password.setCustomValidity('');
}
}
password.onchange = validatePassword;
confirm_password.onkeyup = validatePassword;
</script>
</div>
<button type="submit" class="btn btn-default" name="register">Register</button>
</form>
</body>
</html>
In order to grab the _POST variables, your input forms must have a name attribute. For your Email form, you have only specified an ID and no name. Go back and add in the name='email' attribute and it should work. Same for password.
It looks like i was missing an underscore in the php register code where it states the function. i have added it and now my code seems to be working, thanks for all the feedback!

Categories