PHP Error When retriewing data from MySQL database

Discussion in 'Programming' started by JoshuaReen, Oct 3, 2012.

  1. #1
    I am learning PHP these days and have designed a test website. I'm now going to create a PHP script to display all database from MySQL database. I have created an HTML forum, with <select> tag in order to select one of three columns "First Name", "Last Name" and "Age". When I select all the three column in the from, and click the retrieve button, the code works well and displays all the database available in MySQL. But the problem occurs only, when I leave one or more than one select options. I have attached the screenshots which will clearly define what I want to say. Please provide me the solution for this.

    [​IMG]

    [​IMG]

    And below is the code for HTML form and PHP and MySQL.
    
    <?php
    include "header.php";
    if (isset($_POST['submit']))
    {
    // connect database
    $con = mysql_connect("mysql5.000webhost.com", "xxxxx", "xxxxx");
    // select database and create table
    mysql_select_db("a8995753_db1", $con);
    if(!empty($_POST['col1']))  // tutorial for this is here:  http://www.webdesignerforum.co.uk/topic/36142-help-with-html-select-drop-down-and-phpmysql/
      {
      $col1=$_POST['col1'];
      }
    else
      {
      $col1=NULL;
      }
    
    if(!empty($_POST['col2']))
      {
      $col2=$_POST['col2'];
      }
    else
      {
      $col2=NULL;
      }
    
    if(!empty($_POST['col3']))
      {
      $col3=$_POST['col3'];
      }
    else
      {
      $col3=NULL;
      }
    
    $result = mysql_query("SELECT $col1, $col2, $col3 FROM Persons");
    ?>
    <table border='1' border-color='#000000'>
    <tr>
    <?php
    if (isset($col1))
    {
    echo "<th>First Name</th>";
    }
    if (isset($col2))
    {
    echo "<th>Last Name</th>";
    }
    if
    (isset($col3))
    {
    echo "<th>Age</th>";
    }
    ?>
    </tr>
    <?php
    while($row = mysql_fetch_array($result))
      {
    ?>
      <tr><td> <?php  echo $row['FirstName'] ?>  </td> <td> <?php echo $row['LastName'] ?> </td>  <td> <?php echo $row['Age'] ?> </td></tr>
      <?php
      echo "<br />";
      }
    mysql_close($con);
    
    }
    else
    {
    ?> 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <!-- radio buttons names should be the same as column names in the database. -->
    <select name = "col1">
    <option></option>
    <option>FirstName</option>
    <option>LastName</option>
    <option>Age</option>
    </select>
    
    <select name = "col2">
    <option></option>
    <option>FirstName</option>
    <option>LastName</option>
    <option>Age</option>
    </select>
    
    <select name = "col3">
    <option></option>
    <option>FirstName</option>
    <option>LastName</option>
    <option>Age</option>
    <input type="submit" name="submit" value="Retrieve/Update Data"></input>
    </select></form>
    </body>
    </html>
    <?php
    }
    ?>
    
    Code (markup):

     
    JoshuaReen, Oct 3, 2012 IP
  2. Web Solutions

    Web Solutions Peon

    Messages:
    64
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    0
    #2
    $result = mysql_query("SELECT $col1, $col2, $col3 FROM Persons") or die(mysql_error());
    PHP:
    Any changes ?
     
    Web Solutions, Oct 3, 2012 IP
  3. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #3
    I think your query is wrong. Try this.

    
    if(!empty($_POST['col1']))  // tutorial for this is here:  http://www.webdesignerforum.co.uk/topic/36142-help-with-html-select-drop-down-and-phpmysql/
      {
      $col1=$_POST['col1'].',';
      }
    else
      {
      $col1='';
      }
    
    if(!empty($_POST['col2']))
      {
      $col2=$_POST['col2'].',';
      }
    else
      {
      $col2='';
      }
    
    if(!empty($_POST['col3']))
      {
      $col3=$_POST['col3'];
      }
    else
      {
      $col3='';
      }
    
    $result = mysql_query("SELECT $col1, $col2, $col3 FROM Persons");
    
    PHP:
    You need to debug your code so you can see errors. You can debug it simply by doing this.

    
    $result = mysql_query("SELECT $col1, $col2, $col3 FROM Persons");
    echo mysql_error();
    
    PHP:
     
    plussy, Oct 4, 2012 IP
  4. JoshuaReen

    JoshuaReen Greenhorn

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #4
    Thanks for taking interest. I have tried you code by using single quotes instead of NULL, but still it is showing the same error message. :-(
     
    JoshuaReen, Oct 8, 2012 IP
  5. xuled

    xuled Banned

    Messages:
    286
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    There must be something going wrong in your query system. Try to make some other queries.
     
    xuled, Oct 8, 2012 IP