Forum Moderators: open

Message Too Old, No Replies

Change in mysql only if change in form were made

         

akademik

7:00 pm on Jul 27, 2007 (gmt 0)

10+ Year Member



Hello,

curently I have following script for database update:

for($i=0; $i<$_POST['all']; $i++){
$q = "UPDATE ".TBL_RESULTS." SET time = '".strtotime($_POST['time'.$i])."', team1 = '".$_POST['team1'.$i]."', score1 = '".$_POST['score1'.$i]."', score2 = '".$_POST['score2'.$i]."', team2 = '".$_POST['team2'.$i]."', timestamp = $time WHERE id = '".$_POST['which'.$i]."'";
mysql_query($q);
}

Is it possible to change only rows for which change ware made in form? I mean if I have 10 rows in edit script, but I made change only for one row, is it possible to change only that row? I need this because timestamp will be updated for all rows and that is not what i want. I select from that table against timestamp with limit 2, so I would like to select only two rows where changes ware made last.

Discovery

6:22 am on Jul 28, 2007 (gmt 0)

10+ Year Member



Problem :
for($i=0; $i<$_POST['all']; $i++){
$q = "UPDATE ".TBL_RESULTS." SET time = '".strtotime($_POST['time'.$i])."', team1 = '".$_POST['team1'.$i]."', score1 = '".$_POST['score1'.$i]."', score2 = '".$_POST['score2'.$i]."', team2 = '".$_POST['team2'.$i]."', timestamp = $time WHERE id = '".$_POST['which'.$i]."'";
mysql_query($q);
}

Hi,
What you can do,
1. Define variable 'status'.$i in scripting page and set default value = 0
2. If there is any update then set it to 'status'.$i = 1.
3. In For loop check if $_POST['status'.$i] = 1 then only update the value :
Sol :

for($i=0; $i<$_POST['all']; $i++){
if($_POST['status'.$i]==1)
{
$q = "UPDATE ".TBL_RESULTS." SET time = '".strtotime($_POST['time'.$i])."', team1 = '".$_POST['team1'.$i]."', score1 = '".$_POST['score1'.$i]."', score2 = '".$_POST['score2'.$i]."', team2 = '".$_POST['team2'.$i]."', timestamp = $time WHERE id = '".$_POST['which'.$i]."'";
mysql_query($q);
}
}

hope this will work.

[edited by: Discovery at 7:10 am (utc) on July 28, 2007]

akademik

6:52 pm on Jul 29, 2007 (gmt 0)

10+ Year Member



Thank you Discovery, your answer was very useful.