I am trying to take a user input and update that specific item in my database. For some reason I cannot seem to get the item to get updated.
<?php
include('../inclass/db_connect.php');
$id = $_GET["id"];
$sql = "SELECT id, teamName, coach, yearFormed, numStanleyCups, numPlayers, teamValue, totWins FROM hockeyTeams WHERE id=$id";
$result = $pdo->prepare($sql);
$result->execute();
$row = $result->fetch();
?>
<h1 align="center">Edit Team - <?php echo $row['teamName'] ?></h1>
<form method="post">
<table align="center" border="1">
<tr>
<td>Team Name:<input type="text" name="teamName" value="<?php echo $row['teamName'] ?>" required /></td>
</tr>
<tr>
<td>Coach Name:<input type="text" name="coach" placeholder="Coach Name" value="<?php echo $row['coach'] ?>" required /></td>
</tr>
<tr>
<td>Year Team Formed:<input type="date" name="yearFormed" value="<?php echo $row['yearFormed'] ?>" required /></td>
</tr>
<tr>
<td>Number of Stanley Cups Won:<select name="numStanleyCups"><option value="" selected><?php echo $row['numStanleyCups'] ?></option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option></select></td>
</tr>
<tr>
<td>Number of Players:<input type="text" name="numPlayers" value="<?php echo $row['numPlayers'] ?>"/></td>
</tr>
<tr>
<td>Team Value: <input type="text" name="teamValue" value="<?php echo $row['teamValue'] ?>"/></td>
</tr>
<tr>
<td>Total Team Wins: <input type="text" name="totWins" value="<?php echo $row['totWins'] ?>" /></td>
</tr>
<tr>
<td>
<button type="submit" name="btn-edit"><strong>Edit Team</strong></button></td> </tr>
</table>
</form>
<?php
if(isset($_POST['btn-edit']))
{
$sql = "UPDATE hockeyTeams SET teamName = '".$_POST['teamName']."', coach = '".$_POST['coach']."', yearFormed = '".$_POST['yearFormed']."', numStanleyCups = '".$_POST['numStanleyCups']."', numPlayers = '".$_POST['numPlayers']."', teamValue = '".$_POST['teamValue']."', totWins = '".$_POST['totWins']."' WHERE id = ".$_GET['id'];
$result = $pdo->prepare($sql);
$result->execute();
}
When I took out the top portion of the PHP the update function oddly enough worked once and than broke. I cant seem to put together a reasoning of why the update wouldnt be working other than having something to do with me pulling in the data at the top of the file.
I couldnt seem to find any posts that were finding a good answer to what was going wrong, I checked to make sure all my '" are correct and working so I am stuck on why it wont update!
Any help is greatly appreciated. Thanks!
The problem is that you're displaying the form before you perform the update. So you're showing the old values from the database. Move the code that performs the update to the top.
And since you're using PDO, you should use $pdo->bindParam() for your parameters, instead of concatenating strings into the SQL.
<?php
include('../inclass/db_connect.php');
$id = $_GET["id"];
if(isset($_POST['btn-edit']))
{
$sql = "UPDATE hockeyTeams SET teamName = '".$_POST['teamName']."', coach = '".$_POST['coach']."', yearFormed = '".$_POST['yearFormed']."', numStanleyCups = '".$_POST['numStanleyCups']."', numPlayers = '".$_POST['numPlayers']."', teamValue = '".$_POST['teamValue']."', totWins = '".$_POST['totWins']."' WHERE id = ".$_GET['id'];
$result = $pdo->prepare($sql);
$result->execute();
}
$sql = "SELECT id, teamName, coach, yearFormed, numStanleyCups, numPlayers, teamValue, totWins FROM hockeyTeams WHERE id=$id";
$result = $pdo->prepare($sql);
$result->execute();
$row = $result->fetch();
?>
<h1 align="center">Edit Team - <?php echo $row['teamName'] ?></h1>
<form method="post">
<table align="center" border="1">
<tr>
<td>Team Name:<input type="text" name="teamName" value="<?php echo $row['teamName'] ?>" required /></td>
</tr>
<tr>
<td>Coach Name:<input type="text" name="coach" placeholder="Coach Name" value="<?php echo $row['coach'] ?>" required /></td>
</tr>
<tr>
<td>Year Team Formed:<input type="date" name="yearFormed" value="<?php echo $row['yearFormed'] ?>" required /></td>
</tr>
<tr>
<td>Number of Stanley Cups Won:<select name="numStanleyCups"><option value="" selected><?php echo $row['numStanleyCups'] ?></option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option></select></td>
</tr>
<tr>
<td>Number of Players:<input type="text" name="numPlayers" value="<?php echo $row['numPlayers'] ?>"/></td>
</tr>
<tr>
<td>Team Value: <input type="text" name="teamValue" value="<?php echo $row['teamValue'] ?>"/></td>
</tr>
<tr>
<td>Total Team Wins: <input type="text" name="totWins" value="<?php echo $row['totWins'] ?>" /></td>
</tr>
<tr>
<td>
<button type="submit" name="btn-edit"><strong>Edit Team</strong></button></td> </tr>
</table>
</form>
Related
I'm trying to lay out my MySQL data in a table on the page. Instead of directing the user towards other forms to perform the Add, Update and Delete queries, I instead opted to have the queries within the table in the form of buttons, Saving time and effort as rows can be added, updated or deleted right then and there. However now with my page, form and tables set up I tried establishing the queries that the buttons would have set to them. And after testing it out, nothing works, when I click any of the buttons, the fields just return to whatever they originally were before I changed them.
Admin_Album_Page.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
require_once 'ConnectorCode.php';
?>
<!DOCTYPE HTML>
<html>
<head>
<title> Albums </title>
</head>
<body>
<?php
if(isset($_POST['update'])) {
$UpdateQuery = "UPDATE tbl_Albums SET Album_Name='{$_POST['albumname']}',
Number_Of_Tracks='{$_POST['numberoftracks']}', Genre='{$_POST['genre']}',
Artist_id='{$_POST['artistid']}' WHERE Album_id='{$_POST['hidden']}'";
mysqli_query ($conn, $UpdateQuery);
}
if(isset($_POST['delete'])) {
$DeleteQuery = "DELETE FROM tbl_Albums WHERE Album_id='$_POST[hidden]'";
mysqli_query ($conn, $DeleteQuery);
}
if(isset($_POST['add'])) {
$AddQuery = "INSERT INTO tbl_Album (Album_Name, Number_Of_Tracks, Genre, Artist_id)
VALUES ('$_POST[uartistname]', '$_POST[unumberoftracks]',
'$_POST[ugenre]',
'$_POST[uartist]')";
mysqli_query ($conn, $AddQuery);
}
?>
<?php
$result = mysqli_query($conn, "SELECT*FROM tbl_Albums");
?> <table border="1">
<tr>
<th>Album ID</th>
<th>Album Name</th>
<th>Number of Tracks</th>
<th>Genre</th>
<th>Artist ID</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<form method = "POST">
<tr>
<td><input type="number" name="albumid" value="<?php echo $row ['Album_id']; ?>" /></td>
<td><input type="text" name="albumname" value="<?php echo $row['Album_Name']; ?>" /></td>
<td><input type="number" name="numberoftracks" value="<?php echo $row['Number_Of_Tracks']; ?>" /></td>
<td><input type="text" name="genre" value="<?php echo $row['Genre']; ?>"/></td>
<td><input type="number" name="artistid" value="<?php echo $row['Artist_id']; ?>" /></td>
<td><input type="hidden" name="hidden" value="<?php echo $row['Album_id']; ?>"/></td>
<td><input type="submit" name="update" value="Update" /></td>
<td><input type="submit" name="delete" value="Delete"/></td>
</tr>
</form>
<?php
}
?>
<form method="POST">
<tr>
<td></td>
<td><input type="text" name="ualbumname" /></td>
<td><input type="text" name="unumberoftracks" /></td>
<td><input type="text" name="ugenre" /></td>
<td><input type="text" name="uartistid" /></td>
<td><input type="submit" name="add" value="Add"/></td>
</tr>
</form>
</table>
<?php
mysqli_close($conn);
?>
<p>Return to main page</p>
Where am I going wrong? I'm trying my utmost best to call on my declared rows and use them within the query but all I get is a refreshed page with no change to the data from the rows
If all your query is perfect then this will run perfectly.
Update Block
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE tbl_Albums SET Album_Name='{$_POST['albumname']}',
Number_Of_Tracks='{$_POST['numberoftracks']}', Genre='{$_POST['genre']}',
Artist_id='{$_POST['artistid']}' WHERE Album_id='{$_POST['hidden']}'";
mysqli_query($conn, $UpdateQuery);
}
Delete Block
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM tbl_Albums WHERE Album_id='$_POST[hidden]'";
mysqli_query($conn, $DeleteQuery);
}
Add Block
if(isset($_POST['add'])){
$uartistname = $_POST['uartistname'];
$unumberoftracks = $_POST['unumberoftracks'];
$ugenre = $_POST['ugenre'];
$uartist = $_POST['uartist'];
$AddQuery = "INSERT INTO tbl_Album (Album_Name, Number_Of_Tracks, Genre, Artist_id) VALUES('$uartistname', $unumberoftracks, '$ugenre', $uartist)";
mysqli_query($conn, $AddQuery);
}
Dynamic HTML Block
$result = mysqli_query($conn, "SELECT*FROM tbl_Albums");?>
<table border="1">
<tr>
<th>Album ID</th>
<th>Album Name</th>
<th>Number of Tracks</th>
<th>Genre</th>
<th>Artist ID</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<form method="POST" action="">
<td><input type="number" name="albumid" value="<?php echo $row['Album_id']; ?>" /></td>
<td><input type="text" name="albumname" value="<?php echo $row['Album_Name']; ?>" /></td>
<td><input type="number" name="numberoftracks" value="<?php echo $row['Number_Of_Tracks']; ?>" /></td>
<td><input type="text" name="genre" value="<?php echo $row['Genre']; ?>"/></td>
<td><input type="number" name="artistid" value="<?php echo $row['Artist_id']; ?>" /></td>
<td><input type="hidden" name="hidden" value="<?php echo $row['Album_id']; ?>"/></td>
<td><input type="submit" name="update" value="Update" /></td>
<td><input type="submit" name="delete" value="Delete"/></td>
</form>
</tr>
<?php }?>
<tr>
<form method="POST" action="">
<td></td>
<td><input type="text" name="ualbumname" /></td>
<td><input type="text" name="unumberoftracks" /></td>
<td><input type="text" name="ugenre" /></td>
<td><input type="text" name="uartistid" /></td>
<td><input type="submit" name="add" value="Add"/></td>
</form>
</tr>
</table>
This is just the re-representation of the Code. Let me know if anything wrong with this.
Please look into this code unable to find out the actual error:
This is PHP upload code:
<?php
include_once("config.php");
if(isset($_GET['pro_id']))
{
$id=$_GET['pro_id'];
if(isset($_POST['submitBtn'])) {
$dept_id = $_POST['dept_id'];
$cat_id = $_POST['cat_id'];
/*$pro_id = $_POST['pro_id'];*/
$pro_name = $_POST['pro_name'];
$pro_desc = $_POST['pro_desc'];
$pro_spec = $_POST['pro_spec'];
$pro_price = $_POST['pro_price'];
$status = 'on';
$pro_keywords = $_POST['pro_keywords'];
//image names
$pro_image = $_FILES['pro_image']['name'];
//temp images names
$temp_image = $_FILES['pro_image']['tmp_name'];
if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR $pro_image=='' OR $pro_keywords=='')
{
echo "<script>alert('All the fields are mandatory')</script>";
exit();
}
else
{
//upload image to folder
move_uploaded_file($temp_image,"images/product_images/$pro_image");
$run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values ( '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'");
if($run_query1)
{
echo "<script>alert('Product updated successfully')</script>";
exit();
}
else
{
echo "<script>alert('Errors')</script>";
}
} }
$query1 = mysqli_query($login, "select * from products1 where pro_id='$id'");
$query2 = mysqli_fetch_array($query1);
?>
This the form Part where data retrieve from table and when click on the update button nothing happened and page is redirected to view data page and showing the old data:
<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="650" border="0">
<tr>
<td width="183" align="right">Department:</td>
<th width="231" align="left">
<select name="dept_id" id="dept_id">
<option>Select Department</option>
<?php
$result=dept_show();
while($row=mysqli_fetch_assoc($result))
{
echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="183" align="right">Catagory</td>
<th width="231" align="left">
<select name="cat_id" id="cat_id">
<option>Select Catagory</option>
<?php
$result1=cat_show();
while($row=mysqli_fetch_assoc($result1))
{
echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>";
}
?>
</select></th></tr>
<tr>
<!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>-->
</tr>
<tr>
<td align="right">Product Name/Model:</td>
<td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Description:</td>
<td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td>
</tr>
<tr>
<td align="right">Products Specification:</td>
<td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td>
</tr>
<tr>
<td align="right">Product Price:</td>
<td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Image:</td>
<td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td></td>
<td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td align="right">Keywords:</td>
<td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td>
</tr>
</table>
</form>
</div> <?php } ?>
</td>
</tr>
</table>
</div>
Form method is POST and you are using GET method in if loop
if(isset($_GET['pro_id']))
Use POST here.
I have made the changes in you complete code. Use this code and if required made the changes (if issues arrived)
PHP code
<?php
include_once("config.php");
if(isset($_POST['submitBtn']))
{
$dept_id = $_POST['dept_id'];
$cat_id = $_POST['cat_id'];
$pro_id = $_POST['pro_id'];*/
$pro_name = $_POST['pro_name'];
$pro_desc = $_POST['pro_desc'];
$pro_spec = $_POST['pro_spec'];
$pro_price = $_POST['pro_price'];
$status = 'on';
$pro_keywords = $_POST['pro_keywords'];
//image names
$pro_image = $_FILES['pro_image']['name'];
//temp images names
$temp_image = $_FILES['pro_image']['tmp_name'];
if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR $pro_image=='' OR $pro_keywords=='')
{
echo "<script>alert('All the fields are mandatory')</script>";
exit();
}
else
{
//upload image to folder
move_uploaded_file($temp_image,"images/product_images/$pro_image");
$run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values ( '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'");
if($run_query1)
{
echo "<script>alert('Product updated successfully')</script>";
exit();
}
else
{
echo "<script>alert('Errors')</script>";
}
}
}
$query2 = array();
if(isset($_GET['pro_id']))
{
$id=$_GET['pro_id'];
$query1 = mysqli_query($login, "select * from products1 where pro_id='$id'");
$query2 = mysqli_fetch_array($query1);
}
?>
HTML Code
<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="650" border="0">
<tr>
<td width="183" align="right">Department:</td>
<th width="231" align="left">
<select name="dept_id" id="dept_id">
<option>Select Department</option>
<?php
$result=dept_show();
while($row=mysqli_fetch_assoc($result))
{
echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="183" align="right">Catagory</td>
<th width="231" align="left">
<select name="cat_id" id="cat_id">
<option>Select Catagory</option>
<?php
$result1=cat_show();
while($row=mysqli_fetch_assoc($result1))
{
echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>
</tr>
<tr>
<td align="right">Product Name/Model:</td>
<td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Description:</td>
<td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td>
</tr>
<tr>
<td align="right">Products Specification:</td>
<td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td>
</tr>
<tr>
<td align="right">Product Price:</td>
<td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Image:</td>
<td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td></td>
<td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td align="right">Keywords:</td>
<td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td>
</tr>
</table>
</form>
</div> <?php } ?>
</td>
</tr>
</table>
Your pro_id field is commented out in your HTML using <!-- and -->, so the following never is true:
if(isset($_GET['pro_id']))
Also, you have a mismatch between your form method POST and $_GET that you are looking for.
Your query of update is correct? I think you must use
UPDATE products1 SET dept_id='$dept_id',cat_id ='$cat_id'... the rest of values
WHERE pro_id='$id'
And verify if your dept_id is INT as well cat_id, so if they are INT you don't need ''
UPDATE products1 SET dept_id=$dept_id,cat_id =$cat_id
Try to do this steps:
First thing comment out this line,
<!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>-->
next step,
you are sending data using POST i.e. (form method="post"), so use this
if(isset($_POST['pro_id'])) , then comment out $pro_id = $_POST['pro_id'];
you will get $pro_id value.
Im trying to make an update form using php and updating the sql database but it seems not to work here. I tried updating the form and then it should say that its updated but it doesn't want to work. I need help please. Thanks
<?php
include 'connections/connection.php';
$updateinfo = #$_POST['update'];
$get_info = mysql_query("SELECT * FROM customer");
$id = #$_REQUEST['id'];
$get_row = mysql_fetch_assoc($get_info);
$db_cname = $get_row['customer_name'];
$db_cemail = $get_row['customer_email'];
$db_caddress = $get_row['customer_address'];
$db_cphone = $get_row['customer_phone'];
if ($updateinfo) {
$cname = strip_tags(#$_POST['cname']);
$cemail = strip_tags(#$_POST['cemail']);
$caddress = strip_tags(#$_POST['caddress']);
$cphone = strip_tags(#$_POST['cphone']);
//Submit the form to the database
$info_submit_query = mysql_query("UPDATE customer SET customer_name='$cname', customer_email='$cemail', customer_address='$caddress', customer_phone='$cphone' WHERE id='$id'") or die("Error! " . mysql_error());
echo "updated!";
}
else
{
echo "error " . mysql_error();
}
?>
<head>
<title>Customer Modification</title>
</head>
<body>
<table border="0" cellpadding="10" cellspacing="5" width="30%" style="margin-top:100px;" align="center" bgcolor="#FFB13C">
<form action="edit.php" method="post">
<tr>
<td><b>Customer Name:*</b></td>
<td><input type="text" size="50" name="cname" value="<?php echo $db_cname; ?>"></td>
</tr>
<tr>
<td><b>Customer Email:*</b></td>
<td><input type="text" size="50" name="cemail" value="<?php echo $db_cemail; ?>"></td>
</tr>
<tr>
<td><b>Customer Address:*</b></td>
<td><input type="text" size="50" name="caddress" value="<?php echo $db_caddress; ?>"></td>
</tr>
<tr>
<td><b>Customer Phone:*</b></td>
<td><input type="text" size="50" name="cphone" value="<?php echo $db_cphone; ?>"></td>
</tr>
<tr>
<td><input type="reset" name="reset" value="Reset"></td>
<td><input type="submit" name="update" value="Edit"></td>
</tr>
</form>
</table>
</body>
I found this code online and now im trying to make it work. I get an error when I press Edit from the first page.
Notice: Undefined variable: id in edit_form2.php on line 19
I'm not sure why this variable isn't included in the code, where should it go? or does it have something to do with my table?
Database = album
Table = data_employees
1. id primaray key & a_i
2. name
3. address
When I press the edit button the next page should load the existing data from the database. Now it just turns up empty + the error code I wrote. What should I do?
edit.php
<td align="center">DATA</td>
</tr>
<tr>
<td>
<table border="1">
<?php
include"dbinc.php";//database connection
$order = "SELECT * FROM data_employees";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[name]</td>");
echo ("<td>$row[employees_number]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>Edit</td></tr>");
}
?>
</table>
edit_form2.php
<?php
include "dbinc.php";//database connection
$order = "SELECT * FROM data_employees
where employees_number='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" name=form action="edit_data2.php">
<input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo "$row[name]"?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="address" size="40"
value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
edit_data2.php
<?php
//edit_data.php
include "dbinc.php";
$name = $_POST["name"];
$address = $_POST["address"];
$id = $_POST["id"];
$order = "UPDATE data_employees
SET name='$name',
address='$address'
WHERE
employees_number='$id'";
mysql_query($order);
header("location:edit.php");
?>
$id variable must be declared before use; like-
$id = mysql_real_escape_string($_GET['id']);
This line is wrong
<input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
try
<input type="hidden" name="id" value="<?php echo $row['employees_number']?>">
So quite likely as the input field does not get built properly the browser is ignoring that field completely.
edit.php
replace
echo ("<tr><td>$row[name]</td>");
echo ("<td>$row[employees_number]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>Edit</td></tr>");
with
echo "<tr><td>".$row['name']."</td>";
echo "<td>".$row['employees_number']."</td>";
echo "<td>".$row['address']."</td>";
echo "<td>Edit</td></tr>";
edit_form.php
<form method="post" name=form action="edit_data2.php">
<input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo "$row[name]"?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="address" size="40"
value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
should be
<?php
$id = mysql_real_escape_string($_GET['id']);include "dbinc.php";//database connection
$order = "SELECT * FROM data_employees
where employees_number='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" name=form action="edit_data2.php">
<input type="hidden" name="id" value="<?php echo $row['employees_number']; ?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo $row['name']; ?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="address" size="40"
value="<?php echo $row['address']; ?>">
</td>
</tr>
<tr>
and like Mukesh Soni said, you need to grab te id from the url
I want all details of user to be fetched in textboxes to edit profile of user accordingly based on username stored in session I don't want which statement is wrong I am not able to fetch values in textboxes. Please help me.
Profile.php
<?php
include('connection.php');
//include('validation1.php');
session_start();
if( !empty($_SESSION['username']) && isset($_SESSION['username']))
{
$username=$_SESSION['username'];
$checkinfo = mysql_query("SELECT * FROM profile WHERE username=".'"$username"');
//$result=mysql_fetch_array($checkinfo);
while($result = mysql_fetch_array($checkinfo,MYSQL_ASSOC)){
$name =($result['name']);
$address =($result['address']);
$contact =($result['contact']);
$state =($result['state']);
$city = ($result['city']);
$username =($result['uname']);
$oldpass = ($result['opass']);
}
}
if (isset($_POST['submit']))
{
$name =($_POST['name_txt']);
$address =($_POST['address_txt']);
$contact =($_POST['contact_txt']);
$gender = ($_POST['gender']);
$country = ($_POST['country']);
$state =($_POST['state_txt']);
$city = ($_POST['city_txt']);
$username =($_POST['uname_txt']);
$oldpass = ($_POST['opass_txt']);
$newpass = ($_POST['npass_txt']);
$insquery="UPDATE INTO profile(name, address, contact,gender, country, state, city,username,opassword,npassword) VALUES ('$name','$address','$contact','$gender','$country','$state','$city','$username','$oldpass','$newpass')";
$result=mysql_query($insquery);
// header("location:display.php");
if(!$insquery)
{
echo "Error Save [".mysql_error()."]";
}
/*else
{
// header("location:display.php");
}*/
}
?>
<body>
<form id="form1" name="form1" method="post" action="">
<div align="center">
Registration Form
</div>
<div align="right">
<?php if(isset($_SESSION['username']))
{
$s="Hello,".$_SESSION["username"];
$r=$_SESSION["userrole"];
echo $s;
} ?><a href='logout.php' id='logout'>Logout</a>
</div>
<table>
<tr>
<td><label>Name:</label></td>
<td><input name="name_atxt" type="text" id="name_atxt" value="<?php echo $name; ?>" /></td>
</tr>
<tr>
<td><label>Address:</label></td>
<td><textarea name="address_txt" cols="40" rows="4" value="<?php echo $address; ?>"></textarea></td>
</tr>
<tr>
<td><label>Contact:</label></td>
<td><input name="contact_txt" type="text" id="contact_ntxt" value="<?php echo $contact; ?>" /></td>
</tr>
<tr>
<td><label>Gender:</label>
<td>
<input type=radio name=gender value="male" id=male >Male</br>
<input type=radio name=gender value="female" id=female >Female</br></td>
</td>
</tr>
<tr>
<td><label>Country:</label></td>
<td><select name="country_select" id="country_select">
<option value="0">--select a country--</option>
<option value="India">India</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
<option value="Australia">Australia</option>
</select></td>
</tr>
<tr>
<td><label>State:</label></td>
<td><input name="state_atxt" type="text" id="state_atxt" value="<?php echo $state; ?>"/></td>
</tr>
<tr>
<td><label>City:</label></td>
<td><input name="city_atxt" type="text" id="city_atxt" value="<?php echo $city; ?>" /></td>
</tr>
<tr>
<td><label>Username:</label></td>
<td><input name="uname_txt" type="text" id="uname_txt" value="<?php echo $username; ?>" /></td>
</tr>
<tr>
<td><label>Old Password:</label></td>
<td><input name="opass_txt" type="password" id="opass_txt" value="<?php echo $oldpass; ?>" /></td>
</tr>
<tr>
<td><label>New Password:</label></td>
<td><input name="npass_txt" type="text" id="npass_txt" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit" id="btn1" /></td>
<td><input name="reset_btn" type="reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
I want values already stored in database should be fetched in textbox of particular user who is in session and it is not fetching values in textbox.
$checkinfo = mysql_query("SELECT * FROM profile WHERE username=".'"$username"');
should be
$checkinfo = mysql_query("SELECT * FROM profile WHERE username='$username'");
Also your UPDATE syntax is incorrect
SQL UPDATE Statement:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value