I was trying to create an update system , but I don't know why my code is not working . My code are :
<?php
include "dbconnection.php";
if(isset($_POST['submit'])){
$residency_status = $_POST['residency_status'];
$query="UPDATE profile SET
residential_status='$residency_status'
WHERE id = '".user_id."'
";
}
header('location:profile.php');
?>
I am adding some Code from profile.php page :
<?php
session_start();
include_once ('dbconnection.php');
if(!isset($_SESSION['logged_in'])){
header("Location: login.php");
die();
}
$usrname=$_SESSION['username'];
$pasword=$_SESSION['password'];
$user_id= get_user_id($usrname, $pasword);
while($us_id= $user_id->fetch_assoc()) :
$collected_id=$us_id['id'];
$resi_status=$us_id['residential_status'];
endwhile;
?>
The code for the form is : I am trying to keep the code short to avoid
<form class="form-group row" action="get_update.php" method="POST"
enctype="multipart/form-data">
<h4>Residential Status </h4>
<div class="form-group">
<input type="radio" name="residency_status" value="yes" class=""
id=""
<?php
if ($resi_status == "yes") { ?> checked <?php }
?>
>yes
<input type="radio" name="residency_status" value="No" class="" id=""
<?php
if ($resi_status == "no" || $resi_status == "") { ?> checked
<?php } ?> >no
</div>
<br>
Thank you .
You are missing out on mysqli_query.Your code should be as
<?php
include "dbconnection.php";
if(isset($_POST['submit'])){
$residency_status = $_POST['residency_status'];
$query="UPDATE profile SET
residential_status='$residency_status'
WHERE id = '".user_id."'";
mysqli_query($conn,$query); // $conn must be as per your `mysqli_connect` variable
}
header('location:profile.php');
?>
Related
there are two inputs that should show interactions. Everything works, but when I want to refresh the page I add unset($_SESSION['one'], $_SESSION['second']) an error appears
Without unset($_SESSION['one'], $_SESSION['second'])
But you still need to reset everything when you refresh the page
search.php
<?php
session_start();
?>
<body>
<form action="new.php" method="post">
<div>
<input type="search" name='search1' list="doc" id="inp1">
<datalist id='doc'>
<option value="diclo">diclo</option>
<option value="keto">keto</option>
</datalist>
</div>
<div>
<input type="search" name='search2' list="doc2" id="inp2">
<datalist id='doc2'>
<option value="diclo">diclo</option>
<option value="keto">keto</option>
</datalist>
</div>
<div>
<button name='btn'>Save</button>
</div>
</form>
<?php
if(isset($_SESSION['error'])) { ?>
<div> <?= $_SESSION['error'] ?></div>
<?php }
unset($_SESSION['error']);
if(isset($_SESSION['one']) && isset($_SESSION['second'])) { ?>
<div>
<div>
<p><?= $_SESSION['one'] ?></p>
</div>
<p><?= $_SESSION['second'] ?></p>
<form action="del.php" method='post'>
<button name='btn1'>INTERACTION</button>
</form>
</div>
<?php }
?>
<?php
if (isset($_SESSION['yes'])) {
$getDrugInt = $model->getDrugInt($_SESSION['one'], $_SESSION['second']);
for ($i = 0; $i < count($getDrugInt); $i++) { ?>
<div>
<p> <?= $getDrugInt[$i]['profDescription']?> </p>
</div>
<?php }
}
unset($_SESSION['yes']);
?>
<?php
unset($_SESSION['one'], $_SESSION['second']); ?>
new.php
session_start();
$arr = array('diclo', 'keto');
if(isset($_POST['btn'])) {
unset($_SESSION['one'], $_SESSION['second']);
$inputone = $_POST['search1'];
$inputsecond = $_POST['search2'];
if(in_array($inputone, $arr) && in_array($inputsecond, $arr)) {
$_SESSION['one'] = $inputone;
$_SESSION['second'] = $inputsecond;
header('location: search.php');
} else {
$_SESSION['error'] = 'No results found';
unset($_SESSION['one'], $_SESSION['second']);
header('location: search.php');
}
}
del.php
session_start();
if(isset($_POST['btn1'])) {
$_SESSION['yes'] = true;
header('location: search.php');
} else {
$_SESSION['yes'] = false;
}
Thank you.
I wrote a simple login form website with change language in PHP.
When I run in localhost, I can login after I fill in the registration number and password. I can also change the language to Chinese/English after I click on the hyperlink.
However, after I deploy the PHP files to server, the functions are not working.
After I click on the English hyperlink, the login page will not change to English and remains as the default language (Chinese).
The login form is not working also, I have to click on the any of the Chinese/English hyperlink and fill in the login form, then I can login to the dashboard.
After login, the dashboard language will show in the language I selected in the index.php. I have no idea on why the index.php will not change to the language I wanted. It is working fine in localhost.
Below are the codes. I appreciate if anyone can advise me on this.
Thanks a lot.
index.php
<?php
session_start();
include 'includes/language.php';
?>
<html>
<div class="container2" style="justify-content: center;">
<a href="change-Language.php?language=en">
<?php echo getLanguage($resultAll, 'nav-english'); ?>
</a> |
<a href="change-Language.php?language=zh">
<?php echo getLanguage($resultAll, 'nav-chinese'); ?>
</a>
</div>
<div class="container">
<label for="regno">
<b><?php echo getLanguage($resultAll, 'nav-regno'); ?></b>
</label>
<input type="text" placeholder="<?php echo getLanguage($resultAll, 'nav-regno'); ?>" name="regno">
<label for="psw">
<b><?php echo getLanguage($resultAll, 'nav-password'); ?></b>
</label>
<input type="password" placeholder="<?php echo getLanguage($resultAll, 'nav-password'); ?>" name="password">
<button type="submit">
<?php echo getLanguage($resultAll, 'nav-login'); ?>
</button>
</div>
</html>
includes/language.php
<?php
if (!isset($_SESSION['language']))
{
$_SESSION['language'] = "zh";
}
if ( isset($_SESSION['language']) && $_SESSION['language'] == "en")
{
$query = $pdo->prepare("SELECT message_code, message_en as msg FROM tbl_language");
}
if ( isset($_SESSION['language']) && $_SESSION['language'] == "zh")
{
$query = $pdo->prepare("SELECT message_code, message_zh as msg FROM tbl_language");
}
$results = $query->execute();
$resultAll = $query->fetchAll(\PDO::FETCH_ASSOC);
function getLanguage($resultAll, $key)
{
foreach ($resultAll as $row)
{
if ($row['message_code'] == $key)
{
echo $row['msg'];
}
}
}
?>
change-Language.php
<?php
session_start();
$language = $_GET['language'];
$_SESSION['language'] = $language;
$referer = $_SERVER['HTTP_REFERER'];
header("Location: $referer");
?>
I'm having some trouble with a Form. It contains an input type File for uploading an image, and a select tag to choose where to put this image.
Code goes like this:
<?php
require_once("functions.php");
if(isset($_COOKIE['user']))
{
$username= checkcookie($_COOKIE['user']);
}
if (isset($_SESSION['user'])) {
if ($_POST) {
$errors = array();
$errors = SomeValidation();
if (empty($errors )) {
UpdateImages();
exit;
}else {
Header ("location: somefile.php?error=There was an error");
exit;
}
}else{
var_dump("error");
} ?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
<?php if(isset($_GET['error'])) {?>
<div class="alert alert-danger" role="alert">
<ul>
<li><?php echo $_GET['error'] ?></li>
</ul>
</div>
<?php } ?>
<div class="cbp-mc-column">
<label>Choose new Image</label>
<select id="code" name="code">
<option value="1" >First</option>
<option value="2" >Second</option>
<option value="3" >Third</option>
</select>
<br/>
<input type="file" name="newimage" value="" />
<br>
<input type="submit" name="update" value="Update">
<br>
</div>
</form>
<!-- Bootstrap Core JavaScript -->
<script src="../js/bootstrap.min.js"></script>
</body>
</html>
<?php }else
{
header('location: panel.php');
}
?>
I'm always getting False for if($_POST), and also when I submit the form.
Any idea why?
EDIT: I forgot to mention, I'm sending the select value via post, so I can choose which file upload, and It is always empty.
to upload image or any other file you need to check the $_FILES array not post,
this question could be helpfull
How do you loop through $_FILES array?
It is better to check inputs instead of request method.. so use
if (!empty($_POST["update"])) {
....
}
First, you need to check if the submit button was clicked :isset($_POST['update'])
Next, check if a file has been selected: isset($_FILES['newimage']['tmp_name'])
if (isset($_POST['update'])) {
if (isset($_FILES['newimage']['tmp_name'])) {
$errors = array();
$errors = SomeValidation();
if (empty($errors )) {
UpdateImages();
exit;
} else {
Header ("location: somefile.php?error=There was an error");
exit;
}
} else {
echo "No file selected.";
}
} else {
echo "Not a POST request.";
}
I make a login page like user login. if registered then login with email and password(navigate register.php to index.php page) or sign up if not registered. works good but i want to add update functionality in my index.php page concept is simple if user wants to update its record then he make updations in a form and submit. I make a update query here but its shows Notice: Undefined index: id in C:\wamp\www\LOginKodingMadSimple\index.php on line 34. Where i am wrong ?
index.php :
<?php
session_start();
include_once 'dbconnect.php';
?>
<?php
if (isset($_POST['UpdateUser']))
{
$name = mysqli_real_escape_string($con, $_POST['name']);
$id = mysqli_real_escape_string($con, $_POST['id']);
//------- name can contain only alpha characters and space -------
if (!preg_match("/^[a-zA-Z ]+$/",$name))
{
$error = true;
$name_error = "Name must contain only alphabets and space";
}
//------- Update users -------
if (!$error)
{
if(mysqli_query($con, "UPDATE users SET name='" . $name . "' WHERE id='" . $id . "'"))
{
$successmsg = "Successfully Updated! ";
}
else
{
$errormsg = "Error in Update...Please try again later!";
}
}
}
else
{
$errormsg = "Failed Please Try Again Later !";
}
?>
<div class="collapse navbar-collapse" id="navbar1">
<ul class="nav navbar-nav navbar-right">
<?php if (isset($_SESSION['usr_id'])) { ?>
<li>
<p class="navbar-text">Signed in as <?php echo $_SESSION['usr_name']; ?></p>
<a class="navbar-brand" href="updatephp.php">Update</a>
</li>
<li>
Log Out
</li>
<div style="border:1px dashed transparent;margin-top:400px;position:absolute;margin-left:-35%;">
<h1> Hello User <?php echo $_SESSION['usr_name']; ?> ! </h1>
<h1> Your Email is <?php echo $_SESSION['usr_email']; ?> ! </h1>
<form action="" method="post">
Name: <input type="text" name="name" placeholder="<?php echo $_SESSION['usr_name']; ?>"><br>
<input type="submit" value="Update" name="UpdateUser">
</form>
</div>
<?php } else { ?>
<li>Login</li>
<li>Sign Up</li>
<?php } ?>
</ul>
</div>
add input hidden field with id like.
<input type="hidden" name="id" value="<?php echo $_SESSION['usr_id'];?>">
[NOTE: You don't have any textbox named as id.]
Choose any way.
Way 1)
Change
$id = mysqli_real_escape_string($con, $_POST['id']);
To
$id = $_SESSION['usr_id'];
Updated Code:
<?php
if (isset($_POST['UpdateUser'])) {
$name = mysqli_real_escape_string($con, $_POST['name']);
$id = $_SESSION['usr_id'];
Way 2)
Add one hidden input field having value session usr_id.
<form action="" method="post">
Name: <input type="text" name="name" placeholder="<?php echo $_SESSION['usr_name']; ?>"><br>
<input type='hidden' value="<?php echo $_SESSION['usr_id'];?>" name='id'>
<input type="submit" value="Update" name="UpdateUser">
</form>
In this way, your submitting form code will be same as you are having.
Use below function to hide the error notice :
error_reporting(0);
I'd like to know if it is possible to manage error if in the form, action is set to another script.
ajoutDevis.php
<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
<label for="client">Client</label>
<input type='text' id='client' name='client'>
<span class="error">* <?php echo $clientErr;?></span>
</FORM>
ajoutDevisScript.php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST['client'])) {
$clientErr = "ERROR";
} else $client = $_POST['client'];
So, is it possible to display my error in the span if the input is empty ? Or there is no way doing it with an other script as action ?
Thanks!
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST['client'])) {
$clientErr = "ERROR";
$_SESSION['error] = $clentErr;
header('location:ajoutDevis.php');
} else $client = $_POST['client'];
In your ajoutDevis.php file
session_start();
$clientErr = ($_SESSION['error']!='')?$_SESSION['error']:'';
<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
<label for="client">Client</label>
<input type='text' id='client' name='client'>
<span class="error">* <?php echo $clientErr;?></span>
</FORM>
You need to do the following:
Add the form code as well on the ajoutDevisScript.php page at the bottom of page.
<?php if(!empty($clientErr)) { ?>
<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
<label for="client">Client</label>
<input type='text' id='client' name='client'>
<span class="error">* <?php echo $clientErr;?></span>
</FORM>
<?php } ?>
This will check if there is any error in error variable it will display the form on same page with error.
Yes, it is possibile with $_SESSION variables to store values during the session and header() function to redirect user from the second script back to the first.
Supposing both files are in the root directory, ajoutDevis.php:
<?php
session_start();
?>
<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
<label for="client">Client</label>
<input type='text' id='client' name='client'>
<?php if (isset($_SESSION['clientErr'])) : ?>
<span class="error">* <?php echo $_SESSION['clientErr'] ?></span>
<?php endif; ?>
</FORM>
and ajoutDevisScript.php:
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST['client'])) {
$_SESSION['clientErr'] = "ERROR";
header('Location: ajoutDevis.php');
} else {
$client = $_POST['client'];
}
}
?>
I would suggest you to print the span only if you need to. I would also suggest you to check your brackets to avoid PHP errors.