Forum Moderators: coopster

Message Too Old, No Replies

Table update error?

Table update error?

         

shams

4:08 pm on May 26, 2007 (gmt 0)

10+ Year Member



hi,
Since 2 days i am trying to make php code to update the mysql table but i get the error these are the codes:
piupdate2.php is getting the data from pmti table this work ok:

<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.

henry0

5:29 pm on May 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,
Not sure that is the only error
however: in your querry update $address is missing its first quote

You may also to check your error/s do: $done=mysql_query($query); echo"DONE $done";

then paste in PHPmyAdmin the result
and read from the resulting message your error/s

phparion

7:15 pm on May 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you have forgotten a single quote around address variable. Also while development it is good practice to use

$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'";

shams

10:59 pm on May 26, 2007 (gmt 0)

10+ Year Member



hi,
Thanks so much for replies, these are the correction i made from the posts to the piupdate3.php code, but now i get the other error message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from='2006-05-26', to='2007-05-26', marks='3', totalm='3', average='10%', positi' at line 10

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.

phparion

7:40 am on May 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i dont see any problem with the query you should use mysql_error() to get more friendly error....

check all the column names are correct and matches with the table field names in your query especially for FROM field and remember it is case sensitive

eelixduppy

4:26 am on May 28, 2007 (gmt 0)



FROM is a reserved word [dev.mysql.com] and must be escaped properly within the query. Try something like this:

$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]

henry0

12:28 pm on May 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



AHHH.. why didn't I see that "FROM", maybe because knowing about those I do not use them,
thus not looking for that type of error :)

phparion

6:14 am on May 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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...

henry0

11:18 am on May 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since we are discusssing this
the best way around that little problem
is to test for your setting and use a little function that will do this or that upon settings

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;
}

?>