Signup System in PHP - php

I recently started coding a website with a login/signup system with php & mysql. But when i try to make the signup it give the error, that i didn't insert the username/email. I will leave the code of the signup page and the code of the "script":
Signup page:
<?php
require 'inner.php';
?>
<?php
require 'header.php';
?>
<?php
require 'main.php';
?>
<div class="signup-header">
<center>
<h1>Signup<span>Page</span></h1>
</center>
</div>
<div class="form2">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="text" name="username" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Username</span>
</label>
</form>
</center>
</div>
<div class="form3">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="text" name="email" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Email</span>
</label>
</form>
</center>
</div>
<div class="form4">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="password" name="password" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Password</span>
</label>
</form>
</center>
</div>
<div class="form5">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="password" name="rePassword" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Repeat Password</span>
</label>
</form>
</center>
</div>
<div class="signup-btn">
<form action="includes/signup.inc.php" method="post">
<button type="text" name="signup-submit">Register</button>
</form>
</div>
<?php
require 'footer.php';
?>
Script Page:
<?php
if(isset($_POST["signup-submit"]))
{
require 'dbhandler.php';
$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$repeatpassword = $_POST["rePassword"];
if(empty($username) || empty($email) || empty($password) ||empty($repeatpassword))
{
header("Location: ../signup.php?error=emptyfields&username=".$username."&email=".$email);
exit();
}elseif(!filter_var($email,FILTER_VALIDATE_EMAIL)&& !preg_match("/^[a-zA-Z0-9]*$/",$username)) {
header("Location: ../signup.php?error=invalidmail&username");
exit();
}elseif (!filter_var($email,FILTER_VALIDATE_EMAIL)) {
header("Location: ../signup.php?error=invalidmail");
exit();
}elseif (!preg_match("/^[a-zA-Z0-9]*$/",$username))
{
header("Location: ../signup.php?error=invalidusername");
exit();
}
elseif($ps !== $rp) {
header("Location: ../signup.php/error=passwordcheck");
exit();
}else {
$sql = "SELECT * FROM users WHERE uidUsers=?";
$stmt = mysqli_stmt_init($connect);
if (!mysqli_stmt_prepare($stmt,$sql)) {
header("Location: ../signup.php/error=connectionerror ");
exit();
}else
{
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$result_check = mysqli_stmt_num_rows();
if ($result_check > 0) {
header("Location: ../signup.php/error=usertaken ");
exit();
}else {
$sql = "INSERT INTO user(uidUsers, emailUsers, pwdUsers) VALUES (?,?,?)";
$stmt = mysqli_stmt_init($connect);
if (!mysqli_stmt_prepare($stmt,$sql)) {
header("Location: ../signup.php/error=connectionerror ");
exit();
}else {
$hash_ps = password_hash($ps,PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "sss", $email,$username,$hash_ps);
mysqli_stmt_execute($stmt);
header("Location: ../signup.php/signup=success");
exit();
}
}
}
}
}
?>
It's like PHP can't find the username/mail value.

You need to wrap whole form inside <form> element.
In your code, you have only this little part in form, so it is not sending all of the fields you need for registration:
<form action="includes/signup.inc.php" method="post">
<button type="text" name="signup-submit">Register</button>
</form>
So it will be something like:
<form action="includes/signup.inc.php" method="post"> <!-- FORM BEGINNING HERE -->
<div class="signup-header">
<center>
<h1>Signup<span>Page</span></h1>
</center>
</div>
<div class="form2">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="text" name="username" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Username</span>
</label>
</form>
</center>
</div>
<div class="form3">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="text" name="email" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Email</span>
</label>
</form>
</center>
</div>
<div class="form4">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="password" name="password" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Password</span>
</label>
</form>
</center>
</div>
<div class="form5">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="password" name="rePassword" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Repeat Password</span>
</label>
</form>
</center>
</div>
<div class="signup-btn">
<button type="text" name="signup-submit">Register</button>
</div>
</form> <!-- FORM ENDING HERE -->

Related

redirecting to another page in html

I am trying to redirect html form page after submission of data to an another html page using meta req method. Is their any other method ro redirect page as the problem with this is that it shows an intermediate page of our php command which i dont want
my html code is
<form method="POST" action="yoga_signup.php">
<div class="row" style=" margin-top:4rem; margin-left:24rem;">
<div class="col-md-6 mx-auto p-0">
<div class="card">
<div class="login-box">
<div class="login-snip"> <input id="tab-1" type="radio" name="tab" class="sign-in" checked><label for="tab-1" class="tab">Login</label> <input id="tab-2" type="radio" name="tab" class="sign-up"><label for="tab-2" class="tab">Sign Up</label>
<div class="login-space">
<div class="login">
<input type="hidden" name="for_page" value="login">
<div class="group"> <label for="user" class="label">Username</label> <input id="user" type="text" class="input" name="username" placeholder="Enter your username" required> </div>
<div class="group"> <label for="pass" class="label">Password</label> <input id="pass" type="password" name="password_1" class="input" data-type="password" placeholder="Enter your password" required> </div>
<div class="group"> <input id="check" type="checkbox" class="check" checked> <label for="check"><span class="icon"></span> Keep me Signed in</label> </div>
<div class="group"> <input type="submit" class="button" value="Sign In"> </div>
<div class="hr"></div>
my php code for the same is
<?php
session_start();
$conn= new mysqli("localhost","root","","yoga");
if($_SERVER["REQUEST_METHOD"]=="POST"){
$TypeOfRequest=$_POST['for_page'];
if($TypeOfRequest=="signup"){
$user=$_POST['username'];
$pass_1=$_POST['password_1'];
$pass_2=$_POST['password_2'];
$mail=$_POST['email'];
if($pass_1===$pass_2){
$val=" INSERT INTO yoga_login(username,password_1,password_2,email) VALUES('$user','$pass_1','$pass_2','$mail')";
if($conn->query($val)===TRUE){
echo "redirecting to login page.<meta http-equiv='refresh' content='1;login.html'>";
}else{
echo "registration unsucessfull";
}
}
if($pass_1!==$pass_2){
echo "please enter the same password";
}
}
mysqli_close($conn);
}
so is their any other method which doesnt shows the intermediate page like this
Just replace:
echo "redirecting to login page.<meta http-equiv='refresh' content='1;login.html'>";
With the PHP redirect:
header('Location: login.html');
exit; // this is normally a good idea

Link a php page to another php page

I am trying to link a make a login page using php without database but the link to the other page is not working . It is staying on the same page. No error message also.
Here is the php part of the code:
<?php
$error = "";
if(isset($_POST['MRDlogin']))
{
if(isset($_POST['username'],$_POST['password'])){
/*** You can change username & password ***/
$user = array(
"user" => "MRD",
"pass"=>"msit"
);
$username = $_POST['username'];
$pass = $_POST['password'];
if($username == $user['user'] && $pass == $user['pass']){
header("Location: http://localhost/portal/MRD.php");
}else{
$error = '<div class="alert alert-danger">Invalid Login</div>';
}
}
}
and my html form part is:
> <div class="panel-body">
> <?php echo $error; ?>
> <form accept-charset="UTF-8" role="form" method="post">
> <fieldset>
> <div class="form-group">
> <input class="form-control" placeholder="Username" name="username" type="text">
> </div>
> <div class="form-group">
> <input class="form-control" placeholder="Password" name="password" type="password" value="">
> </div>
> <input class="btn btn-lg btn-success btn-block" type="submit" value="Login">
> </fieldset>
> </form>
> </div>
The following variable cannot be found
$_POST['MRDlogin']
Add it in you button as shown below
<div class="panel-body">
<?php echo $error; ?>
<form accept-charset="UTF-8" action="page.php" role="form" method="post">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" name="username" type="text">
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="">
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="Login" name="MRDlogin">
</fieldset>
</form>
in form tag add action tag with value of page name that will process submited form:
<form action="page.php" accept-charset="UTF-8" role="form" method="post">

Php, Html5, xampp

I was trying execute this PHP file that I have in XAMPP. I am trying to update the data to the database through a form, when I enter the info and give submit button the page just reloads or I get the message all fields are required though I fill up all. Give an it a check to the code. The data I enter just doesn't get updated to the database. I've checked with the database name and table name that if I am mentioning it properly and also I've tried just checking that if I am able to connect to the database. In that case it is getting connected to the database properly.but just not updating the info the table.
<form action="" method="POST">
<div class="main">
<div class="icon-head-wthree">
<h2><span class="fa fa-heart t-w3" aria-hidden="true"></span></h2>
<h4>Donor?</h4>
</div>
<div class="form-left-to-w3l">
<input type="text" name="name" placeholder="First Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="lname" placeholder="Last Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="city" placeholder="City" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="district" placeholder="District" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="state" placeholder="State" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="Phone" placeholder="Phone 1" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="p1" placeholder="Phone 2">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="pincode" placeholder="Pincode" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="bg " placeholder="Blood Group" >
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add1 " placeholder="Address 1" >
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add2 " placeholder="Address 2" >
<div class="clear"></div>
</div>
<input type="checkbox" name="check" checked="checked">
<span class="checkmark"></span>
<label class="cont">I agree to the terms and conditions
</label>
<div class="btnn">
<input type="submit" value="submit" name="submit">Submit Request </button><br>
<div class="clear"></div>
</div>
</div>
</form>
<?php
if(isset($_POST["submit"])){
$db= 'bank';
if(!empty($_POST['name']) && !empty($_POST['lname']) && !empty($_POST['city']) && !empty($_POST['district']) && !empty($_POST['state']) && !empty($_POST['Phone']) && !empty($_POST['p1']) && !empty($_POST['pincode']) && !empty($_POST['bg']) && !empty($_POST['add1']) && !empty($_POST['add2']))
{ //added
$name=$_POST['name'];
$lname=$_POST['lname'];
$city= $_POST['city'];
$district= $_POST['district'];
$state= $_POST['state'];
$Phone= $_POST['Phone'];
$p1= $_POST['p1'];
$pincode= $_POST['pincode'];
$bg= $_POST['bg'];
$add1= $_POST['add1'];
$add2= $_POST['add2'];
$con=mysqli_connect('localhost','root','',$db) or die(mysql_error());
$sql="INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2')";
$result=mysqli_query($con,$sql);
if($result){
echo "Account Successfully Created";
} else {
echo "Failure!";
}
}
else {
echo "All fields are required!";
}
}
?>
as the last answer remove the white space in (blood group : name="bg", address1: name="add1", address2: name="add2") some of name of input tag.
<form action="" method="POST">
<div class="main">
<div class="icon-head-wthree">
<h2><span class="fa fa-heart t-w3" aria-hidden="true"></span></h2>
<h4>Donor?</h4>
</div>
<div class="form-left-to-w3l">
<input type="text" name="name" placeholder="First Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="lname" placeholder="Last Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="city" placeholder="City" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="district" placeholder="District" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="state" placeholder="State" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="Phone" placeholder="Phone 1" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="p1" placeholder="Phone 2">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="pincode" placeholder="Pincode" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="bg " placeholder="Blood Group" > // remoce white space
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add1 " placeholder="Address 1" > // remoce white space
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add2 " placeholder="Address 2" > // remoce white space
<div class="clear"></div>
</div>
<input type="checkbox" name="check" checked="checked">
<span class="checkmark"></span>
<label class="cont">I agree to the terms and conditions
</label>
<div class="btnn">
<input type="submit" value="submit" name="submit">Submit Request </button><br>
<div class="clear"></div>
</div>
</div>
</form>
<?php
if(isset($_POST["submit"]) == "submit"){
$db= 'bank';
if(!empty($_POST['name']) && !empty($_POST['lname']) && !empty($_POST['city']) && !empty($_POST['district']) && !empty($_POST['state']) && !empty($_POST['Phone']) && !empty($_POST['p1']) && !empty($_POST['pincode']) && !empty($_POST['bg']) && !empty($_POST['add1']) && !empty($_POST['add2']))
{ //added
$name=$_POST['name'];
$lname=$_POST['lname'];
$city= $_POST['city'];
$district= $_POST['district'];
$state= $_POST['state'];
$Phone= $_POST['Phone'];
$p1= $_POST['p1'];
$pincode= $_POST['pincode'];
$bg= $_POST['bg'];
$add1= $_POST['add1'];
$add2= $_POST['add2'];
$con=mysqli_connect('localhost','root','',$db) or die(mysql_error());
$sql="INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2')";
$result=mysqli_query($con,$sql);
if($result){
echo "Account Successfully Created";
} else {
echo "Failure!";
}
}
else {
echo "All fields are required!";
} }
?>
this code copy and paste.
and when ever you use the isset function please user the post's value to perfect condition like
if(isset($_POST["submit"]) == "submit")
if you have many button in one page then it will very help full thanks.
Some of your input tag names has spaces like name="bg " instead of name="bg". Same case for inputs - add1, and add2.
Updated code below. It should be working fine.
<form action="" method="POST">
<div class="main">
<div class="icon-head-wthree">
<h2><span class="fa fa-heart t-w3" aria-hidden="true"></span></h2>
<h4>Donor?</h4>
</div>
<div class="form-left-to-w3l">
<input type="text" name="name" placeholder="First Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="lname" placeholder="Last Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="city" placeholder="City" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="district" placeholder="District" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="state" placeholder="State" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="Phone" placeholder="Phone 1" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="p1" placeholder="Phone 2">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="pincode" placeholder="Pincode" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="bg" placeholder="Blood Group" >
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add1" placeholder="Address 1" >
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add2" placeholder="Address 2" >
<div class="clear"></div>
</div>
<input type="checkbox" name="check" checked="checked">
<span class="checkmark"></span>
<label class="cont">I agree to the terms and conditions
</label>
<div class="btnn">
<input type="submit" value="submit" name="submit">Submit Request </button><br>
<div class="clear"></div>
</div>
</div>
</form>
<?php
if(isset($_POST["submit"])){
$db= 'bank';
if(!empty($_POST['name']) && !empty($_POST['lname']) && !empty($_POST['city']) && !empty($_POST['district']) && !empty($_POST['state']) && !empty($_POST['Phone']) && !empty($_POST['p1']) && !empty($_POST['pincode']) && !empty($_POST['bg']) && !empty($_POST['add1']) && !empty($_POST['add2']))
{ //added
$name=$_POST['name'];
$lname=$_POST['lname'];
$city= $_POST['city'];
$district= $_POST['district'];
$state= $_POST['state'];
$Phone= $_POST['Phone'];
$p1= $_POST['p1'];
$pincode= $_POST['pincode'];
$bg= $_POST['bg'];
$add1= $_POST['add1'];
$add2= $_POST['add2'];
$con=mysqli_connect('localhost','root','',$db) or die(mysql_error());
$sql="INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2')";
$result=mysqli_query($con,$sql);
if($result){
echo "Account Successfully Created";
} else {
echo "Failure!";
}
}
else {
echo "All fields are required!";
}
}
?>
It is possible to write the INSERT INTO statement in two ways.
The first way specifies both the column names and the values to be inserted:
Step 1:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. The INSERT INTO syntax would be as follows:
Step 2:
INSERT INTO table_name VALUES (value1, value2, value3, ...);
And in your code I suppose you are trying to use the second method but you are not using all the column. (For example id column which may be auto increment )
So change your this insert code
INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2');
to step 1 as I have shown above which looks like shown below.
INSERT INTO donor (name, lname, city, district, state, phone, p1, pincode, bg, add1, add2) VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2');

PHP form not submitting correctly

I have a form that displays user data based on a query. The form is meant to allow users to update their user account information. i.e., First name, Last name, Email address, etc.
account.php - form
<form class="form-horizontal" role="form" method="post" action="">
<div class="form-group">
<label class="col-lg-3 control-label">First name:</label>
<div class="col-lg-8">
<input class="form-control" name="fName" type="text" placeholder="<?php echo $fName ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Last name:</label>
<div class="col-lg-8">
<input class="form-control" name="lName" type="text" placeholder="<?php echo $lName ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email:</label>
<div class="col-lg-8">
<input class="form-control" name="emailAddress" type="text" placeholder="<?php echo $emailAddress ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Username:</label>
<div class="col-md-8">
<input class="form-control" name="username" type="text" placeholder="<?php echo $username ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Password:</label>
<div class="col-md-8">
<input class="form-control" name="password" type="password">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Confirm password:</label>
<div class="col-md-8">
<input class="form-control" name="confirmPassword" type="password">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Update" name="updateaccount" type="button">
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
updateaccount.php
<?php
$msg = "";
if(isset($_POST["updateaccount"]))
{
$fName = $_POST["fName"];
$lName = $_POST["lName"];
$username = $_POST["username"];
$emailAddress = $_POST["emailAddress"];
$password = $_POST["password"];
$fName = mysqli_real_escape_string($db, $fName);
$lName = mysqli_real_escape_string($db, $lName);
$username = mysqli_real_escape_string($db, $username);
$emailAddress = mysqli_real_escape_string($db, $emailAddress);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$sql="SELECT emailAddress FROM users WHERE emailAddress='$emailAddress'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result) == 1)
{
$msg = "Sorry...This email already exists";
}
else
{
$query = mysqli_query($db, "INSERT INTO users (fName, lName, username, emailAddress, password)VALUES ('$fName', '$lName', '$username', '$emailAddress, '$password')");
if($query)
{
$msg = "Your account has been updated";
}
}
}
?>
The above code is called on the account.php page:
include ("getuseraccount.php");
Did I miss something?
You are using input type button instead of submit for the Update button. Use 'submit' type instead of 'button' type.
<input class="btn btn-primary" value="Update" name="updateaccount" type="button">
try
<input class="btn btn-primary" value="Update" name="updateaccount" type="submit">
Unless you're using Ajax to submit the form (which you made no mention of, you need to add the file that you are submitting the form information to.
<form class="form-horizontal" role="form" method="post" action="updateaccount.php">

how to get the data added verification message at the same page of form

i have this page named courses.php contain the form below (iam using bootstrap):
<form class="" action="coumysql.php" method="post" style="margin-left:100px;">
<input type="hidden" name="act" value="add"/>
<div class="form-group" >
<label class="control-label" for="course_id">Course Code:</label>
<div class="" >
<div class="col-xs-3">
<input type="text" class="form-control" id="email" placeholder="" name="course_id" required="">
</div>
</div>
</div> <br>
<br>
<div class="form-group">
<label class="control-label " for="course_name">Course Name:</label>
<div class="">
<div class="col-xs-3">
<input type="text" class="form-control" id="pwd" placeholder="" name="course_name" required="">
</div>
</div>
</div><br>
<br>
<div class="form-group" style="display: inline;">
<div class="col-sm-offset-1 "><br>
<button type="submit" class="btn btn-default">Add Subject</button>
</div>
</div>
</form>
the form processed at the page named coumysql.php
<?php
include 'connect.php';
if(isset($_POST['act'])){
if($_POST['act'] == 'add'){
if ($mysqli->query("INSERT INTO courses (course_id, course_name) VALUES ('".$_POST['course_id']."', '".$_POST['course_name']."');")) {
echo "data added";
}
}else if($_POST['act'] == 'delete'){
if ($mysqli->query("DELETE FROM courses WHERE course_id= ('".$_POST['course_id']."');")) {
echo "data deleted";
}
}
}
?>
i want the message "data added" or "data deleted" to be shown in courses.php after successfully form submitted to database
You could create a session() variable and pass it to the view. Like so:
<?php
include 'connect.php';
session_start();
if(isset($_POST['act'])){
if($_POST['act'] == 'add'){
if ($mysqli->query("INSERT INTO courses (course_id, course_name) VALUES ('".$_POST['course_id']."', '".$_POST['course_name']."');")) {
$_SESSION['insert']="Data has been successfully created!";
header('Location: courses.php');
}
}else if($_POST['act'] == 'delete'){
if ($mysqli->query("DELETE FROM courses WHERE course_id= ('".$_POST['course_id']."');")) {
$_SESSION['delete']="Data has been successfully deleted!";
header('Location: courses.php');
}
}
}
?>
Then catch them on courses.php like so:
<?php
session_start();
$added = $_SESSION['insert'];
$deleted=$_SESSION['delete'];
?>
<form class="" action="coumysql.php" method="post" style="margin-left:100px;">
<input type="hidden" name="act" value="add"/>
<div class="form-group" >
<label class="control-label" for="course_id">Course Code:</label>
<div class="" >
<div class="col-xs-3">
<input type="text" class="form-control" id="email" placeholder="" name="course_id" required="">
</div>
</div>
</div> <br>
<br>
<div class="form-group">
<label class="control-label " for="course_name">Course Name:</label>
<div class="">
<div class="col-xs-3">
<input type="text" class="form-control" id="pwd" placeholder="" name="course_name" required="">
</div>
</div>
</div><br>
<br>
<div class="form-group" style="display: inline;">
<div class="col-sm-offset-1 "><br>
<button type="submit" class="btn btn-default">Add Subject</button>
</div>
<?php
if(!empty($added)){
?>
<div class="alert aler-success"><p><?php echo $added;?></p></div>
<?php }elseif(!empty($deleted)){
?>
<div class="alert aler-success"><p><?php echo $deleted;?></p></div>
<?php
}?>
</div>
</form>
Note: u can customize div tag as u want..

Categories