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


Help with else if and database script





welshsteve
Hi everyone. I have the below script for displaying league tables on a site I'm building.

Code:

      <form method="post" action="s_tables.php">
         Please select a division:
         <select name="div" id="div">
            <option value="" selected="selected">Please select a division</option>
            <option value="sl_tab_dp">Premier Division</option>
            <option value="sl_tab_d1">Division 1</option>
            <option value="sl_tab_d2">Division 2</option>
            <option value="sl_tab_d3">Division 3</option>
         </select>
         <input type="submit" name="btnSubmit1" id="btnSubmit1" class="btn" value="View Table" />
      </form>

<?php
$div = $_REQUEST['div'] ;
$con = mysql_connect("SERVER","USER","PASS");
if (!$con)
  {
  die('Could not connect: ' . mysql_error() . '<br /><br />Please contact your web developer');
  }

mysql_select_db("DBNAME", $con);

$sql = "SELECT * FROM $div ORDER BY Pts DESC , Diff DESC , W DESC , D DESC , Pld , Team";
$result = mysql_query($sql);
if (!$result)
echo mysql_error();

if($div = 'sl_tab_dp')
{
  echo "<h2>Premier Division</h2>";
}
elseif($div = 'sl_tab_d1')
{
  echo "<h2>Division 1</h2>";
}
elseif($div = 'sl_tab_d2')
{
  echo "<h2>Division 2</h2>";
}
else
{
  echo "<h2>Division 3</h2>";
}

echo "<table style='padding:5px;border: 1px solid #000;border-collapse:collapse;'>
<tr>
<th style='background-color:#0a0;color:#fff;width:250px;border: 1px solid #006001;'>Team</th>
<th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>Pld</th>
<th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>W</th>
<th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>D</th>
<th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>L</th>
<th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>F</th>
<th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>A</th>
<th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>Diff</th>
<th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>Pts</th>
</tr>";
      
while($row = mysql_fetch_array($result))
  {
   echo "<tr>";
   echo "<td style='text-align:left;border: 1px solid #006001;'>&nbsp;" . $row['Team'] . "</td>";
   echo "<td style='text-align:center;border: 1px solid #006001;'>" . $row['Pld'] . "</td>";
   echo "<td style='text-align:center;border: 1px solid #006001;'>" . $row['W'] . "</td>";
   echo "<td style='text-align:center;border: 1px solid #006001;'>" . $row['D'] . "</td>";
   echo "<td style='text-align:center;border: 1px solid #006001;'>" . ($row['Pld'] - $row['W'] - $row['D']) . "</td>";
   echo "<td style='text-align:center;border: 1px solid #006001;'>" . $row['F'] . "</td>";
   echo "<td style='text-align:center;border: 1px solid #006001;'>" . $row['A'] . "</td>";
   echo "<td style='text-align:center;border: 1px solid #006001;'>" . ($row['F'] - $row['A']) . "</td>";
   echo "<td style='text-align:center;border: 1px solid #006001;'>" . ($row['W'] * 2 + $row['D']) . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysql_close($con);
?>


There are two problems.

1. When the page is first loaded, underneath the select box, the following text appears.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY Pts DESC , Diff DESC , W DESC , D DESC , Pld , Team' at line 1

I think this is because when the page first loads, the $div variable has no value, causing the SQL to fail. Is there a way in which I can just display some text instead if there is no value for the variable?

2. The If and Elseif bits are not working. In other words, regardless of the division I select, the code <h2>Premier Division</h2> is always rendered, but if the value of the variable is sl_tab_d1 for instance, it should render <h2>Division 1</h2> for instance.

Is anybody able to help at all? Many thanks in advance.
rvec
try this:
Code:

      <form method="post" action="s_tables.php">
         Please select a division:
         <select name="div" id="div">
            <option value="" selected="selected">Please select a division</option>
            <option value="sl_tab_dp">Premier Division</option>
            <option value="sl_tab_d1">Division 1</option>
            <option value="sl_tab_d2">Division 2</option>
            <option value="sl_tab_d3">Division 3</option>
         </select>
         <input type="submit" name="btnSubmit1" id="btnSubmit1" class="btn" value="View Table" />
      </form>

<?php
if (isset($_POST['div'])) {
  $con = mysql_connect("SERVER","USER","PASS")
    or die('Could not connect: ' . mysql_error() . '<br /><br />Please contact your web developer');

  mysql_select_db("DBNAME", $con);
 
  $div = mysql_real_escape($_POST['div']);
 
  $sql = "SELECT * FROM $div ORDER BY Pts DESC , Diff DESC , W DESC , D DESC , Pld , Team";
  $result = mysql_query($sql);
  if (!$result)
    echo mysql_error();


  switch ($_POST['div']) {
    case 'sl_tab_dp':
      echo "<h2>Premier Division</h2>";
    break;
    case 'sl_tab_d1':
      echo "<h2>Division 1</h2>";
    break;
    case 'sl_tab_d2':
      echo "<h2>Division 2</h2>";
    break;
    default:
      echo "<h2>Division 3</h2>";
    break;
  }

  echo "<table style='padding:5px;border: 1px solid #000;border-collapse:collapse;'>
  <tr>
  <th style='background-color:#0a0;color:#fff;width:250px;border: 1px solid #006001;'>Team</th>
  <th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>Pld</th>
  <th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>W</th>
  <th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>D</th>
  <th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>L</th>
  <th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>F</th>
  <th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>A</th>
  <th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>Diff</th>
  <th style='background-color:#0a0;color:#fff;width:35px;border: 1px solid #006001;'>Pts</th>
  </tr>";

  while($row = mysql_fetch_array($result))
    {
     echo "<tr>";
     echo "<td style='text-align:left;border: 1px solid #006001;'>&nbsp;" . $row['Team'] . "</td>";
     echo "<td style='text-align:center;border: 1px solid #006001;'>" . $row['Pld'] . "</td>";
     echo "<td style='text-align:center;border: 1px solid #006001;'>" . $row['W'] . "</td>";
     echo "<td style='text-align:center;border: 1px solid #006001;'>" . $row['D'] . "</td>";
     echo "<td style='text-align:center;border: 1px solid #006001;'>" . ($row['Pld'] - $row['W'] - $row['D']) . "</td>";
     echo "<td style='text-align:center;border: 1px solid #006001;'>" . $row['F'] . "</td>";
     echo "<td style='text-align:center;border: 1px solid #006001;'>" . $row['A'] . "</td>";
     echo "<td style='text-align:center;border: 1px solid #006001;'>" . ($row['F'] - $row['A']) . "</td>";
     echo "<td style='text-align:center;border: 1px solid #006001;'>" . ($row['W'] * 2 + $row['D']) . "</td>";
    echo "</tr>";
    }
  echo "</table>";
  mysql_close($con);
}
?>

I replaced the else/if/elseif/... with a swich, added mysql escaping and a big if to prevent the php from executing if no option is selected.

As for the if else part, know the difference between =, == and ===
welshsteve
Hi. Thanks for that. With that code I'm getting the following error.

Fatal error: Call to undefined function: mysql_real_escape()
rvec
oh that's supposed to be mysql_real_escape_string. My mistake.
welshsteve
Cheers
Related topics
Navigation Script (javascript) Help Please!
Need help with database, can anyone help?
Flash - Photo Gallery - need help please.
Need help with php script
Need help making logout script
Need help with PHP script[RESOLVED]
Need help with logger script. (half done)
need help with a script
need help with a script for vb its not to hard i think
Problem with perl script
Help! This small script has logical problems. Please help
Need some help on database settings
Need help on database settings
Need Help. Post - Mail script failure
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.