5 Replies - 408 Views - Last Post: 19 April 2013 - 11:49 AM Rate Topic: -----

#1 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

Search Form does not echo values from database to table

Posted 19 April 2013 - 07:20 AM

Hello I'm trying to get the values from my search form to echo into a dynamic table. For example what ever username the person search it's supposed to echo the what that person posted to the database. I know how to make the values echo but I can't figure out how to get them to echo inside a table for my html page. Could someone please help me out. Any help is appreciated.

Here's my code


<?php
mysql_connect ("localhost", "root","root")  or die (mysql_error());
mysql_select_db ("xuswapuser");
 
$term = $_POST['term'];
 
$sql = mysql_query("select * from Books where id like '%$term%'");

if (mysql_num_rows($sql) <= 0) {
// no results
echo 'User is not found in database. Please try again.';
} 

else {  
while ($row = mysql_fetch_array($sql)){
    echo 'Username: '.$row['id'];
    echo '<br/> Product: '.$row['Product'];
    echo '<br/> Condition: '.$row['Condition'];
    echo '<br/> Price: '.$row['Price'];
    echo '<br/><br/>';
    }

}
 
?> 

<html>
<head>
<title> 
XUSWAP 
</title>
<script type="text/javascript" src="sorttable.js"></script>
</head>
<body style="margin: 0; padding: 0;">
       <div id="header" style="background-color:#339900;height:157px;width:100%;position:relative;">                                      <!--header area-->
            <form id="headerForm" name="headerForm" method="Post" action="" align="right">
                  <input type="image" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSN3XIAe50cjq5Cf91GbAywPkChmI5HOAqYxgmRN8F5OhW7I_RT5Q" alt="Placeholder" align="left" width="150" height="150">
                  <br><br><br>
                  <input type="text" name="searchInput"id="search" style="width:500px;"/>
                  <input type="submit" name="searchButton" id="searchButton" value="Search" />
                  <input type="submit" name="logoutButton" id="logoutButton" value="Logout"/>
                  <br><br><br>
                  
            </form>
       </div> 
       <div id="background" style="background-color:#FFD700;height:100%; width:100%;"> 
 <div id = "pageContent"></div>
 
    
 <blockquote><p>Books </p>
  		</blockquote>
        
<div id="menu" style="background-color:#FFD700;height:620px;width:175px;position:absolute;top:200px;left:20px">
<br><b>Browse By Category:</b><br><br>
Books<br><br>
Calculators<br><br>
Clickers<br><br>
Goggles<br><br>
Lab Coats<br><br>
Tickets</div>




Is This A Good Question/Topic? 0
  • +

Replies To: Search Form does not echo values from database to table

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,329
  • Joined: 08-August 08

Re: Search Form does not echo values from database to table

Posted 19 April 2013 - 07:33 AM

Stop using deprecated mysql. Read up on prepared statements. Fix this major problem first!
Was This Post Helpful? 1
  • +
  • -

#3 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Search Form does not echo values from database to table

Posted 19 April 2013 - 07:38 AM

Read up on this. It'll teach you what you need to know about PDO. Trust me - once you've learned how to use it correct, database stuff will be TONS easier.
Was This Post Helpful? 1
  • +
  • -

#4 ChrisGulddahl  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 18
  • Joined: 17-April 13

Re: Search Form does not echo values from database to table

Posted 19 April 2013 - 10:35 AM

This is what I think you want.
Notice, that you can just plunk in some <?php (...) ?> tags anywhere in the document, so the php code that constructs the table has been moved into all the HTML.
Beware though, that this approach can give you very confusing php douments in time.
An alternative strategy would be to define a function at the top of the page (maybe include it from another file) that renders a table from the mysql result set. Then this function would be called in the middle of the html, but you could keep the query logic somewhere else.
Also as suggested by creativecoding and CTphpnwb you really should read up on prepared statements, as the old mysql extension comes with higher security risks and will simply stop working when it gets taken out of php.
<?php
mysql_connect ("localhost", "root","root")  or die (mysql_error());
mysql_select_db ("xuswapuser");
 
$term = $_POST['term'];
 
$sql = mysql_query("select * from Books where id like '%$term%'");
 
?> 

<html>
<head>
<title> 
XUSWAP 
</title>
<script type="text/javascript" src="sorttable.js"></script>
</head>
<body style="margin: 0; padding: 0;">
       <div id="header" style="background-color:#339900;height:157px;width:100%;position:relative;">                                      <!--header area-->
            <form id="headerForm" name="headerForm" method="Post" action="" align="right">
                  <input type="image" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSN3XIAe50cjq5Cf91GbAywPkChmI5HOAqYxgmRN8F5OhW7I_RT5Q" alt="Placeholder" align="left" width="150" height="150">
                  <br><br><br>
                  <input type="text" name="searchInput"id="search" style="width:500px;"/>
                  <input type="submit" name="searchButton" id="searchButton" value="Search" />
                  <input type="submit" name="logoutButton" id="logoutButton" value="Logout"/>
                  <br><br><br>
                  
            </form>
       </div> 
       <div id="background" style="background-color:#FFD700;height:100%; width:100%;"> 
 <div id = "pageContent">
	 <?php
	 if (mysql_num_rows($sql) <= 0) {
		// no results
		echo '<table><tr><td>User is not found in database. Please try again.</td></tr></table>';
	} 
	else {  
		echo '<table>'.
				'<tr><th>Username</th><th>Product</th><th>Condition</th><th>Price</th></tr>';
		while ($row = mysql_fetch_array($sql)){
			echo '<tr><td>'.$row['id'].'</td><td>'.$row['Product'].'</td><td>'.$row['Condition'].'</td><td>'.$row['Price'].'</td></tr>';
		}
		echo '</table>';
	}
	?>
 </div>
 
    
 <blockquote><p>Books </p>
  		</blockquote>
        
<div id="menu" style="background-color:#FFD700;height:620px;width:175px;position:absolute;top:200px;left:20px">
<br><b>Browse By Category:</b><br><br>
Books<br><br>
Calculators<br><br>
Clickers<br><br>
Goggles<br><br>
Lab Coats<br><br>
Tickets</div>




Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,329
  • Joined: 08-August 08

Re: Search Form does not echo values from database to table

Posted 19 April 2013 - 11:23 AM

Great. Reinforce bad coding habits!
:crazy:
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3479
  • View blog
  • Posts: 11,821
  • Joined: 12-December 12

Re: Search Form does not echo values from database to table

Posted 19 April 2013 - 11:49 AM

Forms no longer have a name attribute, nor do elements have an align attribute. All those BR tags are poor as well. Create and attach a CSS-stylesheet!

This in particular:

 <blockquote><p>Books </p></blockquote>

is.. unfortunate; it only requires a single element, and a little bit of touching-up with css.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1