Forum Moderators: coopster
<html>
<head><TITLE>Update Pmti</TITLE></head>
<body>
<?php
include 'library/config.php';
include 'library/opendb.php';
$id=$_POST['id'];
$name=$_POST['name'];
$fatherN=$_POST['fatherN'];
$query=" SELECT * FROM pmti WHERE id='$id' or name='$name' and fathern='$fatherN'";
$result=mysql_query($query) or die(mysql_error());
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$course=mysql_result($result,$i,"course");
$batchs=mysql_result($result,$i,"batchs");
$session=mysql_result($result,$i,"session");
$batch=mysql_result($result,$i,"batch");
$name=mysql_result($result,$i,"name");
$fathern=mysql_result($result,$i,"fathern");
$address=mysql_result($result,$i,"address");
$from=mysql_result($result,$i,"from");
$to=mysql_result($result,$i,"to");
$marks=mysql_result($result,$i,"marks");
$totalm=mysql_result($result,$i,"totalm");
$average=mysql_result($result,$i,"average");
$position=mysql_result($result,$i,"position");
$remark=mysql_result($result,$i,"remark");
?>
<form action="piupdate3.php" method="post">
<input type="hidden" name="id" value="<? echo $id;?>">
<b>
Course:<input type="text" name="ud_course" value="<? echo $course;?>"><br>
Batch Serial: <input type="text" name="ud_batchs" value="<? echo $batchs;?>"><br>
Session: <input type="text" name="ud_session" value="<? echo $session;?>"><br>
Batch: <input type="text" name="ud_batch" value="<? echo $batch;?>"><br>
Name: <input type="text" name="ud_name" value="<? echo $name;?>"><br>
Father Name: <input type="text" name="ud_fathern" value="<? echo $fathern;?>"><br>
Address: <input type="text" name="ud_address" value="<? echo $address;?>"><br>
Date Start: <input type="text" name="ud_from" value="<? echo $from;?>"><br>
</div>
<div id="main-right">
Date Complete: <input type="text" name="ud_to" value="<? echo $to;?>"><br>
Marks: <input type="text" name="ud_marks" value="<? echo $marks;?>"><br>
Total Marks: <input type="text" name="ud_totalm" value="<? echo $totalm;?>"><br>
Average: <input type="text" name="ud_average" value="<? echo $average;?>"><br>
Position: <input type="text" name="ud_position" value="<? echo $position;?>"><br>
Remarks: <input type="text" name="ud_remark" value="<? echo $remark;?>"><br>
<br>
<input type="Submit" value="Update">
<input type="Reset">
<input type="button" value="Cancel" onCLICK="window.location='pimain.php'">
</div>
</form>
</div>
</div>
</div>
<?php
++$i;
}
include 'library/closedb.php';
?>
</b>
</body>
</html>
piupdate3.php getting the values from piupdate2.php and updaing the table:
<?php
include 'library/config.php';
include 'library/opendb.php';
$id=$_POST['id'];
$ud_course=$_POST['ud_course'];
$ud_batchs=$_POST['ud_batchs'];
$ud_session=$_POST['ud_session'];
$ud_batch=$_POST['ud_batch'];
$ud_name=$_POST['ud_name'];
$ud_fathern=$_POST['ud_fathern'];
$ud_address=$_POST['ud_address'];
$ud_from=$_POST['ud_from'];
$ud_to=$_POST['ud_to'];
$ud_marks=$_POST['ud_marks'];
$ud_totalm=$_POST['ud_totalm'];
$ud_average=$_POST['ud_average'];
$ud_position=$_POST['ud_position'];
$ud_remark=$_POST['ud_remark'];
$query="UPDATE pmti SET course='$ud_course', batchs='$ud_batchs', session='$ud_session', batch='$ud_batch', name='$ud_name', fathern='$ud_fathern', address=$ud_address', from='$ud_from', to='$ud_to', marks='$ud_marks', totalm='$ud_totalm', average='$ud_average', position='$ud_position', remark='$ud_remark' WHERE id='$id'";
$done=mysql_query($query);
if($done) { echo "Welcome Records Successfuly Updated, do want to update othe one:<br /><br />";
echo "<input type=\"button\" ";
echo "value=\"OK\" ";
echo "onClick=\"location.href='piupdate.php'\">";
echo "<input type=\"button\" ";
echo "value=\"Cancel\" ";
echo "onClick=\"location.href='pimain.php'\">";
}
else { echo "sorry try again";}
include 'library/closedb.php';
?>
i get the ouput:
sorry try again
these are the mysql query to create table for test:
CREATE TABLE `pmti` (
`id` mediumint(9) NOT NULL auto_increment,
`course` varchar(4) NOT NULL,
`batchs` varchar(5) NOT NULL,
`session` varchar(2) NOT NULL,
`batch` varchar(2) NOT NULL,
`name` varchar(30) NOT NULL,
`fathern` varchar(30) NOT NULL,
`address` varchar(10) NOT NULL,
`from` date NOT NULL,
`to` date NOT NULL,
`marks` tinyint(4) NOT NULL,
`totalm` tinyint(4) NOT NULL,
`average` varchar(6) NOT NULL,
`position` varchar(4) NOT NULL,
`remark` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1";
this is for insert data:
INSERT INTO `pmti` (
`id` ,
`course` ,
`batchs` ,
`session` ,
`batch` ,
`name` ,
`fathern` ,
`address` ,
`from` ,
`to` ,
`marks` ,
`totalm` ,
`average` ,
`position` ,
`remark`
)
VALUES (
NULL , '3', '4', '5', '6', 'a', 'b', 'c', 'd', '2006-05-26', '2007-05-26', '10', '10%', '1', 'ok'
);
thanks for your help in advanced.
$done=mysql_query($query) or die(mysql_error());
and by printing your query gives you a very clear idea of what query shape you have to apply to the database and you can kick out many mistakes easily.
For good programming practice I will suggest you to divide your query into multiple lines for the ease of readibility with
$sql = 'some';
$sql .= 'more';
e.g
$query="UPDATE pmti
SET
course='$ud_course',
batchs='$ud_batchs',
session='$ud_session',
batch='$ud_batch',
name='$ud_name',
fathern='$ud_fathern',
address='$ud_address',
from='$ud_from',
to='$ud_to',
marks='$ud_marks',
totalm='$ud_totalm',
average='$ud_average',
position='$ud_position',
remark='$ud_remark'
WHERE
id='$id'";
this is the piupdate3.php code:
<?php
include 'library/config.php';
include 'library/opendb.php';
$id=$_POST['id'];
$ud_course=$_POST['ud_course'];
$ud_batchs=$_POST['ud_batchs'];
$ud_session=$_POST['ud_session'];
$ud_batch=$_POST['ud_batch'];
$ud_name=$_POST['ud_name'];
$ud_fathern=$_POST['ud_fathern'];
$ud_address=$_POST['ud_address'];
$ud_from=$_POST['ud_from'];
$ud_to=$_POST['ud_to'];
$ud_marks=$_POST['ud_marks'];
$ud_totalm=$_POST['ud_totalm'];
$ud_average=$_POST['ud_average'];
$ud_position=$_POST['ud_position'];
$ud_remark=$_POST['ud_remark'];
$query="UPDATE pmti
SET
course='$ud_course',
batchs='$ud_batchs',
session='$ud_session',
batch='$ud_batch',
name='$ud_name',
fathern='$ud_fathern',
address='$ud_address',
from='$ud_from',
to='$ud_to',
marks='$ud_marks',
totalm='$ud_totalm',
average='$ud_average',
position='$ud_position',
remark='$ud_remark'
WHERE
id='$id'";
$done=mysql_query($query) or die(mysql_error());
if($done) { echo "Welcome Records Successfuly Updated, do want to update othe one:<br /><br />";
echo "<input type=\"button\" ";
echo "value=\"OK\" ";
echo "onClick=\"location.href='piupdate.php'\">";
echo "<input type=\"button\" ";
echo "value=\"OK\" ";
echo "onClick=\"location.href='piupdate.php'\">";
echo "<input type=\"button\" ";
echo "value=\"Cancel\" ";
echo "onClick=\"location.href='pimain.php'\">";
}
else { echo "sorry try again";}
include 'library/closedb.php';
?>
please help to solve the problem.
$query="UPDATE pmti
SET
`course`='$ud_course',
`batchs`='$ud_batchs',
`session`='$ud_session',
`batch`='$ud_batch',
`name`='$ud_name',
`fathern`='$ud_fathern',
`address`='$ud_address',
`from`='$ud_from',
`to`='$ud_to',
`marks`='$ud_marks',
`totalm`='$ud_totalm',
`average`='$ud_average',
`position`='$ud_position',
`remark`='$ud_remark'
WHERE
`id`='$id'";
Also, don't forget to escape those query variables! - mysql_real_escape_string [php.net]
Also, don't forget to escape those query variables! - mysql_real_escape_string
unless your magic quotes are on. because if it is on then it will automatically add slashes and if you are using mysql_real_escape_string with magic quotes ON then wrong values will be stored in the table e.g
I'm will be escaped as I\\'m and your database will store I\'m instead of I'm .... if you know what I mean...
How's your get_magic_quotes_gpc?
function CleanDb($value)
{
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
else
if (!is_numeric($value))
{
$value = "'" . mysql_real_escape_string($value) . "'";
}
// if not a number or a numeric string
else
{
$value = "'" . mysql_real_escape_string($value) . "'";
}
if (is_array($value))
{
$value = '"'.mysql_escape_string(serialize($var)).'"';
}
if (is_object($value))
{
$value = '"'.mysql_escape_string(serialize($var)).'"';
}
return $value;
}
?>