Jump to content

Recommended Posts

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

yes, i know that. :P

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

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.