FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Prevent mysql error showing when code needs user input





welshsteve
Hi everyone, Basically, I have a page which has php script to display all records based on a selection the user needs to make from a form on the same page. But when the page first loads, the variable $week has no value, causing the page to display an error. How can I tell the below script to simply display,

Code:
echo "No week selected";


until the user selects a week?

Here is my php code.

Code:

<?php
$week = $_REQUEST['selectweek'] ;
$con = mysql_connect("SERVER","USER","PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error() . '<br /><br />Please contact the website administrator');
  }

mysql_select_db("DATABASE", $con);

$sql = "SELECT * FROM Results_Prem WHERE Week = ".$week. ";";
//echo $sql;
$result = mysql_query($sql);
if (!$result)
//echo mysql_error();
echo "No week selected";

echo "<table class='fixres'>";
echo "<tr><th colspan='6'>WEEK " . $week . "</th></tr>";

while($row = mysql_fetch_array($result))
  {
      echo "<tr><td class='fix'>" . $row["HomeTeam"] . "<td class='fixv'>" . $row["HomeScore"] . "</td><td class='fixv'>-</td><td class='fixv'>" . $row["AwayScore"] . "</td><td class='fix'>" . $row["AwayTeam"] . "</td><td class='fixv'><a href='javascript:toggleLayer(\"week" . $row["Week"] . "-" . $row["MatchNo"] ."\");'><img src='images/info.gif' style='border:0;' alt='Info' /></a></td></tr>";
      echo "<tr><td colspan='6'><table id='week" .$row["Week"]. "-" .$row["MatchNo"]. "' style='margin-left:5px;display:none;'>";
      echo "<tr><td class='scores'>" . $row["hp1"] . "</td><td class='scoresv'>" . $row["hs1"] . "</td><td class='scoresv'>-</td><td class='scoresv'>" . $row["as1"] . "</td><td class='scores'>" . $row["ap1"] . "</td><td class='scoresv'>&nbsp;</td></tr>";
      echo "<tr><td class='scores'>" . $row["hp2"] . "</td><td class='scoresv'>" . $row["hs2"] . "</td><td class='scoresv'>-</td><td class='scoresv'>" . $row["as2"] . "</td><td class='scores'>" . $row["ap2"] . "</td><td class='scoresv'>&nbsp;</td></tr>";
      echo "<tr><td class='scores'>" . $row["hp3"] . "</td><td class='scoresv'>" . $row["hs3"] . "</td><td class='scoresv'>-</td><td class='scoresv'>" . $row["as3"] . "</td><td class='scores'>" . $row["ap3"] . "</td><td class='scoresv'>&nbsp;</td></tr>";
      echo "<tr><td class='scores'>" . $row["hp4"] . "</td><td class='scoresv'>" . $row["hs4"] . "</td><td class='scoresv'>-</td><td class='scoresv'>" . $row["as4"] . "</td><td class='scores'>" . $row["ap4"] . "</td><td class='scoresv'>&nbsp;</td></tr>";
      echo "<tr><td class='scores'>" . $row["hp5"] . "</td><td class='scoresv'>" . $row["hs5"] . "</td><td class='scoresv'>-</td><td class='scoresv'>" . $row["as5"] . "</td><td class='scores'>" . $row["ap5"] . "</td><td class='scoresv'>&nbsp;</td></tr>";
      echo "<tr><td class='scores'>" . $row["hp6"] . "</td><td class='scoresv'>" . $row["hs6"] . "</td><td class='scoresv'>-</td><td class='scoresv'>" . $row["as6"] . "</td><td class='scores'>" . $row["ap6"] . "</td><td class='scoresv'>&nbsp;</td></tr>";
      echo "<tr><td class='scores'>" . $row["hp7"] . "</td><td class='scoresv'>" . $row["hs7"] . "</td><td class='scoresv'>-</td><td class='scoresv'>" . $row["as7"] . "</td><td class='scores'>" . $row["ap7"] . "</td><td class='scoresv'>&nbsp;</td></tr>";
      echo "<tr><td colspan='6'><hr /></td></tr>";
      echo "<tr><td class='extrainfo' colspan='6'>&nbsp;<b>30+ Breaks:</b><br />" . $row["Breaks"] . "<br /><br /></td></tr></table></td></tr>";
  }
echo "</table>";

mysql_close($con);
?>


Many thanks in advance.
jmraker
You can avoid running the SQL

Code:

$result = false;
if(isset($_REQUEST['selectweek'])){
  $sql = "SELECT * FROM Results_Prem WHERE Week = ".$week. ";";
  $result = mysql_query($sql);
}
if (!$result){
  echo 'No records were found';
}
else{
  ... table ...
}
welshsteve
Was just adding to my post when I saw you posted a reply.

I got around it by doing this.

Just added

Code:

if (!empty($week)) {
//SQL Query code
//Display data code
} else echo "No week selected";


Many thanks
Related topics
Mysql error!
[mySQL] Error I cannot Debug
MySql error: Column count doesn't match value count at row 1
MySQL Error#: 1130 - help
Getting Hebrew User Input...
MySQL error
Error connecting to mysql ; please help
150 frih to make this SIMPLE script
Mysql error: Warning: mysql_num_rows():
Login System Tutorial Part 1 Creating Registration
MySQL error
MYSQL Ajax im Installation Error
escapeshellcmd seems to blank my user input
MySQL Error in code but works in phpMyAdmin
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.