How to change the value of session - php

The app.php is getting the post value from another page. And that value used for query purposes. That post value is used to edit multiple values in app.php. I post again the multiple values to edit.php to update the records that shows in app.php. What I want is when I back into app.php using <meta http-equiv="refresh" content="0;app.php" /> or <script type="text/javascript">window.history.go(-1);</script> the value in app.php should get the new or the updated value from edit.php. I'm not good using sessions. Help please?
app.php
$mysqli = new mysqli("localhost", "root", "", "app");
ob_start();
session_start()
<form name="myform" method="post" action="edit.php"/>
if (isset($_POST['submit'])) {
//unset the session value
$_SESSION['content'] = '';
$drop = $_POST['drop_1'];
$tier_two = $_POST['tier_two'];
$where = "WHERE a.app_cn='$drop' AND a.app_plan_no='$tier_two'";
$result1 = $mysqli->query(" query ");
<table>
while ($row = $result1->fetch_assoc()) {
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['id'] . '"'.($row['pr'] == ""?"disabled ":"").' style="cursor:pointer;" class="checkbox"></td>
}
</table>
$sPageContent = ob_get_clean();
$_SESSION['content'] = $sPageContent;
echo $_SESSION['content'];
} else {
if (isset($_SESSION['content'])) {
echo $_SESSION['content'];
}
}
</form>
Edit.php
<?php
$mysqli = new mysqli("localhost", "root", "", "app");
if (isset($_POST['submit'])) {
$counter=$_POST['counter'];
$pr=$_POST['pr'];
$pr_qty=$_POST['pr_qty'];
$N = count($counter);
for($i=0; $i < $N; $i++)
{
$result = $mysqli->query("UPDATE purchase_request SET pr='$pr[$i]', total_quantity='$pr_qty[$i]' where counter='$counter[$i]'");
}
echo'<meta http-equiv="refresh" content="0;app.php" />';
}
?>
<form class="form-horizontal" action="" method="post">
<?php
$id=$_POST['checkbox'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
$result1 = $mysqli->query("
SELECT a.item_name, a.item_description, a.counter, b.counter, b.pr, b.total_quantity
FROM app a
LEFT OUTER JOIN purchase_request b
ON a.counter=b.counter
WHERE a.counter='$id[$i]'
");
while ($row = $result1->fetch_assoc())
{ ?>
<input name="item_name[]" class="textbox" type="text" value="<?php echo $row['item_name'] ?>"/>
<input name="item_description[]" class="textbox" type="text" value="<?php echo $row['item_description'] ?>"/>
<input name="pr_qty[]" id="pr_qty[]" class="textbox tb1" type="text" value="<?php echo $row['total_quantity']; ?>" />
<input name="pr[]" class="textbox" type="text" value="<?php echo $row['pr'] ?>" />
?>
<input name="submit" type="submit" id="sbtBtn" value="Update">
</form>

using header('Location: app.php') instead of <meta http-equiv="refresh" content="0;app.php" />
if you are using javascript to return the page it may keep the previous data. It was because our browser have the cache.
You may use
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
and
<META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 22 Jul 2002 11:12:01 GMT">
to disable the cache

Related

why this code dosen't update the data

I can't update the data on this code . it just return to the main page without any update . and i am sure about the connection data on connection file .
and all the column in the data bas is correct
so what is the problem please
<?
include "../include/config.php";
echo "<a href='del_update.php'>Delete</a>"
?>
<div class="articleall">
<?
$id = $_REQUEST['do'];
if($_REQUEST['do'] == 'remove'){
$gid= $_GET['id'] ;
$de = mysql_query("delete from article where id='$gid'");
if($de){
echo "Delete doen";
echo '<meta http-equiv="refresh" content="2; url=del_update.php"/>';
exit ;
}
}
#############################################
if($_REQUEST['do'] == 'update'){
$gid2 = $_GET['id'];
$sel = mysql_query("select * from article where id='$gid2'");
$row2 = mysql_fetch_assoc($sel);
$id2 =$row2['id'];
$name2= $row2['name'];
$auther2= $row2['auther'];
$text2 = $row2['text'];
####################################### post
$postid = $_POST['id'];
$postname = $_POST['name'];
$postauther = $_POST['auther'];
$posttext = $_POST['text'];
if($_POST['del_update']){
if($postname ==''){
echo "it is empty";
echo '<meta http-equiv="refresh" content="2; url=del_update.php"/>';
exit ;
}
else {
$update = mysql_query("update article set
name ='$postname',
auther='$postauther'
where id ='$postid'
");
if(isset($update)){
echo "update done ";
echo '<meta http-equiv="refresh" content="2; url=del_update.php"/>';
exit ;
}
}
}
?>
<form action="del_update.php?do=update" method="post" >
title : <input type="text" class="name" name="name" value="<?=$name2; ?>"></br>
auther : <input type="text" class="name" name="auther" value="<?=$auther2;?>"></br>
date : <input type="text" class="name" name="date" value=""></br>
<input type="hidden" class="name" name="id" value=""></br>
text : <textarea value="" id="elm1" name="elm1" rows="15" cols="80" style="width: 80%">
<? echo $text2; ?>
</textarea>
<input type="submit" class="botton" value="update" name="go"><br>
</form>
<?
}
?>
<?
$query = mysql_query("select * from article order by id desc ");
while ($row = mysql_fetch_assoc($query)
){
$id = $row['id'];
$name = $row['name'];
echo "
<div class='shortarticle'>
<h3>$name ||
<a href='?do=remove&id=$id'>Delete</a>||
<a href='?do=update&id=$id'>Update</a>
</h3>
</div>
</div> ";
}
?>

trying to edit SQL via php but keep getting an error

as far as i can see my code is sound however, I keep getting an error
this is the error
Notice: Undefined variable: person in
\sql\modify.php on line 12
here is my code..
<?php
include 'includes/connection.php';
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
<?php
if(isset($_POST['sumbmit'])) {
$u = "UPDATE people SET `Name` = '$_POST[inputName]', `Description` = '$_POST[inputDesc]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modify";
header("Location: index.php");
}
?>
any Thoughts or am im I just blind???
<?php
include 'includes/connection.php';
// set $person veriable
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
// if form submit you use update and redirect
else {
$u = "UPDATE people SET `Name` = '$_POST[inputName]', `Description` = '$_POST[inputDesc]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
//echo "User has been modify"; // this not need, bcz execute header('location') redirect you current page
header("Location: index.php");
exit(); //use it after header location
}
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
You just need to check if you actually got output.
Just a plain example:
if ($person):
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
<?php
endif;
Remember to always validate your input and output, but also if queries you try to run do produce a result set.
Here's the issue:
<?php
include 'includes/connection.php';
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
?>
At this point $person is set only if the form hasn't been submitted; but later on:
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
You're using it anyway. If the form has been submitted, then you're going to get the warning you're seeing. What you need to do is something like:
if (isset($_POST['submit'])){
$name = $_POST['name'];
$description = $_POST['description']
} else {
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
$name = $person['name'];
$description= $person['description'];
}
And then:
Name<input type="text" name="inputName" value="<?php echo $name ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $description; ?>" />
The variables are now set either way.
A couple of other things - you're not doing any error checking to see if your query has worked; if the query fails, your code will carry on regardless.
Secondly, the mysql_ functions are deprecated and will stop working at some point; you should look at moving to using mysqli_* or PDO instead.

Edit record (should be so easy)

I have looked everywhere here in Stackoverflow and I´ve searced 16.493 sites on Google but no answers to the most basic thing in php (edit record)
I´ve managed to code the most complicated stuff - but this is like a cancer and would also help others.
I have to files - edit.php - and update.php
edit.php works and it retrieves the data from the record
Here is the edit.php
<?php
mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$UID = (int)$_GET['id'];
$query = mysql_query("SELECT * FROM cloudbig WHERE id = '$UID'") or die(mysql_error());
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$fs = $row['fs'];
$texti = $row['texti'];
}
?>
<form name="form1" method="post" action="update.php">
<input type="text" name="fs" value="<?php echo $texti ?>" size="60">
<textarea rows="8" name="texti" id="userName" cols="60"><?php echo $texti ?></textarea>
<input type="submit" name="save" value="submit" />
</form>
<?php
}
?>
and here is update.php
<?php
$id = $_REQUEST["id"];
$fs = $_POST["fs"];
$texti = $_POST["texti"];
mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
echo "MySQL Connection Established! <br>";
mysql_select_db("db") or die(mysql_error());
echo "Database Found! <br>";
$query = "UPDATE cloudbig SET fs = '$fs', texti = '$texti' WHERE id = '$id'";
$res = mysql_query($query);
if ($res)
echo "<p>Record Updated<p>";
else
echo "Problem updating record. MySQL Error: " . mysql_error();
?>
I´ve done a whole news/online magazine site in php but simple edit.php function is a problem
I think that the short answer is that you never post the "id" up to the update.php script. Your form needs to look like this:
<form name="form1" method="post" action="update.php">
<input type="hidden" name="id" value="<?php echo $UID ?>">
<input type="text" name="fs" value="<?php echo $fs; ?>" size="60">
<textarea rows="8" name="texti" id="userName" cols="60"><?php echo $texti ?></textarea>
<input type="submit" name="save" value="submit" />
</form>
which will send the id into the POST array where it can be accessed by $id = $_REQUEST["id"];
You can also accomplish this by sending it via _GET by modifying the form action:
<form name="form1" method="post" action="update.php?id=<?php echo $UID ?>">
<input type="text" name="fs" value="<?php echo $fs; ?>" size="60">
<textarea rows="8" name="texti" id="userName" cols="60"><?php echo $texti ?></textarea>
<input type="submit" name="save" value="submit" />
</form>
which will put it in the $_GET array where it will also be seen in the $_REQUEST array.
Lastly, there are some MAJOR ISSUES with your code:
First and foremost, it is subject to SQL injection! You MUST escape
your variables before passing them into a MySQL query.
Second. As pointed out by iDifferent, you appear to bve echoing the wrong value into the fs field (you're setting it equal to the texti field)
Third, why do you have this loop?
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$fs = $row['fs'];
$texti = $row['texti'];
}
If you're fetching by ID you should never have duplicates. Make sure that ID is a primary key and there is no reason to check for multiple rows.

how to update postgresql rows using php?

first I want to say that I'm a beginner in postgresql and php.. my company told me to create a database that they can view and edit on local server.. so I created the database in postgresql.. created a page that views the database:
<html>
<head>
<title>Ongoing projects</title>
</head>
<body bgcolor="666657">
<?php
//database access information
require_once("DB.php");
$host = "localhost";
$user = "admin";
$pass = "";
$db = "Project_list";
$port = "5432";
//open a connection to the database server
$connection = pg_connect("host=$host dbname=$db user=$user password=$pass port=$port");
if (!$connection)
{
die("Could not open connection to database server");
}
?>
<?php
$query = 'select * from ongoing';
$result = pg_query($query); $i = 0;
echo '<html><table bgcolor="666657" width="10" height="30" border="0" cellpadding="0" cellspacing="0"><td align="center"> <h1><font color = "#ffb200"> Ongoing projects</h1>';
echo '<html><body><table border= 2 BORDERCOLOR="000000" cellpadding="1" cellspacing="0"> <tr >';
while ($i < pg_num_fields($result)) {
$fieldName =pg_field_name($result, $i);
echo '<b>'.'<td width="2" bgcolor="666657" align="center">'.'<font color = "#ffb200">'. '</b>'.'<b>'. $fieldName . '</b>'. '</td>';
$i = $i + 1; }
echo("<td><align= center><font color = #ffb200><b>Action</td>");
echo '</tr>' ;
$i = 0;
while ($row = pg_fetch_row($result)) {
echo '<tr align="center" width="1">';
$count = count($row);
$y = 0;
while ($y < $count) {
$c_row = current($row);
echo '<td>' .'<font color = "#ffb200">'. $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo("<td><align= center><a href='editongoing.php?ProjectID=".$row[0]."'>Edit</a></td>");
echo '</tr>';
$i = $i + 1;
}
pg_free_result($result);
echo '</table></body></html>';
?>
<h3>
<a href="projects.php"</a>Back to projects page</a>
</h3>
<SCRIPT LANGUAGE="JavaScript">
if (window.print) {
document.write('<form> '
+ '<input type=button name=print value="Click" '
+ 'onClick="javascript:window.print()"> To Print!</form>');
}
// End -->
</script>
when you click the edit button, you will go to this page where you can edit the raw you want, this is the (edit) code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Edit Ongoing projects</title>
</head>
<body bgcolor="666657">
<?php
// attempt a connection
$connection = pg_connect("host=localhost dbname=Project_list user=admin password=");
if (!$connection) {
die("Error in connection: " . pg_last_error());
}
if ($_REQUEST['ProjectID']!=''){
$QueryStr = "Select * from ongoing where project_no='".$_REQUEST['ProjectID']."'";
$result = pg_query($connection, $QueryStr);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
$row = pg_fetch_row($result);
print_r($row);
}
if ($_POST['submit']) {
// escape strings in input data
$project_no = pg_escape_string($_POST['project_no']);
$title = pg_escape_string($_POST['title']);
$duration = pg_escape_string($_POST['duration']);
$manager = pg_escape_string($_POST['manager']);
$country = pg_escape_string($_POST['country']);
$total_fee = pg_escape_string($_POST['totalfee']);
$performed = pg_escape_string($_POST['performed']);
$remaining = pg_escape_string($_POST['remaining']);
$gross_profit = pg_escape_string($_POST['gross_profit']);
$gp = pg_escape_string($_POST['gp']);
$performance_year = pg_escape_string($_POST['performance_year']);
$gp_year = pg_escape_string($_POST['gp_year']);
// execute query
$sql = "INSERT INTO ongoing (project_no, project_title, duration, manager, country, total_fee,
performed, remaining, gross_profit, gp, performance_year, gp_year)
VALUES('$project_no', '$title', '$duration', '$manager', '$country','$total_fee','$performed','$remaining',
'$gross_profit','$gp', '$performance_year','$gp_year')";
$result = pg_query($connection, $sql);
f (!$result) {
die("Error in SQL query: " . pg_last_error());
}
echo "Data successfully inserted!";
// free memory
pg_free_result($result);
// close connection
pg_close($connection);
}
?>
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><b><font color = "#ffb200">
Project No.: <br> <input id="project_no" type="text" name="project_no" size="20" value=<?= $row[0] ?>>
<p>
Project Title: <br> <input id="title" type="text" name="title" value='<?= $row[1] ?>'>
<p>
Duration: <br> <input ID="duration" type="text" name="duration" value=<?= $row[2] ?>>
<p>
Project Manager: <br> <input ID="manager" type="text" name="manager" value=<?= $row[3] ?>>
<p>
Country: <br> <input ID="country" type="text" name="country" value=<?= $row[4] ?>>
<p>
Total Fee: <br> <input ID="totalfee" type="text" name="total_fee" value=<?= $row[5] ?>>
<p>
Already performed: <br> <input ID="performed" type="text" name="performed" value=<?= $row[6] ?>>
<p>
Remaining performance: <br> <input ID="remaining" type="text" name="remaining" value=<?= $row[7] ?>>
<p>
Gross Profit: <br> <input ID="gross_profit" type="text" name="gross_profit" value='<?= $row[8] ?>'>
<p>
GP%: <br> <input ID="gp" type="text" name="gp" value=<?= $row[9] ?>>
<p>
Performance actual year: <br> <input ID="performance_year" type="text" name="performance_year" value=<?= $row[10] ?>>
<p>
GP actual year: <br> <input ID="gp_year" type="text" name="gp_year" value=<?= $row[11] ?>>
<p>
<input type="submit" name="submit" value="Sumbit my table" size="30">
<P>
<a href="ongoing.php"</a>View ongoing projects</a>
<a href="editproject.php"</a>Back to editing menu</a>
</form>
</body>
</html>
My problem is, when I edit the data and click on submit my table, a new raw is inserted.. but I want it to be updated not inserted... help plz
You need to select which record (id number) you want to update, and then your query will look like
$sql = "UPDATE ongoing SET field1='value', field2='value' ... WHERE id = 'id of project you want to edit'";

php OOP, full sample [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
On this site I found a lot of cool examples of PHP OOP.
Maybe you know where to find complete examples?
Guest books, registration forms, blog...
When I look at the full model is much easier to understand OOP PHP.
As different classes interacting with each other, exchange data
How to use the generic class for sending data to the database
Thanks
Following is the sample code:
class.php
class Database{
/*
* Create variables for credentials to MySQL database
* The variables have been declared as private. This
* means that they will only be available with the
* Database class
*/
private $db_host = "localhost"; // Change as required
private $db_user = "root"; // Change as required
private $db_pass = ""; // Change as required
private $db_name = ""; // Change as required
/*
* Extra variables that are required by other function such as boolean con variable
*/
private $con = false; // Check to see if the connection is active
private $result = array(); // Any results from a query will be stored here
// Function to make connection to database
public function connect(){
if(!$this->con){
$myconn = #mysql_connect($this->db_host,$this->db_user,$this->db_pass); // mysql_connect() with variables defined at the start of Database class
if($myconn){
$seldb = #mysql_select_db($this->db_name,$myconn); // Credentials have been pass through mysql_connect() now select the database
if($seldb){
$this->con = true;
return true; // Connection has been made return TRUE
}else{
array_push($this->result,mysql_error());
return false; // Problem selecting database return FALSE
}
}else{
array_push($this->result,mysql_error());
return false; // Problem connecting return FALSE
}
}else{
return true; // Connection has already been made return TRUE
}
}
// Function to disconnect from the database
public function disconnect(){
// If there is a connection to the database
if($this->con){
// We have found a connection, try to close it
if(#mysql_close()){
// We have successfully closed the connection, set the connection variable to false
$this->con = false;
// Return true tjat we have closed the connection
return true;
}else{
// We could not close the connection, return false
return false;
}
}
}
public function select($sql){
$query = #mysql_query($sql);
// $this->myQuery = $sql; // Pass back the SQL
if($query){
// If the query returns >= 1 assign the number of rows to numResults
$this->numResults = mysql_num_rows($query);
// Loop through the query results by the number of rows returned
for($i = 0; $i < $this->numResults; $i++){
$r = mysql_fetch_array($query);
$key = array_keys($r);
for($x = 0; $x < count($key); $x++){
// Sanitizes keys so only alphavalues are allowed
if(!is_int($key[$x])){
if(mysql_num_rows($query) >= 1){
$this->result[$i][$key[$x]] = $r[$key[$x]];
}else{
$this->result = null;
}
}
}
}
return true; // Query was successful
}else{
array_push($this->result,mysql_error());
return false; // No rows where returned
}
}
// Function to insert into the database
public function insert($sql)
{
// Make the query to insert to the database
if($ins = #mysql_query($sql))
{
array_push($this->result,mysql_insert_id());
return true; // The data has been inserted
}
else
{
array_push($this->result,mysql_error());
return false; // The data has not been inserted
}
}
// Function to update and delete into the database
public function query($sql)
{
if($query = #mysql_query($sql)){
array_push($this->result,mysql_affected_rows());
return true;
}else{
array_push($this->result,mysql_error());
return false;
}
}
// Public function to return the data to the user
public function getResult(){
$val = $this->result;
$this->result = array();
return $val;
}
}
?>
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example Test</title>
</head>
<body>
<?php
if(isset($_REQUEST['failure']))
{
echo "Email/Password Wrong";
}
?>
<form action="operation.php" method="post">
<label>Email:</label>
<input type="text" name="email" />
<br />
<label>Password:</label>
<input type="password" name="password" />
<br />
<input type="submit" name="cmdlogin" value="Login" />
</form>
</body>
</html>
operation.php
<?php
require_once("class.php");
$db = new Database();
$db->connect();
if(isset($_REQUEST['cmdlogin']))
{
$rs = $db->select("SELECT * FROM tbl_login where email = '".$_REQUEST['email']."' and password= '".md5($_REQUEST['password'])."'");
$res = $db->getResult();
if($res)
{
header('location: http://localhost/project/menu.php ');
}
else
{
header('location: http://localhost/project/index.php?failure');
}
}
if(isset($_REQUEST['cmdproduct_save']))
{
$rs = $db->insert("INSERT INTO `tbl_product`(`product`, `description`, `catid`) VALUES ('".$_REQUEST['product']."','".$_REQUEST['description']."',
'".$_REQUEST['drpcat']."')");
$res = $db->getResult();
header('location: http://localhost/project/product.php?saved');
}
if(isset($_REQUEST['cmdcategory_save']))
{
$filename = '';
if(isset($_FILES['catimage']['name']) && $_FILES['catimage']['name'] != '')
{
$filename = time();
$ext=substr($_FILES['catimage']['name'],strrpos($_FILES['catimage']['name'],'.'),strlen($_FILES['catimage']['name'])-1);
$filepath = $_SERVER['DOCUMENT_ROOT'].'project/image/'.$filename.$ext;
move_uploaded_file($_FILES['catimage']['tmp_name'],$filepath);
}
$rs = $db->insert("INSERT INTO `tbl_category`(`category`, `description`,`catimage`) VALUES ('".$_REQUEST['category']."','".$_REQUEST['description']."','".$filename."')");
$res = $db->getResult();
header('location: http://localhost/project/category.php?saved');
}
if(isset($_REQUEST['cmdproduct_update']))
{
$rs = $db->query("UPDATE `tbl_product` SET `product` = '".$_REQUEST['product']."', `description`= '".$_REQUEST['description']."',`catid` = '".$_REQUEST['drpcat']."' where id = ".$_REQUEST['proeditid']);
$res = $db->getResult();
header('location: http://localhost/project/product.php?updated');
}
if(isset($_REQUEST['cmdcategory_update']))
{
//print_r($_REQUEST);exit;
$rs = $db->query("UPDATE `tbl_category` SET `category`= '".$_REQUEST['category']."', `description` = '".$_REQUEST['description']."' where id =".$_REQUEST['cateditid']);
$res = $db->getResult();
//print_r($res);exit;
header('location: http://localhost/project/category.php?updated');
}
if(isset($_REQUEST['catdelete']))
{
$rs = $db->query("DELETE FROM `tbl_category` WHERE id = ".$_REQUEST['catdelete']);
$res = $db->getResult();
header('location: http://localhost/project/category.php?deleted');
}
?>
product.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example Test</title>
</head>
<body>
<?php
include("menu.php");
require_once("class.php");
$db = new Database();
$db->connect();
$db->select("select * from tbl_category");
$rs = $db->getResult();
if(isset($_REQUEST['proeditid']))
{
$db->select("select * from tbl_product where id = ".$_REQUEST['proeditid']);
$result = $db->getResult();
//print_r($result);
}
?>
<form action="operation.php" method="post">
<label>Product:</label>
<input type="text" name="product" value="<?php if(isset($result)){ echo $result[0]['product']; }?>" />
<br />
<label>Description:</label>
<input type="text" name="description" value="<?php if(isset($result)){ echo $result[0]['description']; }?>" />
<br />
<label>Category:</label>
<select name="drpcat">
<option value="0">Select Category</option>
<?php foreach($rs as $val){?>
<option value="<?php echo $val['id']; ?>"<?php if(isset($result) && $result[0]['catid'] == $val['id']){ echo 'selected="selected"';}?>><?php echo $val['category'];?></option>
<?php }?>
</select>
<br />
<?php if(isset($result)) {?>
<input type="hidden" name="proeditid" value="<?php echo $result[0]['id'];?>" />
<input type="submit" name="cmdproduct_update" value="Update" />
<?php }else {?>
<input type="submit" name="cmdproduct_save" value="Save" />
<?php }?>
</form>
<br />
<br />
<br />
<br />
<?php
$db->select("select p.*,c.category from tbl_product as p, tbl_category as c where c.id = p. catid");
$res = $db->getResult();
?>
<table border="1">
<thead>
<tr>
<th>Product</th>
<th>Description</th>
<th>Category</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
foreach($res as $output){
?>
<tr>
<td><?php echo $output['product']; ?></td>
<td><?php echo $output['description']; ?></td>
<td><?php echo $output['category']; ?></td>
<td>
<form action="operation.php" method="post" name="catedit<?php echo $output['id']; ?>">
<input type="hidden" name="proedit" value="<?php echo $output['id']; ?>" />
</form>
Edit
</td>
<td>
<form action="operation.php" method="post" name="prodelete<?php echo $output['id']; ?>">
<input type="hidden" name="prodelete" value="<?php echo $output['id']; ?>" />
</form>
Delete
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
<div style="margin-bottom:30px;">
Category
Product
</div>
menu.php
<div style="margin-bottom:30px;">
Category
Product
</div>
category.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example Test</title>
</head>
<body>
<?php
require_once("class.php");
include("menu.php");
$db = new Database();
$db->connect();
if(isset($_REQUEST['cateditid']))
{
$db->select("select * from tbl_category where id = ".$_REQUEST['cateditid']);
$rs = $db->getResult();
//print_r($rs);
}
?>
<form action="operation.php" method="post" enctype="multipart/form-data">
<label>Category:</label>
<input type="text" name="category" value="<?php if(isset($rs)){ echo $rs[0]['category']; }?>" />
<br />
<label>Description:</label>
<input type="text" name="description" value="<?php if(isset($rs)){ echo $rs[0]['description']; }?>" />
<br />
<label>Category Image:</label>
<input type="file" name="catimage" />
<br />
<?php if(isset($rs)) {?>
<input type="hidden" name="cateditid" value="<?php echo $rs[0]['id'];?>" />
<input type="submit" name="cmdcategory_update" value="Update" />
<?php }else {?>
<input type="submit" name="cmdcategory_save" value="Save" />
<?php }?>
</form>
<br />
<br />
<br />
<br />
<?php
$db->select("select * from tbl_category");
$res = $db->getResult();
?>
<table border="1">
<thead>
<tr>
<th>Category</th>
<th>Description</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
foreach($res as $output){
?>
<tr>
<td><?php echo $output['category']; ?></td>
<td><?php echo $output['description']; ?></td>
<td>
<form action="operation.php" method="post" name="catedit<?php echo $output['id']; ?>">
<input type="hidden" name="catedit" value="<?php echo $output['id']; ?>" />
</form>
Edit
</td>
<td>
<form action="operation.php" method="post" name="catdelete<?php echo $output['id']; ?>">
<input type="hidden" name="catdelete" value="<?php echo $output['id']; ?>" />
</form>
Delete
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
This article helped me very much to understand the basics of OOP
Object Oriented Programming in PHP
I hope it can help you
PHP object oriented solutions
http://www.amazon.com/dp/1430210117
i really enjoyed this book when i was getting rolling with php

Categories