ade2901 Posted July 17, 2011 Share Posted July 17, 2011 Hi guys, I am trying to get my head around how to only insert data from a table when the data is entered. If the text field is empty then I don't want the data inserting. I have a result tables which shows all fixtures and the results in them. There is also a section within it at the bottom to deduct points from teams. In some cases the League do not always get results from every team and sometimes they don't get results for weeks. The way my existing page owrksi s that it will only ever insert if the boxes are filled in. When they are all filled in it works a treat. So my issue is that when there are a number of resultless fixtures how can I get them ignored by the PHP so that it doesn't try to insert them? I have tried a if not null then carry out the calculations but it still seems to be erroring... Please see my screenshot of the page and the update form that the page is linked to below; <?php $host="localhost"; // Host name $username="USERNAME"; // Mysql username $password="PWDFORDB"; // Mysql password $db_name="lsl"; // Database name $div1Table = "division1"; // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $submit = $_POST['submit']; $fixtureID = $_POST['fixtureID']; $teamAScore = $_POST['teamAScore']; $teamBScore = $_POST['teamBScore']; $teamAID = $_POST['teamAID']; $teamBID = $_POST['teamBID']; $pd = $_POST['pd']; $teamID = $_POST['teamID']; $limit = count($fixtureID); $deleteQRY = mysql_query("DELETE from division1")or die(mysql_error()); $selectTeamIDName = mysql_query("INSERT INTO division1 (teamID, teamName) SELECT teamID, teamName FROM team")or die(mysql_error()); for($i=0;$i<$limit;$i++) { $fixtureID[$i] = mysql_real_escape_string($fixtureID[$i]); $teamAScore[$i] = mysql_real_escape_string($teamAScore[$i]); $teamBScore[$i] = mysql_real_escape_string($teamBScore[$i]); $teamAID [$i] = mysql_real_escape_string($teamAID[$i]); $teamBID [$i] = mysql_real_escape_string($teamBID[$i]); $teamID [$i] = mysql_real_escape_string($teamID[$i]); $pd[$i] = mysql_real_escape_string($pd[$i]); $query = "UPDATE fixture SET teamAScore=$teamAScore[$i], teamBScore=$teamBScore[$i] WHERE fixtureID = $fixtureID[$i]"; if(mysql_query($query)) echo "$i successfully updated results and league table.<br/><a href='div1Results.php'>Back to main page</a>"; //If fixtures update then update league table if ($teamAScore[$i] > $teamBScore[$i]) { //Team A update for team A win $teamAWin = "UPDATE division1 SET played=played+1, won=won+1, gf= gf+".$teamAScore[$i].", ga= ga+".$teamBScore[$i].", gd=gf-ga, pts= pts+3 WHERE teamID=$teamAID[$i]"; $resultTeamA = mysql_query($teamAWin) or die( "$teamAWin: " .mysql_error( ) ); //Team B update for teamB loss $teamBLoss = "UPDATE division1 SET played=played+1, lost=lost+1, gf=gf+".$teamBScore[$i].", ga=ga+".$teamAScore[$i].", gd=gf-ga WHERE teamID=$teamBID[$i]"; $resultTeamB = mysql_query($teamBLoss) or die( "$teamBLoss: " .mysql_error( ) ); } else if ($teamAScore[$i] < $teamBScore[$i]) { //Team A update for team A win $teamALoss = "UPDATE division1 SET played=played+1, lost=lost+1, gf=gf+".$teamAScore[$i].", ga=ga+".$teamBScore[$i].", gd=gf-ga WHERE teamID=$teamAID[$i]"; $resultTeamALoss = mysql_query($teamALoss) or die( "$teamAloss: " .mysql_error( ) ); //Team B update for teamB loss $teamBWin = "UPDATE division1 SET played=played+1, won =won+1, gf=gf+".$teamBScore[$i].", ga=ga+".$teamAScore[$i].", pts=pts+3, gd=gf-ga WHERE teamID=$teamBID[$i]"; $resultTeamBWin = mysql_query($teamBWin) or die( "$teamBWin: " .mysql_error( ) ); }else if ($teamAScore[$i] == $teamBScore[$i]) { //Team A update for team A win $teamADraw = "UPDATE division1 SET played=played+1, drawn=drawn+1, gf=gf+".$teamAScore[$i].", ga=ga+".$teamBScore[$i].", pts=pts+1, gd=gf-ga WHERE teamID=$teamAID[$i]"; $resultTeamADraw = mysql_query($teamADraw) or die( "$teamADraw: " .mysql_error( ) ); //Team B update for teamB loss $teamBDraw = "UPDATE division1 SET played=played+1, drawn=drawn+1, gf=gf+".$teamBScore[$i].", ga=ga+".$teamAScore[$i].", pts=pts+1, gd=gf-ga WHERE teamID=$teamBID[$i]"; $resultTeamBDraw = mysql_query($teamBDraw) or die( "$teamBDraw: sfsdsfsd " .mysql_error( ) ); } else echo "$i encountered an error.<br/>"; } echo "<br /><br /><br /><br />"; $deductionLimit = count ($teamID); for($i=0;$i<$deductionLimit;$i++) { $teamID [$i] = mysql_real_escape_string($teamID[$i]); $pd[$i] = mysql_real_escape_string($pd[$i]); $queryDeduct = "UPDATE division1 SET pd=$pd[$i] WHERE teamID = $teamID[$i]"; if (mysql_query($queryDeduct)) echo "$i successfully deducted points.<br/><a href='div1Results.php'>Back to main page</a>"; if ($pd[$i] !=NULL) { $teamPointDeduction= "UPDATE division1 SET pts=pts-".$pd[$i]." WHERE teamID = $teamID[$i]"; $teamPointDeductionRun = mysql_query($teamPointDeduction) or die( "$pd: " .mysql_error( ) ); } else echo "Cannot update"; } // close connection mysql_close(); ?> Many thanks in advance! Link to comment https://forums.phpfreaks.com/topic/242198-only-insert-data-where-fields-are-not-null/ Share on other sites More sharing options...
ade2901 Posted July 17, 2011 Author Share Posted July 17, 2011 Seems like I posted too early! I was making a stupid mistake, when using the empty function I didn't pass it the correct values. I encased all processing/calculations within the following; if(!empty($teamAScore[$i]) || !empty($teamBScore[$i])) //This line is how I solved the problem*********************** { if(mysql_query($query)) echo "$i successfully updated results and league table.<br/><a href='div1Results.php'>Back to main page</a>"; //If fixtures update then update league table if ($teamAScore[$i] > $teamBScore[$i]) { //Team A update for team A win $teamAWin = "UPDATE division1 SET played=played+1, won=won+1, gf= gf+".$teamAScore[$i].", ga= ga+".$teamBScore[$i].", gd=gf-ga, pts= pts+3 WHERE teamID=$teamAID[$i]"; $resultTeamA = mysql_query($teamAWin) or die( "$teamAWin: " .mysql_error( ) ); //Team B update for teamB loss $teamBLoss = "UPDATE division1 SET played=played+1, lost=lost+1, gf=gf+".$teamBScore[$i].", ga=ga+".$teamAScore[$i].", gd=gf-ga WHERE teamID=$teamBID[$i]"; $resultTeamB = mysql_query($teamBLoss) or die( "$teamBLoss: " .mysql_error( ) ); } else if ($teamAScore[$i] < $teamBScore[$i]) { //Team A update for team A win $teamALoss = "UPDATE division1 SET played=played+1, lost=lost+1, gf=gf+".$teamAScore[$i].", ga=ga+".$teamBScore[$i].", gd=gf-ga WHERE teamID=$teamAID[$i]"; $resultTeamALoss = mysql_query($teamALoss) or die( "$teamAloss: " .mysql_error( ) ); //Team B update for teamB loss $teamBWin = "UPDATE division1 SET played=played+1, won =won+1, gf=gf+".$teamBScore[$i].", ga=ga+".$teamAScore[$i].", pts=pts+3, gd=gf-ga WHERE teamID=$teamBID[$i]"; $resultTeamBWin = mysql_query($teamBWin) or die( "$teamBWin: " .mysql_error( ) ); }else if ($teamAScore[$i] == $teamBScore[$i]) { //Team A update for team A win $teamADraw = "UPDATE division1 SET played=played+1, drawn=drawn+1, gf=gf+".$teamAScore[$i].", ga=ga+".$teamBScore[$i].", pts=pts+1, gd=gf-ga WHERE teamID=$teamAID[$i]"; $resultTeamADraw = mysql_query($teamADraw) or die( "$teamADraw: " .mysql_error( ) ); //Team B update for teamB loss $teamBDraw = "UPDATE division1 SET played=played+1, drawn=drawn+1, gf=gf+".$teamBScore[$i].", ga=ga+".$teamAScore[$i].", pts=pts+1, gd=gf-ga WHERE teamID=$teamBID[$i]"; $resultTeamBDraw = mysql_query($teamBDraw) or die( "$teamBDraw: sfsdsfsd " .mysql_error( ) ); } else echo "$i encountered an error.<br/>"; } }//Closes off the IF empty if statement***************************** Link to comment https://forums.phpfreaks.com/topic/242198-only-insert-data-where-fields-are-not-null/#findComment-1243787 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.