update form not populating from existing data in mysql database - php

Hi im trying to populate my Update form with existing data from mysql database so I can modify the data and save it back to the mysql database. Everything is working and no errors but its not populating at all. Thanks in advance
Index.php
<?php
require('includes/mysqli_connect.php');
$query = "SELECT * FROM item";
$result = mysqli_query($dbc, $query);
WHILE($items = mysqli_fetch_array($result)) {
echo "<h3>" . $items['item_id'] . "</h3>";
echo "<p>" . $items['title'] . "</p>";
echo "Modify Item";
echo "<span> </span>";
echo "<span> </span>";
echo "Delete Item";
}
?>
Modify.php
<?php
require('includes/mysqli_connect.php');
if(!isset($_POST['submit'])) {
include('includes/mysqli_connect.php');
$q = "SELECT * FROM item WHERE item_id = '$_GET[item_id]'";
$result = mysqli_query($dbc, $q);
$items = mysqli_fetch_array($result)
/* or die("Error: ".mysqli_error($dbc)) */;
}
?>
<h1>Modify item</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<h4>Selling format:</h4>
<h4>Auction:
<input type="checkbox" value="<?php echo $items['auction']; ?>" name="inputauction"></h4>
<h4>Buy Now:
<input type="checkbox" value="<?php echo $items['buynow']; ?>" name="inputbuynow"></h4>
<h4>Select category:</h4>
<select name="cat_id">
<?php
include('includes/mysqli_connect.php');
$q = "SELECT cat_id, cat_name FROM category ".
"ORDER BY cat_id";
$r = mysqli_query($dbc,$q);
while($row = mysqli_fetch_array($r))
{
echo "<option value=\"".$row['cat_id']."\">".$row['cat_name']."</option>\n ";
}
?>
</select>
<h4>Title:
<input type="text" size="30" maxlength="30"
value="<?php echo $items['title']; ?>" name="inputtitle"></h4>
<h4>Description:</h4>
<textarea rows="7" cols="27" value="<?php echo
$items['description']; ?>" name="inputdescription" wrap="physical"></textarea>
<p><a href="#" onclick="window.open('uploadwin.php', 'newwindow',
'width=200, height=150'); return false;">
<h4>Click here to upload picture</h4></a></p>
<h4>Auction Start Price:
<input type="text" size="15" maxlength="15"
value="<?php echo $items['auct_price']; ?>" name="inputauct_price"></h4>
<h4>Buynow Price:
<input type="text" size="15" maxlength="15"
value="<?php echo $items['buynow_price']; ?>" name="inputbuynow_price"></h4>
<h4>Duration:
<select name="inputduration">
<option value="<?php echo $items['1 Day']; ?>">1 Day</option>
<option value="<?php echo $items['3 Days']; ?>">3 Days</option>
<option value="<?php echo $items['5 Days']; ?>">5 Days</option>
</select></h4>
<h4>Quantity:
<input type="text" size="15" maxlength="15"
value="<?php echo $items['qty']; ?>" name="inputqty"></h4>
<h4>Item Location:
<input type="text" size="15" maxlength="15"
value="<?php echo $items['item_location']; ?>" name="inputitem_location"></h4>
<h4>Accepted Payment:</h4>
<h4>PayPal:
<input type="checkbox" value="<?php echo $items['paypal']; ?>" name="inputpaypal"></h4>
<h4>EFT:
<input type="checkbox" value="<?php echo $items['EFT']; ?>" name="inputEFT"></h4>
<h4>Postage:</h4>
<h4>Pickup:
<input type="checkbox" value="<?php echo $items['pickup']; ?>" name="inputpickup"></h4>
<h4>Post Interstate:
<input type="checkbox"
value="<?php echo $items['post_nationally']; ?>" name="inputpost_nationally"></h4>
<h4>Post National:
<input type="checkbox" value="
<?php echo $items['post_internation']; ?>" name="inputpost_internation"></h4>
<h4>Post Internation:
<input type="checkbox"
value="<?php echo $items['buyerpay_post']; ?>" name="inputbuyerpay_post"></h4>
<input type="hidden" name="item_id" value="<?php echo $_GET['item_id']; ?>" />
<input type="submit" value="Modify" name="submit">
</form><br><br>
<?php
if(isset($_POST['submit'])) {
$u = "UPDATE `ICADBS504A`.`item` SET `auct_price` = '$_POST[inputauct_price]',
`buynow_price` ='$_POST[inputbuynow_price]', `title` = '$_POST[inputtitle]',
`description` = '$_POST[inputdescription]', `qty` = '$_POST[inputqty]',
`img_path` = '$imgpath', `item_location` = '$_POST[inputitem_location]',
`auction` = '$_POST[inputauction]', `buynow` = '$_POST[inputbuynow]',
`duration` = '$_POST[inputduration]', `pickup` = '$_POST[inputpickup]',
`post_nationally` = '$_POST[inputpost_nationally]',
`post_internation` = '$_POST[inputpost_internation]',
`buyerpay_post` = '$_POST[inputbuyerpay_post]', `cat_id` = '$cat',
`paypal` = '$_POST[inputpaypal]', `EFT` = '$_POST[inputEFT]' WHERE
`item_id` = $_POST[item_id]";
mysqli_query($u) or die(mysqli_error());
echo "Item has been modified!";
header("Location: index.php");
}
include('includes/footer.html');
?>

Related

How do I display the record before deleting that record?

I wish to display the record to the user that I am deleting in PHP?
I am trying to display the record in a form that I want to delete but only the input type = text values are being displayed and the radio button values are not being displayed?
<?php
require('/home/s3022041/sqlC/dbConnect.php');
if(isset($_POST['search']))
{
$search = mysqli_real_escape_string($connection, $_POST['id']);
$id = $_POST['id'];
$stp1 = preg_replace("/[^a-zA-Z0-9]/", "", $id); //grab only the alphanumerics
$stp2 = strtoupper($stp1); //Make all alphabets uppercase
$stp3 = preg_replace('/\d+/', '',$stp2); //extract the alphabets part
$newsearchid = str_replace($stp3,"-".$stp3."-",$stp2); //put hyphens before and after the alphabet part
$query = "SELECT * FROM cars WHERE Registration_Number = '$newsearchid' ";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
{
?>
<form action="delete_process.php" method="POST" class="form">
<h2>Car Registration Form</h2>
<div class="mb-3">
<input type="radio" name="Car" id="Toyota" value=" <?php echo $row['make']; ?>" />
<label for="Toyota" class="form-label">Toyota</label>
<select name="Models[Toyota]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="Camri">Camri</option>
<option value="Corolla">Corolla</option>
<option value="Estima">Estima</option>
</select><br>
<input type="radio" name="Car" id="Nissan" value=" <?php echo $row['make']; ?>"/>
<label for="Nissan" class="form-label">Nissan</label>
<select name="Models[Nissan]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="Micra">Micra</option>
<option value="roller">roller</option>
<option value="fushe">fushe</option>
</select><br>
<input type="radio" name="Car" id="Mercedez" value=" <?php echo $row['make']; ?>"/>
<label for="Mercedez" class="form-label">Mercedez</label>
<select name="Models[Mercedez]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="5series">5series</option>
<option value="x7">x7</option>
<option value="x5">x5</option>
</select><br>
<label for="vehicleidentificationnumber" class="form-label">vehicle identification
number(VIN)</label>
<input type="text" disabled="disabled" name="vehicleidentificationnumber" class="form-control" maxlength=20 value=" <?php echo $row['VIN']; ?>" required >
<br>
<label for="ManufacturingYear" class="form-label">Manufacturing Year</label>
<input type="text" name="Manufacture_Year" class="form-control" maxlength=20 value=" <?php echo $row['Manufacture_Year']; ?>" required >
<label for="Enginesize" class="form-label">Engine size</label>
<input type="text" name="Engine_Size" class="form-control" maxlength=20 value=" <?php echo $row['Engine_Size']; ?>" required>
<label for="TransmissionType" class="form-label">TransmissionType</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="Automatic" class="form-label">Automatic</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="Manual" class="form-label">Manual</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="SemiAutomatic" class="form-label">Semi-Automatic</label><br>
<label for="NoofSeats" class="form-label">No. of Seats</label>
<input type="number" name="NoOfSeats" class="form-control" value="<?php echo $row['NoOfSeats']; ?>" maxlength=20 required>
<label for="Noofdoors" class="form-label">No. of doors</label>
<input type="number" name="NoOfDoors" class="form-control" maxlength=20 value="<?php echo $row['NoOfDoors']; ?>" required>
<label for="Fueltype" class="form-label">Fuel type</label>
<input type="text" name="Fuel_Type" class="form-control" maxlength=20 value=" <?php echo $row['Fuel_Type']; ?>" required>
<label for="Colour" class="form-label">Colour</label>
<input type="text" name="Colour" class="form-control" maxlength=20 value=" <?php echo $row['Colour']; ?>" required>
<label for="RegistrationNumber" class="form-label">Registration Number (use Dublin
registration) </label>
<input type="hidden" disabled="disabled" name="Registration_Number" class="form-control" maxlength=20 value=" <?php echo $row['Registration_Number']; ?>" required>
<label for="Dateoffirstregistration" class="form-label">Date of first registration</label>
<input type="date" name="DateOfRegestration" class="form-control" maxlength=20 value=" <?php echo $row['DateOfRegestration']; ?>" required>
</div>
<button type="submit" name="search" class="btn btn-primary">Delete</button>
</form>
<?php
}
}
else{
echo "<h1> No records found </h1>";
echo "<a href='index.php'>home</a>";
}
?>
</div>
<div class="modal-footer">
</form>
Here I am trying to delete the above record that is displayed but it says deleted successfully but when I go and check the records it is not deleted it's still there?
<?php
require('/home/s3022041/sqlC/dbConnect.php');
if(isset($_POST['search']))
{
$search = mysqli_real_escape_string($connection, $_POST['search']);
$id = $_POST['Registration_Number'];
$query = "DELETE FROM `cars` WHERE Registration_Number='$id' ";
$query_run = mysqli_query($connection, $query) or die ("not done");
if($query_run)
{
echo "<h1> deleted successfully</h1>";
echo "<a href='index.php'>home</a>";
}
else
{
echo "<h1> not deleted </h1>";
echo "<a href='index.php'>home</a>";
echo 'Error! ' . mysqli_error($connection);
}
}
Change
1. Form
<input type="radio" name="Car" id="Toyota" value="<?=$row['make']; ?>" />
to
<input type="radio" name="Car" id="Toyota" <?=($row['make'] == 'Toyota' ? 'value="'.$row['make'].'" checked':NULL); ?> />
<!-- or better yet -->
<input type="radio" name="Car" id="Toyota" value="Toyota" <?=($row['make'] == 'Toyota' ? 'checked':NULL); ?> />
2. Database
File: delete_process.php
// Change
if($query_run) // << This only tell the script the sql statement run with no errors
// To
if(mysql_affected_rows()<=1) // << This confirms that one or more rows where changed by the sql statement
Explained
PHP Shorthand If/Else Using Ternary Operators (?:)
/* basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
Reason for error
If a radio button are not being displayed, it most-likely an 'Undefined variable' when getting $row['make'].
Other thing, remove the white spacing from. As this may play a part when using a MYSQL Statement.
<!-- From -->
value=" <?php echo $row['Registration_Number']; ?>"
<!-- To -->
value="<?=$row['Registration_Number']; ?>"
*Shorthand one line echo/print <?=

Update query for database in PHP not working

I am having trouble updating my database data. I put the data that I want to update but when I click on the "Update" button it does
nothing. I have called the file on another php file by using Update
It also shows this error
Notice: Undefined variable: fname in
C:\xampp\htdocs\project\change1.php on line 71
Can someone help me figure this issue, please?
<?php include("config.php"); ?>
<?php
if (isset($_GET['edit'])) {
$update = true;
$record = mysqli_query($con, "SELECT * FROM employee WHERE id='".$_GET['edit']."'");
$row = mysqli_fetch_array($record,MYSQLI_BOTH);
}
if (isset($_POST['update'])) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$password = $_POST['password'];
$addr = $_POST['addr'];
$phone = $_POST['phone'];
$id=$_GET['edit'];
$query = "UPDATE employee SET fname='".$fname."',lname='".$lname."',password='".$password."',addr='".$addr."',phone='".$phone."' WHERE id='".$id."'";
$result = mysqli_query($con,$query) or die ("problem inserting new record into database");
if($result){
header('location: show_db.php');
}
else {echo "Update not successful"; }
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Update Data</title>
</head>
<body>
Home
<br/><br/>
<input type="hidden" name="id" value="<?php echo $id; ?>">
Name:<input type="text" name="fname" value="<?php echo $fname ; ?>">
Surname:<input type="text" name="lname" value="<?php echo $lname; ?>">
Password:<input type="text" name="password" value="<?php echo $password; ?>">
Address:<input type="text" name="addr" value="<?php echo $addr; ?>">
Contact:<input type="text" name="phone" value="<?php echo $phone; ?>">
<input type="submit" name="update" value="Update">
</body>
</html>
Put the html inputs inside a form
<form name ="form1" method ="get" action="">
<input type="hidden" name="id" value="<?php echo $id; ?>">
Name:<input type="text" name="fname" value="<?php echo $fname ; ?>">
Surname:<input type="text" name="lname" value="<?php echo $lname; ?>">
Password:<input type="text" name="password" value="<?php echo $password; ?>">
Address:<input type="text" name="addr" value="<?php echo $addr; ?>">
Contact:<input type="text" name="phone" value="<?php echo $phone; ?>">
<input type="submit" name="update" value="Update">
</form>

How to submit and edit data from database of paritcular id on same page

Good Evening, Everybody..
i am using php form to submit data of students in database using the below code...
<?php
if(isset($_POST['submit1']))
{
$a1=mysql_real_escape_string($_POST['a1']);
$a2=mysql_real_escape_string($_POST['a2']);
$a3=mysql_real_escape_string($_POST['a3']);
$a=mysql_real_escape_string($_POST['a']);
$a4=mysql_real_escape_string($_POST['a4']);
$a5=mysql_real_escape_string($_POST['a5']);
$a6=mysql_real_escape_string($_POST['a6']);
$color=mysql_real_escape_string($_POST['color']);
$date=mysql_real_escape_string($_POST['date']);
$query1=mysql_query("insert into student_table values('','$a1','$a2','$a3','$a','$a4','$a5','$a6','$color','$date')");
if($query1)
{
echo "Open Updated Successfully";
}
}
?>
<div class="RECORD" align="center"><font color="red" size="4">! STUDENT RESULTS !</font><div class="HEADLINES">! Live Result !<br>
<font color="yellow"><?php
$query1 = "SELECT CONCAT(a1,a2,a3,' - ',a,' - ',a4,a5,a6) FROM student_table ORDER BY id DESC
LIMIT 1;";
$result = mysql_query($query1);
$data = mysql_fetch_array($result);
echo $data[0];
?></font></div><hr>
<form method="post" action="" autocomplete="off">
DATE:<input type="text" name="date" size="6" align="absmiddle" value="<?php
$current_dayname = date("l");
echo $date = date("d/m/y",strtotime('monday this week')).' <br>to<br> '.date("d/m/y",strtotime("sunday this week"));
?>">
<br><br>Open:
<input type="text" name="a1" size="1" value="<?php echo $query2['a1']; ?>">
<input type="text" name="a2" size="1" value="<?php echo $query2['a2']; ?>">
<input type="text" name="a3" size="1" value="<?php echo $query2['a3']; ?>"> -
<input type="text" name="a" size="2" value="<?php echo $query2['a']; ?>">
<input type="submit" name="submit1">
</form>
<?php
$query1 = "SELECT id FROM student_table ORDER BY id DESC
LIMIT 1;";
$result = mysql_query($query1);
$data = mysql_fetch_array($result);
?>
Now By using the above code it shows desired result....
but i want to add one more result along with previous result, due to this i have to required edit page of above result, the desired code of edit page is below...
<?php
if(isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['submit2']))
{
$a1=$_POST['a1'];
$a2=$_POST['a2'];
$a3=$_POST['a3'];
$a=$_POST['a'];
$a4=$_POST['a4'];
$a5=$_POST['a5'];
$a6=$_POST['a6'];
$color=$_POST['color'];
$date=$_POST['date'];
$query3=mysql_query("update student_table set a1='$a1', a2='$a2', a3='$a3', a='$a', a4='$a4', a5='$a5', a6='$a6', color='$color', date='$date' where id='$id'");
if($query3)
{
echo "</br> Close Updated Successfully";
}
}
$query1=mysql_query("select * from student_table where id='$id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="" autocomplete="off">
<input type="text" name="a4" size="1" value="<?php echo $query2['a4']; ?>">
<input type="text" name="a5" size="1" value="<?php echo $query2['a5']; ?>">
<input type="text" name="a6" size="1" value="<?php echo $query2['a6']; ?>">
<input type="text" name="a" size="2" value="<?php echo $query2['a']; ?>"><select type="text" name="color" value="<?php echo $query2['color']; ?>"> <option value="#000000">Black</option><option value="#FF0000">Red</option></select>
<input type="submit" name="submit2"><br>
<div style="background:black; color:black; font-size:1px">DATE:<input type="text" name="date" size="5" align="absmiddle" value="<?php
$current_dayname = date("l");
echo $date = date("d/m/y",strtotime('monday this week')).' <br>to<br> '.date("d/m/y",strtotime("sunday this week"));
?>"><input type="text" name="a1" size="1" value="<?php echo $query2['a1']; ?>">
<input type="text" name="a2" size="1" value="<?php echo $query2['a2']; ?>">
<input type="text" name="a3" size="1" value="<?php echo $query2['a3']; ?>"></div>
</form>
<?php
}
?>
Now my question is that....is it possible to perform both submitting of students numbers and having an option to add one more same result with editable function of previous result entry on the same page...
please refer below image...
can anybody help me in order to perform both functioning on same page with editable option

Editing mysql data with drop downs

I have a data entry page where a few drop downs exist. The selected item in the drop down stores in my mysql database with no issue.
I've made a 2nd page to edit individual records. I can display the data from the drop down in a text box with no problem. However, I want to be able to edit the result with the same drop down choices.
I can add the drop down menu on my edit.php page with all the correct options, but the stored value from the database does not appear. Instead I get the first choice by default and not the stored value.
<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";
while ($row = mysql_fetch_array($position_result)) {
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>
I'm using POST and GET to get the correct record ID.
My text boxes, which work fine are as follows:
Department:<input type="text" name="department" size="20" value="<?php echo "$row[department]"; ?>">
I'm assuming I have to build some sort of if statement to display the stored value?
not sure if this helps, but this is my how I grab the ID for the record I want to edit:
<?php
$id= ($_GET["id"]);
$sql = "SELECT * FROM people
WHERE id='$id' LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>
Entire Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<?php
$BASE_PATH = 'C:\xampp\htdocs\OGS';
include_once($BASE_PATH . "\includes\layouts\header.php");
?>
<div id="main">
<div id="subnavigation">
<?php include_once($BASE_PATH . "\mods\main_menu\index.html");?>
</div>
<div id="page"
<br><br>
<table border=1>
<tr>
<td align=center>Update Employee Information</td>
</tr>
<tr>
<td>
<table>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('ogs');
?>
<?php
$id= ($_GET["id"]);
$sql = "SELECT * FROM people
WHERE id='$id' LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo "$row[id]"; ?>">
<fieldset>
<legend><b>Name</b></legend>
First Name:<input type="text" name="first_name" size="20" value="<?php echo "$row[first_name]"; ?>">
Last Name:<input type="text" name="last_name" size="40" value="<?php echo "$row[last_name]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Contact Information</b></legend>
Town:<input type="text" name="town" size="20" value="<?php echo "$row[town]"; ?>">
Address:<input type="text" name="address" size="40" value="<?php echo "$row[address]"; ?>">
Province:<input type="text" name="province" size="20" value="<?php echo "$row[province]"; ?>">
Postal Code:<input type="text" name="postal_code" size="40" value="<?php echo "$row[postal_code]"; ?>">
<br><br>
Home Phone:<input type="text" name="home_phone" size="20" value="<?php echo "$row[home_phone]"; ?>">
Cell Phone:<input type="text" name="cell_phone" size="40" value="<?php echo "$row[cell_phone]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Emergency Contact</b></legend>
Emergency Contact Name:<input type="text" name="first_name" size="20" value="<?php echo "$row[first_name]"; ?>">
Emergency Contact Number:<input type="text" name="last_name" size="40" value="<?php echo "$row[last_name]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Work Information</b></legend>
Role:<input type="text" name="role" size="20" value="<?php echo "$row[role]"; ?>">
Employer:<input type="text" name="company_works_for" size="40" value="<?php echo "$row[company_works_for]"; ?>">
<br><br>
Department:<input type="text" name="department" size="20" value="<?php echo "$row[department]"; ?>">
Position:
<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";
// You should use PHP to get the existing value here, I have made it up here as 14
$existing_id = '$row[id]';
while ($row = mysql_fetch_array($position_result))
{
// Check if the existing id is the same as the current id we are displaying
// If it is, set the selected attribute
if($existing_id == $row['id'])
echo "<option selected='selected' value='" . $row['id'] . "'>" . $row['position'] . "</option>";
else
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>
<br><br>
Is Supervisor?:
<input type="radio" name="is_supervisor" value="<?php echo "$row[is_supervisor]"; ?>"> Yes
<input type="radio" name="is_supervisor" value="<?php echo "$row[is_supervisor]"; ?>"> No
<br><br>
Is Active?:
<input type="radio" name="active_employee" value="<?php echo "$row[active_employee]"; ?>"> Yes
<input type="radio" name="active_employee" value="<?php echo "$row[active_employee]"; ?>"> No
<br><br>
Start Date:<input type="text" name="start_date" size="40" value="<?php echo "$row[start_date]"; ?>">
</fieldset>
<input type="submit"
name="submit value" value="Update">
</form>
</div>
</div>
</body>
</html>
The record for Nicholas Furlong shows him as a Health & Safety Coordinator.
http://prntscr.com/3o34g2
But when I click edit, and go to my edit page, it has him listed as controller. (This is the first option in the column.)
http://prntscr.com/3o36qu
Its not entirely clear what you are asking. But I will try
<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";
// You should use PHP to get the existing value here, I have made it up here as 14
$existing_id = 14;
while ($row = mysql_fetch_array($position_result))
{
// Check if the existing id is the same as the current id we are displaying
// If it is, set the selected attribute
if($existing_id == $row['id'])
echo "<option selected='selected' value='" . $row['id'] . "'>" . $row['position'] . "</option>";
else
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>

php Edit Profile when user is not providing password how to check if its empty

I have the form that is for users to edit their profiles:
<div id="page">
<?php echo message(); ?>
<?php echo form_errors($errors); ?>
<h2>Edit Admin: <?php echo htmlentities($admin["username"]); ?></h2>
<form action="edit_admin.php?usr_serno=<?php echo urlencode($admin["usr_serno"]); ?>" method="post">
<p>Username:
<input type="text" name="username" value="<?php echo htmlentities($admin["username"]); ?>" />
</p>
<p>Password:
<input type="password" name="password" value="" />
<?php if (!isset($_GET["password"])
</p>
<p>Email:
<input type="text" name="email" value="<?php echo htmlentities($admin["email"]); ?>" />
</p>
<p>Role:
<select type="int" name="role_serno">
<option value="1">Administrator</option>
<option value="2">User</option>
</select>
</p>
<p>First Name:
<input type="text" name="name" value="<?php echo htmlentities($admin["name"]); ?>" />
</p>
<p>Last Name:
<input type="text" name="lastname" value="<?php echo htmlentities($admin["lastname"]); ?>" />
</p>
<p>ID:
<input type="text" name="id" value="<?php echo htmlentities($admin["id"]); ?>" />
</p>
<p>Address:
<input type="text" name="address" value="<?php echo htmlentities($admin["address"]); ?>" />
</p>
<p>Postcode:
<input type="text" name="postcode" value="<?php echo htmlentities($admin["postcode"]); ?>" />
</p>
<p>City:
<select id="city" type="text" name="city" value="<?php echo htmlentities($admin["city"]); ?>">
<option value="Nicosia">Nicosia</option>
<option value="Limassol">Limassol</option>
<option value="Larnaca">Larnaca</option>
<option value="Paphos">Paphos</option>
<option value="Other">Other</option>
</select>
<input id="other_city" type="text" name="other_city" value="<?php echo htmlentities($admin["city"]); ?>" />
</p>
<p>Telephone:
<input type="text" name="telephone" value="<?php echo htmlentities($admin["telephone"]); ?>" />
</p>
<p>College:
<input type="text" name="college" value="<?php echo htmlentities($admin["college"]); ?>" />
</p>
<input type="submit" name="submit" value="Edit Admin" />
</form>
<br />
Cancel
</div>
`
And on top of that page i am checking and trying to update the users profile:
if (isset($_POST["submit"])) {
$required_fields = array("username","email","role_serno",
"name","lastname","id","telephone","address","college","postcode","city");
validate_presences($required_fields);
$fields_with_max_lengths = array("username" => 30);
validate_max_lengths($fields_with_max_lengths);
if (empty($errors)) {
// Perform Update
//if (!isset ($_POST["password"]))
//$hashed_password = $_GET["password"];
//if (isset ($_POST["password"]))
$id_serno = $admin["usr_serno"];
$username = mysql_prep($_POST["username"]);
$hashed_password = password_encrypt($_POST["password"]);
$email=mysql_prep($_POST["email"]);
$role_serno=mysql_prep($_POST["role_serno"]);
$name=mysql_prep($_POST["name"]);
$lastname=mysql_prep($_POST["lastname"]);
$id=mysql_prep( $_POST["id"]);
$date_create=mysql_prep( $_POST["date_create"]);
$address=mysql_prep( $_POST["address"]);
$postcode=mysql_prep( $_POST["postcode"]);
$city=mysql_prep( $_POST["city"]);
$telephone=mysql_prep( $_POST["telephone"]);
$college=mysql_prep( $_POST["college"]);
$query = "UPDATE users SET ";
$query .= "username = '{$username}', ";
$query .= "password = '{$hashed_password}', ";
$query .= "email = '{$email}', ";
$query .= "role_serno = '{$role_serno}', ";
$query .= "name = '{$name}', ";
$query .= "lastname = '{$lastname}', ";
$query .= "id = '{$id}', ";
$query .= "address = '{$address}', ";
$query .= "postcode = '{$postcode}', ";
$query .= "city = '{$city}', ";
$query .= "telephone = '{$telephone}', ";
$query .= "college = '{$college}' ";
$query .= "WHERE usr_serno = {$id_serno} ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) == 1) {
// Success
$_SESSION["message"] = "Admin updated.";
redirect_to("manage_admins.php");
} else {
// Failure
$_SESSION["message"] = "Admin update failed.";
}
}
} else {
// This is probably a GET request
} // end: if (is set($_POST["submit"]))`
the issue comes when the user is posting a form without passing the password in cause then the database is updated with empty password and i have a check not to accept empty passwords at lo-gin page.
Is there a way to avoid and check this.
Please advise cause I am new with developing in php and I am not 100% sure how this is going to be done.
I tried so far to check if post value is empty then pass the current database value without encoding it.. but for some reason the if check doesn't actually do anything
you can check like this
if (isset($_POST["submit"]) && trim($_POST['password']) !="") {
//run the code
}else{
}
You can add a client side javascript validation here.
In case of server side
check
if(empty($_POST['password']))
{
// whatever you want to ask and do
}

Categories