I want to add a back button to this php code I'm using. So once I enter in details and click "Register", I get ""New record is inserted sucessfully".
Which is great but I'd like to add a back button that can take me back to another page.
Below is my code. I have no idea where to place the back button either
<?php
$FirstName = filter_input(INPUT_POST, 'FirstName');
$LastName = filter_input(INPUT_POST, 'LastName');
$username = filter_input(INPUT_POST, 'username');
$Email = filter_input(INPUT_POST, 'email');
$password = filter_input(INPUT_POST, 'password');
if (!empty($username)){
if (!empty($password)){
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "test";
// Create connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_errno() .') '
. mysqli_connect_error());
}
else{
$sql = "INSERT INTO details (Username, Password, FirstName, LastName, Email)
values ('$username','$password','$FirstName','$LastName','$Email')";
if ($conn->query($sql)){
echo "New record is inserted sucessfully";
}
else{
echo "Error: ". $sql ."
". $conn->error;
}
$conn->close();
}
}
else{
echo "Password should not be empty";
die();
}
}
else{
echo "Username should not be empty";
die();
}
?>
if ($conn->query($sql)){
echo "New record is inserted sucessfully";
echo '<button onclick="history.go(-1);">Back </button>';
}
OR
echo 'Back';
Related
I have created a simple HTML Login page.
I have created a PHP file to transfer data to MySQL database(Using MySQL workbench 8.0)
However, when I click submit, a page just open with some code from my PHP file and no data is transferred to my database.
How can I transfer data from my Login page to my MYSQL database?
I have saved my PHP file in the C://
Code for Login Page:
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="/Test1.php">
Username:<br>
<input type="text" value="username"><br>
Password:<br>
<input type="text" value="password"><br>
<input type="submit" value="submit"><br>
</form>
</body>
</html>
CODE for PHP:
<html>
<? php
$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
if (!empty($username)){
if (!empty($password)){
$host = "localhost:3306";
$dbusername = "root";
$dbpassword = "";
$dbname = "login";
// Create Connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if (mysql_connect_error()){
die ( 'Connect Error ('.mysqli_connect_errno().')'.mysqli_connect_error());
}
else {
$sql = "INSERT INTO login.user (username, password)
values ('$username', '$password')";
if ($conn->query($sql)){
echo "New record is inserted successfully";
}
else {
echo "Error:".$sql."<br>".$conn->error;
}
$conn->close();
}
}
else {
echo "Password should not be empty";
die ();
}
else {
echo "Username should not be empty";
die();
}
?>
</html>
I expect data from HTML file to be inputed in MYSQL Database but instead a new page open up with this:
query($sql)){ echo "New record is inserted successfully"; } else { echo "Error:".$sql."
".$conn->error; } $conn->close(); } } else { echo "Password should not be empty"; die (); } else { echo "Username should not be empty"; die(); } ?>
Try this:
<html>
<?php
$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
if (empty($username)) {
echo 'Username should not be empty';
exit;
}
if (empty($password)) {
echo 'Password should not be empty';
exit;
}
$host = 'localhost:3306';
$dbusername = '';
$dbpassword = '';
$dbname = '';
// Create Connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$connection = new mysqli($host, $dbusername, $dbpassword, $dbname);
} catch (Exception $exception) {
exit('Error connecting to database');
}
$connection->set_charset('utf8mb4');
try {
$statement = $connection->prepare("INSERT INTO login.user (username, password) values (?, ?)");
$statement->bind_param('ss', $username, $password);
$statement->execute();
$statement->close();
echo 'New record is inserted successfully';
} catch (Exception $exception) {
echo 'Error: ' . $exception->getMessage();
}
$connection->close();
?>
</html>
I would like to stop user registration if the registered name already exists.
Here is my code:
<?php
$username = filter_input(INPUT_POST, 'name');
$password = md5(filter_input(INPUT_POST, 'password'));
print_r($_POST['name']);
if (empty($username)){
echo "Username should not be empty"; die();
}
if (empty($password)){
echo "Password should not be empty"; die();
}
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "db_account";
//create connection
$conn = new mysqli($host, $dbusername, $dbpassword, $dbname);
// Check the database for duplicate username
$username_check_query = "SELECT * FROM t_account WHERE name='$username' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$username = mysqli_fetch_assoc($result);
if ($username) { // if user exists
if ($username['name'] === $username) {
echo "Username already exists"; die();
}
}
// Register user
if (mysqli_connect_error()) {
die('Connect Error ('. mysqli_connect_error() .') ' . mysqli_connect_error());
} else {
$sql = "INSERT INTO t_account (name, pwd)
VALUES('$username', '$password')";
if ($conn->query($sql)){
echo " Account created successfully!";
}
else{
echo "Error: ". $sql."<br>". $conn->error;
}
$conn->close();
}
}
?>
I'm still a newbie so I got no idea if its even correct. I used a part from a template and edited that. Now when I try to register, it gives me Error 500..
<form action="" method="post">
<input type="text" name="name">
<input type="text" name="password">
<button type="submit">send</button>
</form>
<?php
$username = filter_input(INPUT_POST, 'name');
$password = md5(filter_input(INPUT_POST, 'password'));
print_r($_POST['name']);
if (empty($username)){
echo "Username should not be empty"; die();
}
if (empty($password)){
echo "Password should not be empty"; die();
}
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "db_account";
//create connection
$conn = new mysqli($host, $dbusername, $dbpassword, $dbname);
// Check the database for duplicate username
$username_check_query = "SELECT * FROM t_account WHERE name='$username' LIMIT 1";
$result = mysqli_query($conn, $username_check_query);
$username_from_db = mysqli_fetch_assoc($result);
if ($username_from_db) { // if user
if ($username_from_db['name'] === $username) {
echo "Username already exists"; die();
}
}
// Register user
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_error() .') '
. mysqli_connect_error());
} else{
$sql = "INSERT INTO t_account (name, pwd)
VALUES('$username', '$password')";
if ($conn->query($sql)){
echo " Account created successfully!";
}
else{
echo "Error: ". $sql."<br>". $conn->error;
}
$conn->close();
}
?>
I need to send md5 hashed password to database.
<?php
$username = filter_input(INPUT_POST, 'name');
$password = filter_input(INPUT_POST, 'password');
print_r($_POST['name']);
if (empty($username)){
echo "Username should not be empty"; die();
}
if (empty($password)){
echo "Password should not be empty"; die();
}
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "db_account";
//create connection
$conn = new mysqli($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_error() .') '
. mysqli_connect_error());
} else{
$sql = "INSERT INTO t_account (name, pwd)
values ('{$username}','MD5({$password})')";
if ($conn->query($sql)){
echo "Account was created successfully!";
}
else{
echo "Error: ". $sql."<br>". $conn->error;
}
$conn->close();
}
?>
When I fill register form with password for example 123456 it sends it to database like this (MD5)123456 but not as a hashed code.
Also after successfull registration it only shows text after #echo, is possible to redirect user to another page?
Try to use the PHP md5() function:
$password = md5(filter_input(INPUT_POST, 'password'));
Also your statement:
$sql = "INSERT INTO t_account (name, pwd)
values ('{$username}','{$password}')";
I strongly suggest you use prepared statements. Not hard to learn and time well invested.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "youtube";
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
//if ($conn->connect_error) {
// die("Connection failed: " . $conn->connect_error);
//}
//echo "Connected successfull
if(isset($_POST['Submit']))
{
if(mysqli_num_rows(mysqli_query($conn," INSERT INTO admin where
Name='".$_POST['Name']."' , Password='".$_POST['Password']."', E-
mail='".$_POST['E-mail']."' and country='".$_POST['country']."'"))>0)
{
echo 'signup successfull';
}
else
{
echo 'incorrect username password';
}
}
?>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
<?php echo "<br>"?>
Your country is: <?php echo $_POST["country"]; ?>
<?php echo "<br>"?>
Your password is: <?php echo $_POST["pwd"]; ?>
<?php echo "<br>"?>
the problem in data inserts query.
$sql = "INSERT INTO admin(name, Password, mail,country)
VALUES ("$_POST['Name']", "$_POST['Password']", "$_POST['E-mail']","$_POST['country']")";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
try this
<?php
if(isset($_POST['ok1'])){
$nomo = filter_input(INPUT_POST, 'nomo');
$prenomo = filter_input(INPUT_POST, 'prenomo');
$emailo = filter_input(INPUT_POST, 'emailo');
$ageo = filter_input(INPUT_POST, 'ageo');
$sexeo = filter_input(INPUT_POST, 'sexeo');
$villeo = filter_input(INPUT_POST, 'villeo');
$cpo = filter_input(INPUT_POST, 'cpo');
if (!empty($nomo) && !empty($prenomo) && !empty($emailo) && !empty($ageo) && !empty($sexeo) && !empty($villeo) && !empty($cpo)){
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "srv_msn";
// Create connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_errno() .') '
. mysqli_connect_error());
}
else{
$sql = "INSERT INTO personne (CP, EMAIL, PRENOM, NOM)
values ('$cpo','$emailo','$prenomo','$nomo')";
if ($conn->query($sql)){
echo "New record is inserted sucessfully";
}
else{
echo "Error: ". $sql ."
". $conn->error;
$conn->close();
}
}
}
else {
echo "Password should not be empty";
die();
}
}
?>
if someone can please help me on this code i tried every single solution i found on google and youtube too , i have a form on my website project and i want the informations entered on the form to get inserted to mysql DB but it doesnt insert the information to the database