when I insert data using the code I got the message that you are registered successfully i.e. data is inserted into table but I am unable to find data in my local database.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
require('db.php');
if (isset($_REQUEST['username'])) {
$username = "username";
$email = "email";
$password = "password";
$trn_date = date("Y-m-d H:i:s");
$query = "INSERT into `users` (username, password, email, trn_date)
VALUES ('$username', '".md5($password)."', '$email', '$trn_date')";
$result = mysqli_query($con, $query);
if($result) {
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
} else {
?>
<div class="form">
<h1>Registration</h1>
<form name="registration" action="" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" name="submit" value="Register" />
</form>
</div>
<?php }
?>
</body>
</html>
You have to learn more about php before you can ask.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
require('db.php');
if (isset($_POST['username'])){
$username= $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$trn_date = date("Y-m-d H:i:s");
$query = "INSERT into `users` (username, password, email, trn_date)
VALUES ('$username', '".md5($password)."', '$email', '$trn_date')";
$result = mysqli_query($con,$query);
if($result){
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<div class="form">
<h1>Registration</h1>
<form name="registration" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" name="submit" value="Register" />
</form>
</div>
<?php } ?>
</body>
</html>
Several issues:
The action tag in the <form> is empty. It works but you should specify the script name.
You assign to $username, $email and $password the wrong values. They should receive the value from $_REQUEST.
mysqli_query returns TRUE when there was an error. Therefore you get the message displayed it was successful.
Check the MySQL error and then you have the solution for your problem.
Related
I have this problem with the submit button in the html form where by when I click it instead of sending the form details to the database it displays the php script in a new window,I've tried everything I could still not working,please help
html code:
<!DOCTYPE html>
<html>
<head>
<title>sign up</title>
<link href="sign in.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div class="signbox">
<h1>SIGN UP HERE</h1>
<form method="" action="connect.php" method="POST">
<p>First name:</p>
<input type="text" name="firstname" placeholder="enter your first name">
<p>Email address:</p>
<input type="text" name="email" placeholder="enter your email address">
<p>Password:</p>
<input type="password" name="password" placeholder="enter your password">
<p>Confirm password:</p>
<input type="password" name="password2" placeholder="confirm your password">
<input type="submit" value="sign in">
already have an account?login here.
</form>
</div>
</body>
</html>
PHP code:
<?php
$firstname = $_POST['firstname'];
$email = $_POST['email'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
//database connection.
$conn = new mysqli('loacalhost','root','','','first_db');
if($conn->connect_error){
die('connection failed : '.$conn->connect_error);
}else{
$stmt = $conn->prepare("insert into users3(firstname, email, password, password2)
values(?, ?, ?, ?)");
$stmt->bind_param("ssss",$firstname, $email, $password, $password2);
$stmt->execute();
echo "registration complete...";
$stmt->close();
$conn->close();
}
?>
In your form tag there are 2 method attributes, should be corrected as;
<form action="connect.php" method="POST">
And also in the php code, mysqli connection should be;
$conn = new mysqli('localhost','root','','first_db');
in which the four parameter are;
$conn= new mysqli("server name","username","password","database name");
I have a login form through which a user can login to the website. I am checking whether the username and password match or not. If it matches, the user will be redirected to the Website, but if it doesn't match, an error message will be shown on the upper part of a form.
For this, I am storing the error message in a variable and then printing it to the upper part of the form. But it is not getting the value of the variable. Codes are as follows:
<?php
$link = mysqli_connect("localhost", "root", "", "youtube_project");
?>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Login Form</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="login">
<div class="login-triangle"></div>
<h2 class="login-header">Log in</h2>
<form class="login-container" name="form1" action="" method="post">
<p style="color:red;"><?php if(isset($msg)){ echo $msg; } //Printing error msg, but couldn't get it ?></p>
<p><input type="text" placeholder="Username" name="username" required></p>
<p><input type="password" placeholder="Password" name="pwd" required></p>
<p><input type="submit" name="submit1" value="Log in"></p>
</form>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<?php
if(isset($_POST['submit1'])){
$username = mysqli_real_escape_string($link, $_POST['username']);
$pwd = mysqli_real_escape_string($link, $_POST['pwd']);
$sql = mysqli_query($link, "SELECT * FROM admin_login WHERE username='$username' && password='$pwd'");
if(mysqli_num_rows($sql) >= 1){
?>
<script type="text/javascript">
window.location="demo.php";
</script>
<?php
} else {
$msg = "Invalid Username/Password combination"; //Storing error msg
}
}
?>
</body>
</html>
You need to move your PHP code at the top so that the variable $msg gets set or unset.
Below is the formatted updated code:
<?php
$link = mysqli_connect("localhost", "root", "", "youtube_project");
if(isset($_POST['submit1'])){
$username = mysqli_real_escape_string($link, $_POST['username']);
$pwd = mysqli_real_escape_string($link, $_POST['pwd']);
$sql = mysqli_query($link, "SELECT * FROM admin_login WHERE username='$username' AND password='$pwd'");
if(mysqli_num_rows($sql) >= 1){
?>
<script type="text/javascript">
window.location="demo.php";
</script>
<?php
} else {
$msg = "Invalid Username/Password combination"; //Storing error msg
}
}
?>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>
Login Form
</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="login">
<div class="login-triangle">
</div>
<h2 class="login-header">
Log in
</h2>
<form class="login-container" name="form1" action="" method="post">
<p style="color:red;">
<?php if(isset($msg)){ echo $msg; } //Printing error msg, but couldn't get it ?></p>
<p>
<input type="text" placeholder="Username" name="username" required>
</p>
<p>
<input type="password" placeholder="Password" name="pwd" required>
</p>
<p>
<input type="submit" name="submit1" value="Log in">
</p>
</form>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
</body>
</html>
I would recommend you never store plain text passwords! Please use PHP's built-in functions to handle password security.
You are wide open to SQL Injections too and should really use Prepared Statements.
I hope it can help
<?php
$link = mysqli_connect("localhost", "root", "", "youtube_project");
if(isset($_POST['submit1'])){
$username = mysqli_real_escape_string($link, $_POST['username']);
$pwd = mysqli_real_escape_string($link, $_POST['pwd']);
$sql = mysqli_query($link, "SELECT * FROM admin_login WHERE username='$username' && password='$pwd'");
if(mysqli_num_rows($sql) >= 1){
?>
<script type="text/javascript">
window.location="demo.php";
</script>
<?php
} else {
$msg = "Invalid Username/Password combination"; //Storing error msg
}
}
?>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Login Form</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="login">
<div class="login-triangle"></div>
<h2 class="login-header">Log in</h2>
<form class="login-container" name="form1" action="" method="post">
<p style="color:red;"><?php if(isset($msg)){ echo $msg; } //Printing error msg, but couldn't get it ?></p>
<p><input type="text" placeholder="Username" name="username" required></p>
<p><input type="password" placeholder="Password" name="pwd" required></p>
<p><input type="submit" name="submit1" value="Log in"></p>
</form>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
</body>
</html>
You have to initialized the php variable first before display it.so you have to write the php part on the top.
<?php
$link = mysqli_connect("localhost", "root", "", "youtube_project");
?>
<?php
if(isset($_POST['submit1'])){
$username = mysqli_real_escape_string($link, $_POST['username']);
$pwd = mysqli_real_escape_string($link, $_POST['pwd']);
$sql = mysqli_query($link, "SELECT * FROM admin_login WHERE username='$username' && password='$pwd'");
if(mysqli_num_rows($sql) >= 1){
?>
<script type="text/javascript">
window.location="demo.php";
</script>
<?php
} else {
$msg = "Invalid Username/Password combination"; //Storing error msg
}
}
?>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Login Form</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="login">
<div class="login-triangle"></div>
<h2 class="login-header">Log in</h2>
<form class="login-container" name="form1" action="" method="post">
<p style="color:red;"><?php if(isset($msg)){ echo $msg; } //Printing error msg, but couldn't get it ?></p>
<p><input type="text" placeholder="Username" name="username" required></p>
<p><input type="password" placeholder="Password" name="pwd" required></p>
<p><input type="submit" name="submit1" value="Log in"></p>
</form>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
</body>
</html>
I have a project and in this project, I mast to use the registers. I try to write many user registers but isn't show in the databases. I don't know what is the problem. Please, can you help me?
This is one of them:
form.php
<?php
session_start();
$_SESSION['massage'] = " ";
?>
<html>
<head>
</head>
<body>
<div class="module">
<fieldset>
<legend>Create an account:</legend>
<form class="flp" mathod ="POST" action="Register.php" enctype="multipart/form-data">
<div>
<input type="text" placeholder="User Name" name="username" required />
</div>
<div>
<input type="email" placeholder="Email" name="email" required />
</div>
<div>
<input type="password" placeholder="Password" name="password" required />
</div>
<div>
<input type="password" placeholder="Confrim Password" name="confrimpassword" required />
</div>
<center>
<input type="submit" name="Register" value="Go"/>
</center>
<div>
<?php echo $_SESSION['massage']; ?>
</div>
</fomr>
</fieldset>
</div>
</body>
</html>
Register.php
<?php
session_start();
$_SESSION['massage'] = " ";
//connect to database
$mysqli = new mysqli("localhost", "root", "", "accounts");
if (isset($_POST['Register'])){//to check the button
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
$password2 = mysql_real_escape_string($_POST['confrimpassword']);
if($password == $password2){
//Create User
$password = md5($_POST['password']);//hash password before storing for security purposes
$sql = "INSERT INTO account (UserName, Email, PassWord) VALUES ('$username', '$email', '$password')";
mysql_query($mysqli, $sql);
}
else
{
$_SESSION['massage'] = "There error!";
}
mysql_close($mysqli);
}
?>
I am having trouble with a some php code. I was able to get the code to work on my test website. I am now trying to register a user with a popup register menu. When I enter the information and click on my register button nothing is sent to my database table.
Heres my code.
<!doctype html>
<html lang="en">
<head>
<title>untitled.com</title>
<meta name="description" content="">
<link rel="stylesheet" type="text/css" href="frontPage.css">
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script src="js/general.js" type="text/javascript">
</script>
</head>
<body>
<h1> untitled </h1>
<h2> description of website</h2>
<div id="wrapper">
<nav id="nav">
<ul id="navigation">
<a class="button" href="" >Login</a>
<div class="popup">
CLOSE
<form>
<P><span class="title">Username</span> <input name="" type="text" /></P>
<P><span class="title">Password</span> <input name="" type="password" /></P>
<P><input name="" type="button" value="Login" /></P>
</form>
</div>
</ul>
</nav>
<?php
require('db.php');
if (isset($_POST['username'])){
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];;
$query = "INSERT into `users` (username, password, email, city, state, zip, phone) VALUES ('$username', '".md5($password)."', '$email', '$city', '$state', '$zip', '$phone' )";
$result = mysql_query($query);
if($result){
echo "<div class='form'><h3>You are registered successfully.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<a class="Regbutton" href="#" >Register</a>
<div class="Regpopup">
CLOSE
<form>
<P><span class="title">Username</span> <input name="username" type="text" /></P>
<P><span class="title">Password</span> <input name="password" type="password" /></P>
<P><span class="title">Email</span> <input name="email" type="email" /></P>
<P><span class="title">City</span> <input name="city" type="text" /></P>
<P><span class="title">State</span> <input name="state" type="text" maxlength="2" /></P>
<P><span class="title">zip</span> <input name="zip" type="text" maxlength="5" /></P>
<P><span class="title">phone</span> <input name="phone" type="text" maxlength="15"/></P>
<P><input type="submit" name="submit" value="Register" /></P>
</form>
</div>
<?php } ?>
</body>
</html>
To get the value from input field by post method, it is important to define form method. Example
<form action="" method="POST">
</form>
I'm creating a website which allows users to register an account and login with it. Now, I would like for them to be able to update their particulars like username and email. I've followed a guide here as I happened to be using the same code as him in register.php however it did not work. I'm quite a beginner in php so please bear with me! Help is appreciated.
register.php
<?php
if(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
}
$email = mysql_real_escape_string($_POST['email']);
$checkusername = mysql_query("SELECT * FROM users WHERE username = '".$username."'");
if(mysql_num_rows($checkusername) == 1)
{
echo "<h1>Error</h1>";
echo "<p>Sorry, that username is taken. Please go back and try again.</p>";
}
else
{
$registerquery = mysql_query("INSERT INTO users (username, password, email) VALUES('".$username."', '".$password."', '".$email."')");
if($registerquery)
{
echo "<h1>Success</h1>";
echo "<p>Your account was successfully created. Please click here to login.</p>";
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your registration failed. Please go back and try again.</p>";
}
}
?>
<h1>Register</h1>
<p>Please enter your details below to register.</p>
<form method="post" action="register.php" name="registerform" id="registerform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<label for="email">Email Address:</label><input type="text" name="email" id="email" /><br />
<input type="submit" name="register" id="register" value="Register" />
</fieldset>
</form>
</div>
</body>
</html>
When I enter editprofile.php, the page is blank. this is my editprofile.php
<?php
include "base.php";
session_start();
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['username']))
{
$nameuser = $_SESSION['username'];
$checkinfo = mysql_query("SELECT * FROM users WHERE username = '$nameuser'");
while($results = mysql_fetch_array($checkinfo,MYSQL_ASSOC)) {
$id = $results['id'];
$username = $results['username'];
$email = $results['email'];
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$editNAME=$_POST['username'];
$editEMAIL=$_POST['email'];
$editID=$_POST['id'];
$editquery = mysql_query("UPDATE users SET username='$editNAME' , email='$editEMAIL' WHERE id='$editID'");
if($editquery)
{
echo "<b>Success!</b>";
echo "Your profile was successfully updated. Please click here to view.";
}
else
{
echo "<b>Error</b>";
echo "<p>Sorry, your profile update failed. Please go back and try again.</p>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Edit Profile</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<form method="post" action="editprofile.php" name="editform" id="editform">
<p>Username: <input type="text" name="username" id="username" value="<?php echo $username; ?>" /></p>
<p>Email Address : <input type="email" name="email" id="email" value="<?php echo $email; ?>"></p>
<input type="submit" name="save" value="Save"/>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
</form>
</body>
<?php
include "base.php";
session_start();
?>
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['username']))
{
$nameuser = $_SESSION['username'];
$checkinfo = mysql_query("SELECT * FROM users WHERE username = '$nameuser'");
while($results = mysql_fetch_array($checkinfo,MYSQL_ASSOC)){
$id = $row['id'];
$username = $row['username'];
$email = $row['email'];
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$editNAME=$_POST['username'];
$editEMAIL=$_POST['email'];
$editID=$_POST['id'];
$editquery = mysql_query("UPDATE users SET username='$editNAME' , email='$editEMAIL' WHERE id='$editID'");
if($editquery)
{
echo "<b>Success!</b>";
echo "Your profile was successfully updated. Please click here to view.";
}
else
{
echo "<b>Error</b>";
echo "<p>Sorry, your profile update failed. Please go back and try again.</p>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Edit Profile</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<form method="post" action="editprofile.php" name="editform" id="editform">
<p>Username: <input type="text" name="username" id="username" value="<?php echo $username; ?>" /></p>
<p>Email Address : <input type="email" name="email" id="email" value="<?php echo $email; ?>"></p>
<input type="submit" name="save" value="Save"/>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
</form>
</body>
you try this....
Not 100% sure but you try..