Jump to content

HTML Form that pulls MYSQL DB with PHP


fporter

Recommended Posts

Trying to figure out how to approach this.  I have a form that uses php to pull from a mysql database.  I can get the text submission form to work but I am having trouble with a drop down menu that pulls directly from the DB and submitting what is selected as the basis of the result query.  I am including the two sets of code. one for my search.php and the other for my search_result.php.

 

search.php

<?php require_once('Connections/search_db.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_search_db, $search_db);

// Search engine functionality
if ($_GET['title'] || $_GET['country2']) {

$ti = $_GET['title'];
$count = $_GET['country2'];

$query_rsLinks = "SELECT * FROM tbl_links WHERE (tbl_links.linkemail LIKE '%$ti%' OR tbl_links.linktitle LIKE '%$ti%' OR tbl_links.linkurl LIKE '%$ti%') AND tbl_links.linkcat LIKE '$count' ORDER BY tbl_links.linkdate DESC";
} else {

$query_rsCountries = "SELECT search_db.tb_name FROM search_db";

}

$rsCountries = mysql_query($query_rsCountries, $search_db) or die(mysql_error());
$row_rsCountries = mysql_fetch_assoc($rsCountries);
$totalRows_rsCountries = mysql_num_rows($rsCountries);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body><form action="search_result.php" method="get" name="fmsearch">
  <table width="200" border="1">
    <tr>
      <td>search</td>
      <td><label for="title"></label>
      <input name="title" type="text" id="title" /></td>
       
      <td><input name="Search" type="button" id="Submit" value="Submit" />
       </td>
    </tr>
  </table>
</form>
<br>


<!-- action="search_result.php" -->
<form id="forminsert" name="fmsearch" action="search_result.php" method="get">
<table border="0" id="tblinsert">
  <caption>
    search for country
  </caption>
  <tr>
    <th><label for="title">search for:</label></td>
    <th>
      <input type="text" name="title" id="title" 
  <?php if (isset($_GET['title'])) {
	  echo 'value="' .$GET['title']. '"'; } ?> />
          </td>
           <td><input type="submit"  id="button" value="Submit" /></td>
  </tr>
</table>
    
   <form id="forminsert" name="fmsearch" action="search_result.php" method="get">
<table border="0" id="tblinsert2">
  <caption>
    search for country
  </caption>
  <tr>      
  
    
          
    <td><select name="country2" id="country2">
    <option value="">All Countries</option>>
      <?php
do {  
?>
      <option value="<?php echo $row_rsCountries['tb_name']?>">

   
  
  <?php echo $row_rsCountries['tb_name']?></option>
      <?php
} while ($row_rsCountries = mysql_fetch_assoc($rsCountries));
  $rows = mysql_num_rows($rsCountries);
  if($rows > 0) {
      mysql_data_seek($rsCountries, 0);
  $row_rsCountries = mysql_fetch_assoc($rsCountries);
  }
?>
    </select></td>
    <td><input type="submit"  id="button" value="Submit" /></td>
  </tr>
</table>

</form>
</body>
</html>
<?php
mysql_free_result($rsCountries);
?>

 

search_result.php

<?php require_once('Connections/search_db.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_rs83 = "-1";
if (isset($_GET['country2'])) {
  $colname_rs83 = $_GET['country2'];
}
mysql_select_db($database_search_db, $search_db);
$query_rs83 = sprintf("SELECT * FROM search_db WHERE tb_name LIKE %s", GetSQLValueString("%" . $colname_rs83 . "%", "text"),GetSQLValueString("%" . $colname_rs83 . "%", "text"));
$rs83 = mysql_query($query_rs83, $search_db) or die(mysql_error());
$row_rs83 = mysql_fetch_assoc($rs83);
$colname_rs83 = "-1";


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php echo $row_rs83['']; ?>
<?php do { ?>
  <table width="300" border="1">
    <tr>
      <td rowspan="2"><img src="<?php echo $row_rs83['tb_Img']; ?>" alt="image" /></td>
      <td><?php echo $row_rs83['tb_name']; ?></td>
    </tr>
    <tr>
      <td><?php echo $row_rs83['tb_desc']; ?></td>
    </tr>
  </table>
  <?php } while ($row_rs83 = mysql_fetch_assoc($rs83)); ?>
  <a href="http://barium.worldteam.org/phptesting/search.php">Back to Search</a>
</body>
</html>
<?php
mysql_free_result($rs83);
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/229259-html-form-that-pulls-mysql-db-with-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.