rooz999 Posted September 14, 2015 Share Posted September 14, 2015 I want to update data row But the problem is that it is updated All rowsthis is my code: $query = mysql_query("SELECT * FROM emp JOIN inv ON emp.name=inv.empname WHERE inv.empname='".$name."'") or die ("mysql error query"); $id = $_POST['id']; $updatestartdate = date('d/m/Y'); $updateenddate = date('d/m/Y'); $status = $_POST['status']; while ($rowshow = mysql_fetch_assoc($query)){ $timetoshow = unix_time($rowshow['timex']); //UPDATE if (isset($_POST['Update']) and $_POST['Update'] == 'dataupdate'){ $updatestatus = mysql_query ("UPDATE inv SET updatestartdate='$updatestartdate', updateenddate='$updateenddate', status='$status' WHERE id='".$rowshow['id']."'") or die ("updatestatus Error"); if (isset ($updatestatus)){ echo "<div class='hidecontent'><h3 style='background-color:#3F3F3F; padding:5px;' align='center'> <font color='#FFFFFF'>Update is done</font></h3><meta http-equiv='Refresh' content='5; url=cpanel_user.php' /></div>"; } else { echo "<div class='hidecontent'><h3 style='background-color:#FF0000; padding:5px;' align='center'> <font color='#FFFF00'>Update Error</font></h3></div>"; } } Where is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/298167-when-update-data-where-idrowid/ Share on other sites More sharing options...
cyberRobot Posted September 14, 2015 Share Posted September 14, 2015 Are you only looking to update the record which matches $id? If so, try changing this if (isset($_POST['Update']) and $_POST['Update'] == 'dataupdate'){ To this if (isset($_POST['Update']) and $_POST['Update'] == 'dataupdate' and $id==$rowshow['id']){ Quote Link to comment https://forums.phpfreaks.com/topic/298167-when-update-data-where-idrowid/#findComment-1520834 Share on other sites More sharing options...
cyberRobot Posted September 14, 2015 Share Posted September 14, 2015 (edited) Note that you could also consider running the query outside of the loop. Instead of using $rowshow, you probably could just use the GET variable that was assigned to $id. $updatestatus = mysql_query ("UPDATE inv SET updatestartdate='$updatestartdate', updateenddate='$updateenddate', status='$status' WHERE id='".$id."'") or die ("updatestatus Error"); Of course, you would want to verify that $id contains an expected value to avoid SQL injection attacks. Edited September 14, 2015 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/298167-when-update-data-where-idrowid/#findComment-1520837 Share on other sites More sharing options...
Psycho Posted September 14, 2015 Share Posted September 14, 2015 Note that you could also consider running the query outside of the loop. I was just going to suggest the same thing. The SELECT query and the loop are completely unnecessary for what is being accomplished here. Quote Link to comment https://forums.phpfreaks.com/topic/298167-when-update-data-where-idrowid/#findComment-1520842 Share on other sites More sharing options...
Barand Posted September 14, 2015 Share Posted September 14, 2015 Also, never store dates in d/m/y format - it is totally useless as a date storage format. Use DATE type column with format yyyy-mm-dd. Having done that you can then use CURDATE() to get the current date in your queries insted of generating in php first. Quote Link to comment https://forums.phpfreaks.com/topic/298167-when-update-data-where-idrowid/#findComment-1520847 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.