I have been trying to update a record on my table by using an html form. I am able to create and delete a record successfully but I am unable to update it. I am not sure what I have done wrong. Could it be the SQL query syntax? or is my save button not calling my condition statement? I would appreciate any advice given.
ps. I am aware that my SQL database is open to SQL injection. It will be implemented soon!
<?php
include('partregister2.php');
$epr='';
$msg='';
if(isset($_GET['epr']))
$epr=$_GET['epr'];
//+++++++++++++++ UPDATE PARTICIPANTS RECORD +++++++++++++++++
if($epr=='saveup'){
$Name=$_POST['name'];
$Surname=$_POST['surname'];
$Date_of_Birth=$_POST['dob'];
$Age_at_Camp=$_POST['age'];
$Branch=$_POST['branch'];
$Gender=$_POST['gender'];
$Address=$_POST['address'];
$Contact_No=$_POST['contactNo'];
$Next_of_Kin=$_POST['nextKin'];
$Kin_ContactNo=$_POST['kinContact'];
$Attendance_Camp=$_POST['attendCamp'];
$Attendance_School=$_POST['attendSchool'];
$Comments=$_POST['comments'];
$event_name_FK=$_POST['Event_Name'];
$Room_Name_FK=$_POST['Room_Name'];
$a_sql = mysql_query("UPDATE participants SET Name='$Name',Surname='$Surname',Date_of_Birth ='$Date_of_Birth',Age_at_Camp ='$Age_at_Camp',Branch ='$Branch',Gender ='$Gender',Address ='$Address',
Contact_No ='$Contact_No',Next_of_Kin ='$Next_of_Kin',Kin_ContactNo = '$Kin_ContactNo',Attendance_Camp ='$Attendance_Camp',Attendance_School ='$Attendance_School',Comments ='$Comments',event_name_FK ='$event_name_FK',Room_Name_FK ='$Room_Name_FK' WHERE partID='$id'");
if(a_sql)
header("location:index.php");
else
$msg='Error : '.mysql_error();
}
?>
<html>
<head>
</head>
<body>
<?php
if($epr=='update'){
$id=$_GET['id'];
$row=mysql_query("SELECT * FROM participants WHERE partID='$id'");
$st_row=mysql_fetch_array($row);
?>
<h2 align="center">Update Participant Records</h2>
<form method="POST" action='index.php?epr=saveup'>
<table align="center">
<tr>
<td>First Name:</td>
<td><input type='text' name ='name' value="<?PHP echo $st_row['Name'] ?>"/></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type='text' name ='surname' value="<?PHP echo $st_row['Surname'] ?>"/></td>
</tr>
<tr>
<td>Date of Birth:</td>
<td><input type='date' name ='dob' value="<?PHP echo $st_row['Date_of_Birth'] ?>"/></td>
</tr>
<tr>
<td>Age at Camp:</td>
<td><input type='text' name ='age' value="<?PHP echo $st_row['Age_at_Camp'] ?>"/></td>
</tr>
<tr>
<td>Branch:</td>
<td><select name='branch' value="<?PHP echo $st_row['Branch'] ?>"/>
<option></option>
<option>Brixton</option>
<option>North London</option>
<option>East London</option>
<option>Southall</option>
<option>Leicester</option>
<option>Newport</option>
<option>Liverpool</option></td>
</tr>
</select>
<tr>
<td>Gender:</td>
<td>Male<input type="radio" value="male" name="gender" value="<?PHP echo $st_row['Gender'] ?>"/>
Female<input type="radio" value="female" name="gender" value="<?PHP echo $st_row['Gender'] ?>" /><td/>
</tr>
<tr>
<td>Address:</td>
<td><input type='text' name ='address' value="<?PHP echo $st_row['Address'] ?>"/></td>
</tr>
<tr>
<td>Contact No:</td>
<td><input type='text' name ='contactNo' value="<?PHP echo $st_row['Contact_No'] ?>"/></td>
</tr>
<tr>
<td>Next of Kin:</td>
<td><input type='text' name ='nextKin' value="<?PHP echo $st_row['Next_of_Kin'] ?>"/></td>
</tr>
<tr>
<td>Kin's Contact No:</td>
<td><input type='text' name ='kinContact' value="<?PHP echo $st_row['Kin_ContactNo'] ?>"/></td>
</tr>
<tr>
<td>Attendance at Camp:</td>
<td><input type='text' name ='attendCamp' value="<?PHP echo $st_row['Attendance_Camp'] ?>"/></td>
</tr>
<tr>
<td>Attendance at Sunday School:</td>
<td><input type='text' name ='attendSchool' value="<?PHP echo $st_row['Attendance_School'] ?>"/></td>
</tr>
<tr>
<td>Comments:</td>
<td><input type='text' name ='comments' value="<?PHP echo $st_row['Comments'] ?>"/></td>
</tr>
<tr>
<td>Event Name:</td>
<td><select name='Event_Name' value="<?PHP echo $st_row['event_name_FK'] ?>">
<?php
$res = mysql_query("SELECT * FROM events");
while($row=mysql_fetch_array($res))
{
?>
<option>
<?php echo $row["Event_Name"]; ?>
</option>
<?php } ?>
</tr>
</select>
<tr>
<td>Allocate Room:</td>
<td><select name='Room_Name' value="<?PHP echo $st_row['Room_Name_FK'] ?>">
<?php
$res = mysql_query("SELECT * FROM rooms");
while($row=mysql_fetch_array($res))
{
?>
<option>
<?php echo $row["Room_Name"]; ?>
</option>
<?php } ?>
</td>
</select>
</tr>
<td></td>
<tr>
<td></td>
<td><input type ='submit' name='save'/></td>
</tr>
</table>
</form>
<?php } else{
?>
</body>
</html>
I think you forgot to add $
$a_sql = mysql_query("UPDATE participants SET Name='$Name',Surname='$Surname',Date_of_Birth ='$Date_of_Birth',Age_at_Camp ='$Age_at_Camp',Branch ='$Branch',Gender ='$Gender',Address ='$Address',
Contact_No ='$Contact_No',Next_of_Kin ='$Next_of_Kin',Kin_ContactNo = '$Kin_ContactNo',Attendance_Camp ='$Attendance_Camp',Attendance_School ='$Attendance_School',Comments ='$Comments',event_name_FK ='$event_name_FK',Room_Name_FK ='$Room_Name_FK' WHERE partID='$id'");
if($a_sql) //here
header("location:index.php");
else
$msg='Error : '.mysql_error();
After executing sql query, you have used a_sql variable without $ sign inside if condition, i have improved your code.
if($a_sql)
header("location:index.php");
else
$msg='Error : '.mysql_error();
Related
I am at the very basic level of coding and I need help regarding this. You can post other links if there are any, related to this thing.
I want to get values from database dynamically on selection of city drop down and set it in a text box.
Any help will be appreciated.!!
enter image description here
<tr>
<td>
<label>City</label>
</td>
<td>
<select id="state" name="city" class="form-control" >
<?php
$sql = "SELECT city FROM service_point GROUP BY city";
$sel = mysql_query($sql);
// output data of each row
while($row = mysql_fetch_array($sel)) {
$city=$row['city'];
?>
<option value="<?php echo $row['city'] ?>">
<?php echo $row['city']; ?></option>
<?php }?>
</select>
</td>
</select>
</tr>
<tr>
<td> </td>
</tr>
<?php
include('config.php');
$s_name="";
$address="";
$phone_no="";
$email="";
$cperson="";
$city="";
$id=$_POST['city'];
$sql = "SELECT * FROM service_point where city = $id'' ";
$query = mysql_query($sql);
echo $sql;
while($row = mysql_fetch_array($query)){
?>
<tr>
<td> Name Of Service Point</td>
<td><input type="text" name="name" value="<?php echo $row['s_name'];?>"/></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Address </td>
<td><textarea name="address" col="5" value="<?php echo $row['address'];?>"></textarea></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Phone No</td>
<td><input type="text" name="phoneno" value="<?php echo $row[' phone_no'];?>"/></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Email Id</td>
<td><input type="text" name="email" value="<?php echo $row['email'];?>"/></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Contact Person</td>
<td><input type="text" name="cperson" value="<?php echo $row['cperson'];?>"/></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" name="submit" value="submit"/><td>
</tr>
</table>
<?php }?>
</form>
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.
Thank you for taking the time to read this. I am using an HTML form and submitting information to a MySQL database through it, using PHP. I can not figure out why the information is not making it into the database though. Any help would be greatly appreciated.
Here is what the database looks like:
This is the HTML form:
<?php ?>
<html>
<head>
<title>Raid Boss Strategy Editor</title>
</head>
<body>
<h3>Raid Strats</h3>
<p>
<form action="stratadd2.php" method="POST">
<table cellpadding="3" cellspacing="4" border="0">
<tr>
<td>Boss Name</td>
<td><input type="text" name="bossName" value="<?php echo "$bossName"; ?>"></td>
<td></td>
</tr>
<tr>
<td>Raid Zone</td>
<td><input type="text" name="raidZone" value="<?php echo "$raidZone"; ?>"></td>
<td></td>
</tr>
<tr>
<td>Boss Health</td>
<td><input type="text" name="bossHealth" value="<?php echo "$bossHealth"; ?>"></td>
<td></td>
</tr>
<tr>
<td>Boss Enrage</td>
<td><input type="text" name="bossEnrage" value="<?php echo "$bossEnrage"; ?>"></td>
<td></td>
</tr>
<tr>
<td>Boss Abilities</td>
<td><input type="text" name="bossAbilities" value="<?php echo "$bossAbilities"; ?>"></td>
<td></td>
</tr>
<tr>
<td>Phase One</td>
<td><input type="text" name="bossPhaseone" value="<?php echo "$bossPhaseone"; ?>"></td>
<td></td>
</tr>
<tr>
<td>Phase Two</td>
<td><input type="text" name="bossPhasetwo" value="<?php echo "$bossPhasetwo"; ?>"></td>
<td></td>
</tr>
<tr>
<td>Phase Three</td>
<td><input type="text" name="bossPhasethree" value="<?php echo "$bossPhasethree"; ?>"></td>
<td></td>
</tr>
<tr>
<td>Final Notes</td>
<td><input type="text" name="finalNotes" value="<?php echo "$finalNotes"; ?>"></td>
<td></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"> <input type="reset" name="reset" value="reset"></td>
</tr>
</table>
</form>
</body>
</html>
This is the PHP code:
<?php
require_once 'dbc.php';
error_reporting(0);
if($_POST['submit'])
{
$bossName = $_POST['bossName'];
$raidZone = $_POST['raidZone'];
$bossHealth = $_POST['bossHealth'];
$bossEnrage = $_POST['bossEnrage'];
$bossAbilities = $_POST['bossAbilities'];
$bossPhaseone = $_POST['bossPhaseone'];
$bossPhasetwo = $_POST['bossPhasetwo'];
$bossPhasethree = $_POST['bossPhasethree'];
$finalNotes = $_POST['finalNotes'];
require "dbc.php";
$query = mysql_query("INSERT INTO raidstrats VALUES ('','$bossName','$raidZone','$bossHealth','$bossEnrage','$bossAbilities','$bossPhaseone','$bossPhasetwo','$bossPhasethree','$finalNotes')");
die("Information Submitted!");
}
?>
Probably to do with inserting an empty string ('') into an autoincrement index column (Id). Try specifying the column names
$query = mysql_query("INSERT INTO raidstrats (bossName,raidZone,bossHealth,bossEnrage,bossAbilities,bossPhaseone,bossPhasetwo,bossPhasethree,finalNotes) VALUES ('$bossName','$raidZone','$bossHealth','$bossEnrage','$bossAbilities','$bossPhaseone','$bossPhasetwo','$bossPhasethree','$finalNotes')");
<?php
require_once 'dbc.php';
error_reporting(0);
if($_POST['submit'])
{
$bossName = $_POST['bossName'];
$raidZone = $_POST['raidZone'];
$bossHealth = $_POST['bossHealth'];
$bossEnrage = $_POST['bossEnrage'];
$bossAbilities = $_POST['bossAbilities'];
$bossPhaseone = $_POST['bossPhaseone'];
$bossPhasetwo = $_POST['bossPhasetwo'];
$bossPhasethree = $_POST['bossPhasethree'];
$finalNotes = $_POST['finalNotes'];
require "dbc.php";
$query = mysql_query("INSERT INTO raidstrats VALUES (NULL,$bossName, $raidZone, $bossHealth, $bossEnrage, $bossAbilities, $bossPhaseone, $bossPhasetwo', $bossPhasethree, $finalNotes)");
die("Information Submitted!");
}
?>
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
This is a php code i wrote to edit a line in an array "article" every line is compsed by:
(Colonne: Type)
(ref : int(11),
nom :varchar(25),
cat : varchar(25),
prix : int(11),
fabricant : varchar(35),
photo :varchar(35))
so i make this php code:
<?php
require_once("connection.php");
$ref=$_POST['ref'];
$req="Select * from article where ('ref =". $ref."')";
$rs= mysql_query($req) or die(mysql_error());
$AR=mysql_fetch_assoc($rs);
?>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form method="POST" action="modifierArticle.php" enctype="multipart/form-data">
<table>
<tr>
<td>Référence:</td>
<td><input type="text" name="ref" value="<?php echo ($AR['ref']) ?>" readonly="true"></td>
</tr>
<tr>
<td>Nom:</td>
<td><input type="text" name="nom" value="<?php echo ($AR['nom']) ?>"></td>
</tr>
<tr>
<td> Catégorie: </td>
<td><input type="text" name="cat" value="<?php echo ($AR['cat']) ?>"></td>
</tr>
<tr>
<td> Prix Unitaire: </td>
<td><input type="text" name="prix" value="<?php echo ($AR['prix']) ?>"></td>
</tr>
<tr>
<td> Fabricant: </td>
<td><input type="text" name="fab" value="<?php echo ($AR['fabricant']) ?>"></td>
</tr>
<tr>
<td>Photo: </td>
<td><input type="file" name="photo"><img src="./images/<?php echo ($AR['photo']) ?>"</td>
</tr>
<tr>
<td></td>
<td> <input type="submit" value="Enregistrer"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
mysql_free_result($rs);
mysql_close($conn);
?>
so i got an error:
Notice: Undefined index: ref in ...\editerArticle.php on line 3
Notice: Undefined index: ref in ...\editerArticle.php on line 3
Use this code
<?php
require_once("connection.php");
$ref=$_POST['ref'];
$req="Select * from article where ref =$ref";
$rs= mysql_query($req) or die(mysql_error());
?>
<html>
<had>
<meta charset="utf-8">
</had>
<body>
<form method="POST" action="modifierArticle.php" enctype="multipart/form-data">
<table>
<?php while($AR=mysql_fetch_assoc($rs)) { ?>
<tr>
<td>Référence:</td>
<td><input type="text" name="ref" value="<?php echo ($AR['ref']) ?>" readonly="true"></td>
</tr>
<tr>
<td>Nom:</td>
<td><input type="text" name="nom" value="<?php echo ($AR['nom']) ?>"></td>
</tr>
<tr>
<td> Catégorie: </td>
<td><input type="text" name="cat" value="<?php echo ($AR['cat']) ?>"></td>
</tr>
<tr>
<td> Prix Unitaire: </td>
<td><input type="text" name="prix" value="<?php echo ($AR['prix']) ?>"></td>
</tr>
<tr>
<td> Fabricant: </td>
<td><input type="text" name="fab" value="<?php echo ($AR['fabricant']) ?>"></td>
</tr>
<tr>
<td>Photo: </td>
<td><input type="file" name="photo"><img src="./images/<?php echo ($AR['photo']) ?>"</td>
</tr>
<tr>
<td></td>
<td> <input type="submit" value="Enregistrer"></td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>
<?php
mysql_free_result($rs);
mysql_close($conn);
?>
you need to use isset and iempty to ensure you have value in your $_POST
<?php
require_once("connection.php");
if (isset($_POST['ref']) && !empty ($_POST['ref']))
{
$ref=$_POST['ref'];
$req="Select * from article where ref ='.$ref.'";
$rs= mysql_query($req) or die(mysql_error());
while($rs = mysql_fetch_array($rs)){
$ref = $rs['ref'];
$nom = $rs['nom'];
$cat = $rs['cat'];
$prix = $rs['prix'];
$fabricant = $rs['fabricant'];
$photo = $rs['photo'];
}
?>
html
<form method="POST" action="modifierArticle.php" enctype="multipart/form-data">
<table>
<tr>
<td>Référence:</td>
<td><input type="text" name="ref" value="<?php echo $ref; ?>" readonly="true"></td>
</tr>
<tr>
<td>Nom:</td>
<td><input type="text" name="nom" value="<?php echo $nom; ?>"></td>
</tr>
<tr>
<td> Catégorie: </td>
<td><input type="text" name="cat" value="<?php echo $cat; ?>"></td>
</tr>
<tr>
<td> Prix Unitaire: </td>
<td><input type="text" name="prix" value="<?php echo $prix; ?>"></td>
</tr>
<tr>
<td> Fabricant: </td>
<td><input type="text" name="fab" value="<?php echo $fabricant; ?>"></td>
</tr>
<tr>
<td>Photo: </td>
<td><input type="file" name="photo"><img src="./images/<?php echo $photo; ?>"</td>
</tr>
<tr>
<td></td>
<td> <input type="submit" value="Enregistrer"></td>
</tr>
</table>
</form>
</body>
</html>
end mysql connnection
<?php
mysql_free_result($rs);
mysql_close($conn);
?>
EDIT
EDITED FEW SYNTEX mistakes :)
if(isset($_POST['ref']))
{
$ref=$_POST['ref'];
$req="Select * from article where ref = $ref";
$rs= mysql_query($req) or die(mysql_error());
$AR=mysql_fetch_assoc($rs);
}
You had a syntax error in $req add the if condition to make sure that $_POST['ref'] isset before running the query
HTML
<html>
<had>
<meta charset="utf-8">
</had>
<body>
<form method="POST" action="modifierArticle.php" enctype="multipart/form-data">
<table>
<tr>
<td>Référence:</td>
<td><input type="text" name="ref" value="<?php echo $AR['ref']; ?>" readonly="true"></td>
</tr>
<tr>
<td>Nom:</td>
<td><input type="text" name="nom" value="<?php echo $AR['nom']; ?>"></td>
</tr>
<tr>
<td> Catégorie: </td>
<td><input type="text" name="cat" value="<?php echo ;AR['cat']; ?>"></td>
</tr>
<tr>
<td> Prix Unitaire: </td>
<td><input type="text" name="prix" value="<?php echo $AR['prix']; ?>"></td>
</tr>
<tr>
<td> Fabricant: </td>
<td><input type="text" name="fab" value="<?php echo $AR['fabricant']; ?>"></td>
</tr>
<tr>
<td>Photo: </td>
<td><input type="file" name="photo"><img src="../images/<?php echo $AR['photo']; ?>"</td>
</tr>
<tr>
<td></td>
<td> <input type="submit" value="Enregistrer"></td>
</tr>
</table>
</form>
</body>
</html>
Use a array_key_exists check. Like this:
if(array_key_exists('ref', $_POST)) {
//your code here
}
check whether the POST request is empty or not
if(!empty($_POST['ref']))
{
$ref=$_POST['ref'];
}
else
{
echo 'no value';
exit;
}
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