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!");
}
?>
Related
I am not getting my orignal array after using unserialize().
I am retrieving array from database and want to display it.
I have 2 arrays, created them from checkbox input, then I serialize() them and stored it in database.
Create.php
<table>
<tr>
<td>Aadhar</td>
<td><input type="checkbox" name="original[]" value="Aadhar"/></td>
<td><input type="checkbox" name="xerox[]" value="Aadhar"/></td>
</tr>
<tr>
<td>Pan Card</td>
<td><input type="checkbox" name="original[]" value="Pan Card"/></td>
<td><input type="checkbox" name="xerox[]" value="Pan Card"/></td>
</tr>
<tr>
<td>Address</td>
<td><input type="checkbox" name="original[]" value="Address"/></td>
<td><input type="checkbox" name="xerox[]" value="Address"/></td>
</tr>
<tr>
<td>Light Bill</td>
<td><input type="checkbox" name="original[]" value="Light Bill"/></td>
<td><input type="checkbox" name="xerox[]" value="Light Bill"/></td>
</tr>
<tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
Create2.php(this page is just like confirmation page..before user submit to the database.)
<?php
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$company = $_POST['company'];
$original_list = $_POST['original'];
$xerox_list = $_POST['xerox'];
?>
<form action="create2.php" method="POST">
<table cellpadding="10">
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $name; ?>" readonly="readonly" ></td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="mobile" value="<?php echo $mobile; ?>" readonly="readonly" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php echo $email; ?>" readonly="readonly" /></td>
</tr>
<tr>
<td>Company</td>
<td><input type="text" name="company" value="<?php echo $company; ?>" readonly="readonly" /></td>
</tr>
<tr>
<input type="hidden" name="original_list" value="<?php $original_list ?>" />
<input type="hidden" name="xerox_list" value="<?php $xerox_list ?>" />
<td>Documents Selected</td>
<td>
<?php
echo "<b>ORIGINAL</b><br />";
foreach($original_list as $value)
{
echo $value."<br />";
}
?>
</td>
<td>
<?php
echo "<b>XEROX</b><br />";
foreach($xerox_list as $value)
{
echo $value."<br />";
}
?>
</td>
</tr>
<tr>
<td><input type="submit" value="Confirm" /></td>
</tr>
</table>
</form>
</body>
Create2.php (insert in database)
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$company = $_POST['company'];
$original = serialize($_POST['original_list']);
$xerox = serialize($_POST['xerox_list']);
echo $sql = "INSERT INTO users (name,mobile,email,company,original,xerox)
VALUES ('$name','$mobile','$email','$company','$original','$xerox')";
mysqli_query($con,$sql);
So when I try to unserialize, and display the array using foreach, this is what I get:
Warning: Invalid argument supplied for foreach()
Code:
$original = unserialize($rows['original']);
foreach($original as $value)
{
echo $value."<br />";
}
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();
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.
I have tried to get this working for a couple hours now, i think its related to the fact that i have a $_GET['ID']; on the first script but im not sure:
Script 1 (FORM):
<?php
require_once('db_access.php');
$editID = $_GET['id'];
$query = mysql_query("SELECT * from routes where id = '".$editID."'");
$row = mysql_fetch_assoc($query);
?>
<!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>
<table border=1>
<tr>
<td align=center>Route Edit Data</td>
</tr>
<tr>
<td>
<table>
<form method="post" action="complete_edit.php">
<tr>
<td>ID #</td>
<td>
<input type="hidden" name="formid" value="<?php echo $row['id'] ?>">
</td>
</tr>
<tr>
<td>Route Name</td>
<td>
<input type="text" name="route_title" size="40"
value="<?php echo $row['route_title']?>">
</td>
</tr>
<tr>
<td>Total Price</td>
<td>
<input type="text" name="total_price" size="40"
value="<?php echo $row['total_price']?>">
</td>
</tr>
<tr>
<td>Down Payment</td>
<td>
<input type="text" name="down_payment" size="40"
value="<?php echo $row['down_payment']?>">
</td>
</tr>
<tr>
<td>Weekly Net</td>
<td>
<input type="text" name="weekly_net" size="40"
value="<?php echo $row['weekly_net']?>">
</td>
</tr>
<tr>
<td>Location</td>
<td>
<input type="text" name="location" size="40"
value="<?php echo $row['location']?>">
</td>
</tr>
<tr>
<td>Remarks</td>
<td>
<input type="text" name="remarks" size="40"
value="<?php echo $row['remarks']?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
SCRIPT 2(PROCESSING):
<?php
$id = $_POSt['formid'];
$editroute = $_POST['route_title'];
$editprice = $_POST['total_price'];
$editdownpay = $_POST['down_payment'];
$editweeklynet = $_POST['weekly_net'];
$editlocation = $_POST['location'];
$editremarks = $_POST['remarks'];
$query = "UPDATE routes SET id = '$id', route_title = '$editroute', total_price = '$editprice', down_payment = '$editdownpay', weekly_net = '$editweeklynet', location = '$editlocation', remarks = '$editremarks' WHERE id = '$id'";
header('Location:index.php');
?>
The first lot of code is where my form is placed and the second is where the processing happens
Thanks for your help people :)
Alex
$id = $_POSt['formid'];
$_POSt is not $_POST
Im so silly after reading the comments to the question here i realised that i had no mysql_query string :)
Thanks guys for giving me my mind haha :)
Alex