Update record using a specific id..

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
mani123
Forum Newbie
Posts: 8
Joined: Thu Mar 03, 2011 6:48 pm

Update record using a specific id..

Post by mani123 »

I have a form that is displaying rows of data from mysql and contains a pair of buttons.
When the user press any of the button,I want the row of data to be updated but don't know how it will be done can any one help me please??
The code is..

Code: Select all

<head>
<body>
<?php 

session_start(); 
$d= $_SESSION['txt_id'];
include ('dbinfo.inc');
echo mysql_error();

$query=mysql_query("SELECT * FROM `login` WHERE `login_id` LIKE '$d'");
if(mysql_num_rows($query) >0){
while($far=mysql_fetch_array($query)){
$level=$far[4];
$dept=$far[3];}
}
$result = mysql_query("SELECT * FROM `request` WHERE `req_for_dept` LIKE '$dept' AND `status` LIKE 'basic'");
 
echo " <h1><strong><center>Request/s</center></strong></h1>";

       ///****************** Print out lthe contents of each row into a table *************************///

echo "<body bgcolor='#cfad5f'><table width='90%' border='1' align='center' cellpadding='5' cellspacing='1' bgcolor='' class='text_brown' >";


echo "                 <tr align='center' >";
echo "                 <td ><h3>Request no</h3></td>";
echo "                 <td><h3>Item name</h3></td>";
echo "                 <td><h3>Request for Department</h3></td>";
echo "                 <td><h3>Quantity</h3></td>";
echo "                 <td><h3>Request Date</h3></td>";
echo "                 <td><h3>Status</h3></td>";
echo "                 <td><h3>Action</h3></td>";

$field = mysql_fetch_field($result);
      if($fields_num = mysql_num_fields($result)){
            for($i=0; $i<$fields_num; $i++){
              while($val=mysql_fetch_array($result))
              {   
		        echo       "<tr class=\"black\">";
                echo       " <td align='center' width='15%'>".$val[0]."</td>                ";
                echo       " <td align='center' width='20%'>".$val[4]."</td>                ";
                echo       " <td  align='center' width='25%'>".$val[2]."</td>                ";
				echo       " <td  align='center' width='25%'>".$val[6]."</td>                ";
				echo       " <td  align='center' width='20%'>".$val[8]."</td>                ";
                echo       " <td  align='center' width='20%'>".$val[10]."</td>                ";
				echo       "<td 'align='center' width='80%'>".('<form action="action.php?req_no=<?$val[req_no]?>" method="post">
				            <div   align="center"> <input name="submit" type="submit" value="Accept Request" />
					        <input name="submit" type="submit" value="Reject Request" /> 
					        <input name="hiddenValue" type="hidden" value="$val[0]"/>')."</td>";

								echo "</tr>";
			   }}
	echo"</table>";}
else
echo " <body bgcolor='#cfad5f'><h2><strong><center>No Request Found</center></strong></h2>";

?>
</body>
</html>
and action.php is..
<?php
session_start();
$d= $_SESSION['txt_id'];
include ('dbinfo.inc');



if (isset($_POST['Yes'])){

$sql =mysql_query("UPDATE TABLE `request` SET `status` = 'approved' WHERE 'req_no'='$_POST['req_no']");
//$result= mysql_query("UPDATE 'request' SET 'status' LIKE 'approved' WHERE 'req_no'= ''");


$_SESSION['txt_id'] = $_POST['txt_id'];
header ('Location:success.php');

}
else if (isset($_POST['No'])){
$_SESSION['txt_id'] = $_POST['txt_id'];
header ('Location:req_display.php');

}
?>

Please help I am stuck..
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Update record using a specific id..

Post by Jonah Bron »

What's the problem? I see an UPDATE query there... are you getting an error or something?
mani123
Forum Newbie
Posts: 8
Joined: Thu Mar 03, 2011 6:48 pm

Re: Update record using a specific id..

Post by mani123 »

there's no error but The records don't get updated.
and may be this is because the query is not recognising the request number,though I have added a hidden value in the form but I am not sure its working..
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Update record using a specific id..

Post by Darhazer »

Check the value of $_POST['req_no'] as well as the return value of mysql_query (if $sql is falce, the query have failed, you can check the reason by calling mysql_error)
mani123
Forum Newbie
Posts: 8
Joined: Thu Mar 03, 2011 6:48 pm

Re: Update record using a specific id..

Post by mani123 »

thanks for replying Darhazer.
checked it already,didn't find any error.
Is the procedure for assigning a hidden value is right??
I have a doubt in my mind about the hidden value procedure.

Code: Select all

echo       "<td 'align='center' width='80%'>".('<form action="action.php?req_no=<?$val[req_no]?>" method="post">
                                            <div   align="center"> <input name="submit" type="submit" value="Accept Request" />
                                                <input name="submit" type="submit" value="Reject Request" /> 
                                                <input name="hiddenValue" type="hidden" value="$val[0]"/>')."</td>";
and I am puting it in the query like

Code: Select all

if (isset($_POST['Yes'])){

$sql =mysql_query("UPDATE TABLE `request` SET `status` = 'approved' WHERE 'req_no'='$_POST['req_no']");}
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Update record using a specific id..

Post by Darhazer »

When I said check, I meant:

Code: Select all

var_dump($_POST['req_no']);
Actually you are passing it via $_GET, not via $_POST (as it's part of the form's action URI)
mani123
Forum Newbie
Posts: 8
Joined: Thu Mar 03, 2011 6:48 pm

Re: Update record using a specific id..

Post by mani123 »

Thanks its working..
If you don't mind I have another question.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Update record using a specific id..

Post by Darhazer »

I don't mind but it's best to open new topic. There is a common rule in the forums - one question, one topic - in this way it's easier to search for the answers, as well to answer question (because if someone post several questions and we start answering - each one to different question - it will be complete chaos)
mani123
Forum Newbie
Posts: 8
Joined: Thu Mar 03, 2011 6:48 pm

Re: Update record using a specific id..

Post by mani123 »

After re fixing my previous problem I have got another in the same code that is the loop once display the values but when I run it for the second time,it refuses to identify $dept, and this is driving me crazy.it was working well before.
Post Reply