Jump to content

beginner- return query with user input


iulo0566

Recommended Posts

Hello.  I'm very new to php, and I'm running into the same problem over and over.  I'm trying to get info from a database based on a variable (user defined).  I've been testing this on Earthlink.net and also on inmotionhosting.com, but don't know what I'm missing.  Any help would be greatly appreciated.

 

Here is the search section of the code (not sure what other info you'll need to help me out):

if ($searchtype || $searchterm) {

$query = 'SELECT * FROM `rawmaterials` WHERE `$searchtype` LIKE `$searchterm` ';

 

$result = mysql_query($query) or DIE("Could not Execute Query on table $usertable. ");

echo '<table border="1">';

while ($row = mysql_fetch_array($result)) {

 

echo '<tr><td colspan="2"><strong>Trade Name: </strong>'.'<br />';

echo $row['trade_name'].'<br />';

        echo '<strong>Code #: </strong>'.$row['code_num'].'<br />';

      echo '<strong>INCI Name: </strong>'.'<br />';

        echo $row['INCI_name'].'<br />';

        echo '<strong>Usage Levels: </strong>'.$row['usage_levels'].'<br />';

        echo '<strong>Platform: </strong>'.$row['platform'].'<br />';

echo '</td>';

      echo '<td colspan="4"><strong>Description: </strong>'.'<br />';

      echo $row['description'].'<br />';

      echo '</td>';

      echo '</tr>';

}

      echo '</table>';

Link to comment
Share on other sites

first thing i notice is this:

      $query = 'SELECT * FROM `rawmaterials` WHERE `$searchtype` LIKE `$searchterm` ';

should be

      $query = "SELECT * FROM `rawmaterials` WHERE `$searchtype` LIKE '$searchterm'";

to make it syntactically correct...depending on what your logic is, it may need more changes

 

also, when debugging, use mysql_error() to see what is wrong:

      $result = mysql_query($query) or DIE("Could not Execute Query on table $usertable: ".mysql_error());

Link to comment
Share on other sites

also, when debugging, use mysql_error() to see what is wrong:

      $result = mysql_query($query) or DIE("Could not Execute Query on table $usertable: ".mysql_error());

 

It is very important that you only use 'or die()' during debugging never ever use it in a production environment your visitors don't know what: "Could not Execute Query on table SomeTable: Query was empty" means. Always display a nicer message telling them what happened and what they can do next.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.