geekisthenewsexy Posted November 30, 2010 Share Posted November 30, 2010 hi guys, i'm trying to update 2 tables here and if possible in one go. but i'm kinda stuck 'cause only one table gets updated..please have a look at my code (i bet think there are errors in it).. i'm using php and mysql <?php include("dbconnection_wmsuipil.php"); $id = $_POST['id']; $sy = $_POST['sy']; $check=mysql_query("SELECT SchoolYear FROM tblsetsy WHERE SchoolYear='$sy'")or die (mysql_error()); while($row = mysql_fetch_array($check)) { } $setsy=$row['SchoolYear']; if($setsy==$sy) { $sql=mysql_query("UPDATE admin_sy SET school_year='$sy' WHERE id='$id'") or die(mysql_error()); $sql=mysql_query("UPDATE tblSetSY SET SchoolYear='$sy' WHERE id='$id'") or die(mysql_error()); } else{ $sql=mysql_query("UPDATE admin_sy SET school_year='$sy' WHERE id='$id'") or die(mysql_error()); } ?> ..hope you guys can help.please inform me if it's not clear.. Quote Link to comment https://forums.phpfreaks.com/topic/220210-how-to-update-2-tables-in-one-statement-please-help/ Share on other sites More sharing options...
requinix Posted November 30, 2010 Share Posted November 30, 2010 Your while loop is empty. Quote Link to comment https://forums.phpfreaks.com/topic/220210-how-to-update-2-tables-in-one-statement-please-help/#findComment-1141251 Share on other sites More sharing options...
geekisthenewsexy Posted November 30, 2010 Author Share Posted November 30, 2010 yes, i know that. i tried placing $setsy=$row['SchoolYear']; if($setsy==$sy) { $sql=mysql_query("UPDATE admin_sy SET school_year='$sy' WHERE id='$id'") or die(mysql_error()); $sql=mysql_query("UPDATE tblSetSY SET SchoolYear='$sy' WHERE id='$id'") or die(mysql_error()); } else{ $sql=mysql_query("UPDATE admin_sy SET school_year='$sy' WHERE id='$id'") or die(mysql_error()); } inside, but i won't get any updates..so there..i didn't have errors with it though.. Quote Link to comment https://forums.phpfreaks.com/topic/220210-how-to-update-2-tables-in-one-statement-please-help/#findComment-1141254 Share on other sites More sharing options...
requinix Posted November 30, 2010 Share Posted November 30, 2010 There's a fair bit of redundancy in there. After moving the stuff into the while loop, I can condense the whole thing down into this functionally-equivalent code: include("dbconnection_wmsuipil.php"); $id = $_POST["id"]; $sy = $_POST["sy"]; // * your query asks for SchoolYear but only when SchoolYear=$sy // so... why are you asking for it? $results = mysql_query("SELECT 1 FROM tblsetsy WHERE SchoolYear='$sy' LIMIT 1"); if (mysql_num_rows($results) > 0) { // * using a loop will only repeat the same actions over and over again // * comparing $row[schoolYear] with $sy is pointless because, according // to the query, SchoolYear=$sy mysql_query("UPDATE admin_sy SET school_year='$sy' WHERE id='$id'"); mysql_query("UPDATE tblSetSY SET SchoolYear='$sy' WHERE id='$id'"); } So does that make sense according to what you're trying to do? If there are any rows in tblsetsy with the matching SchoolYear=$sy, update the admin_sy and tblsetsy tables to use that same SchoolYear for any matching id=$id Quote Link to comment https://forums.phpfreaks.com/topic/220210-how-to-update-2-tables-in-one-statement-please-help/#findComment-1141267 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.