Jump to content

php select problem


witham

Recommended Posts

I voted "help". No not that one, the other one.

if your db structure is similar to this, with integer id fields,
[pre]
manufacturer      product
-------------      ---------------
mfrID    <---+    prodID
mfrName      |    prodDesc
              +--- mfrID
[/pre]
then this will help.

|[url=http://members.aol.com/barryaandrew/baaselect/baaselectguide.html]baaSelect[/url]| generate js and php code for dynamic linked dropdowns

Or another approach is
|[url=http://members.aol.com/barryaandrew/xmlhttp/article.html]AJAX/xmlhttp[/url]| article and sample application
Link to comment
https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-56421
Share on other sites

Thank you very much for your reply

This is the code but I don't know what to add to the sqlPRODNAME to only show the data for that particular manufacturer.

// the sql query to select the data for displaying later

$sqlMANUFACTURERS = "SELECT MANID, MANNAME FROM man order by MANNAME";

$sqlPRODNAME = "SELECT manid, manno, manname,  prodno, proddesc from prodname, man where manno = manid order by proddesc;";

<form action="weld2.php" method="get">

<hr>
<tr><td colspan = 3><h3 align = "center"><i><b>PRODUCT DELETION</b></i></h3></td><tr>
<br>
<br>
<br>
<br
<tr><td colspan = 2><h2 align=center><i>This screen allows users to add products to W.E.L.D.</h2></td></tr>
<tr><td><b>Manufacturer:</b></td>
<?

//initiate the $result variable and populate it with the data from the $sqlMANUFACTURERS table

$result  = mysql_query($sqlMANUFACTURERS, $dblink)
or die("<p>Error Processing Query</p><hr /><p>".mysql_error()."</p>\n");


// mysql_fetch_array fetches the results from the query a row at a time each time it's called
// the result is returned as an array that can be referenced either by field name or by it's index
// this data is then put into a drop down box popualted by the updated manufacturers list

echo '<td><select name="Manufacturer">';
while ($row = mysql_fetch_array ($result))
// loop through the rows outputing them as html table rows

  {
echo '<option value="' . $row["MANID"] . '">' . $row["MANNAME"] . "</option>";
  }

echo '</select></td></tr>';

?>

<tr><td><b>Product Name:</b></td>

<?

//initiate the $result variable and populate it with the data from the $sqlPRODNAME table

$result  = mysql_query($sqlPRODNAME, $dblink)
or die("<p>Error Processing Query</p><hr /><p>".mysql_error()."</p>\n");


// mysql_fetch_array fetches the results from the query a row at a time each time it's called
// the result is returned as an array that can be referenced either by field name or by it's index
// this data is then put into a drop down box popualted by the updated manufacturers list

echo '<td><select name="Product Name">';
while ($row = mysql_fetch_array ($result))
// loop through the rows outputing them as html table rows


  {
echo '<option value="' . $row["prodno"] . '">' . $row["proddesc"] . "</option>";

  }
echo '</select></td></tr>';
?>
Link to comment
https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-56431
Share on other sites

If you use the baaSelect class, all you need is

[code]
<?php

include 'baaSelect.php';

$sel = new baaSelect();
$sel->addSelect('manufacturer', 'man', 'manid', 'manname', '', 1, '- manufacturer -');
$sel->addSelect('product', 'prodname', 'prodno', 'proddesc', 'manno', 1, '- product -');
?>

<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>baaSelect Sample</title>
<meta name="author" content="Barand">

<?php $sel->makeScript() ?>

</head>

<body>
    <form>
  <table>
  <tr>
      <td>
        Manufacturer
      </td>
      <td>
        <?php $sel->makeSelect('manufacturer'); ?>
      </td>
  </tr>
  <tr>
      <td>
        Product
      </td>
      <td>
        <?php $sel->makeSelect('product'); ?>
      </td>
  </tr>
</table>
<input type="submit" name="submit" value="Submit">
</form>
?>
</body>
</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-56452
Share on other sites

[quote author=Barand link=topic=100227.msg395416#msg395416 date=1152651455]
If you use the baaSelect class, all you need is

[code]
<?php

include 'baaSelect.php';

$sel = new baaSelect();
$sel->addSelect('manufacturer', 'man', 'manid', 'manname', '', 1, '- manufacturer -');
$sel->addSelect('product', 'prodname', 'prodno', 'proddesc', 'manno', 1, '- product -');
?>

<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>baaSelect Sample</title>
<meta name="author" content="Barand">

<?php $sel->makeScript() ?>

</head>

<body>
    <form>
  <table>
  <tr>
      <td>
        Manufacturer
      </td>
      <td>
        <?php $sel->makeSelect('manufacturer'); ?>
      </td>
  </tr>
  <tr>
      <td>
        Product
      </td>
      <td>
        <?php $sel->makeSelect('product'); ?>
      </td>
  </tr>
</table>
<input type="submit" name="submit" value="Submit">
</form>
?>
</body>
</html>[/code]
[/quote]
Link to comment
https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-57537
Share on other sites

thanks very much but being a newie I need a little more assistance, I copied and pasted the code you kindly sent but got an error message


Warning: main(baaSelect.php) [function.main]: failed to open stream: No such file or directory in C:\Program Files\xampp\htdocs\delprod.php on line 32

Warning: main() [function.include]: Failed opening 'baaSelect.php' for inclusion (include_path='.;C:\Program Files\xampp\php\pear\') in C:\Program Files\xampp\htdocs\delprod.php on line 32

Fatal error: Class 'baaSelect' not found in C:\Program Files\xampp\htdocs\delprod.php on line 34

The actual code as it stands currently is:

Could you please, please tell me where I am going wrong?


<?php

//prevents caching
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter();
session_start();

require('C:\Program Files\xampp\htdocs\config.php');

require('C:\Program Files\xampp\htdocs\functions.php');

//this is group name or username of the group or person that you wish to allow access to
// - please be advise that the Administrators Groups has access to all pages.
if (allow_access(Administrators) != "yes")
{
include ('C:\Program Files\xampp\htdocs\no_access.html');
exit;
}
?>




<?
$dbuser  = ''; // your database server user name
$dbhost  = 'localhost'; // name of the sql server
$dbpass  = ''; // your database server password
$dbname  = 'weld'; // the name of the database to connect to

include 'baaSelect.php';

$sel = new baaSelect();
$sel->addSelect('manufacturer', 'man', 'manid', 'manname', '', 1, '- manufacturer -');
$sel->addSelect('product', 'prodname', 'prodno', 'proddesc', 'manno', 1, '- product -');
?>

<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>baaSelect Sample</title>
<meta name="author" content="Barand">

<?php $sel->makeScript() ?>

</head>

<body>
    <form>
  <table>
  <tr>
      <td>
        Manufacturer
      </td>
      <td>
        <?php $sel->makeSelect('manufacturer'); ?>
      </td>
  </tr>
  <tr>
      <td>
        Product
      </td>
      <td>
        <?php $sel->makeSelect('product'); ?>
      </td>
  </tr>
</table>
<input type="submit" name="submit" value="Submit">
</form>
?>
</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-57541
Share on other sites

Barand another question please what does this mean?


Manufacturer
No Database Selected


Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\baaSelect.php on line 283
Product
No Database Selected


Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\baaSelect.php on line 283
?>
Link to comment
https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-58075
Share on other sites

After this code

[code]$dbuser  = '';      // your database server user name
$dbhost  = 'localhost';      // name of the sql server
$dbpass  = '';      // your database server password
$dbname  = 'weld';      // the name of the database to connect to[/code]

you need

[code]$connection = mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error());
mysql_select_db($dbname, $connection) or die (mysql_error());[/code]

The easiest thing to do is put all the above code in a file, say, "db.php" then at start of every script that uses the database

[code]include 'db.php';[/code]
Link to comment
https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-58082
Share on other sites

As a comparison for you, this is the same problem using ajax.

Save this code, which provides a selection of products for a particular manufacturer as "prod_select.php"
[code]<?php

  include 'db.php';
 
  $manid = $_GET['man'];
 
  $sql = "SELECT prodno, proddesc FROM prodname
  WHERE manno='$manid'
  ORDER BY proddesc";
  $res = mysql_query($sql) or die(mysql_error());
  echo "<select name='product'>";
  echo "<option value='0'>- select product -</option>";
  while (list($id, $desc) = mysql_fetch_row($res)) {
    echo "<option value='$id'> $desc</option>";
  }
  echo "</select>";
?>[/code]

Now save this main script as "ajax1.php" and run it.

[code]<?php
include 'db.php';

function manOptions($current) {
$sql = "SELECT manid, manname
FROM man
ORDER BY manname";
$res = mysql_query($sql) or die(mysql_error());
$str = '';
while (list($id, $name) = mysql_fetch_row($res)) {
$selected = $id==$current ? 'selected' : '';
$str .= "<option value='$id' $selected>$name</option>";
}
return $str;
}
?>
<html>
<head>
<title>AJAX sample</title>
<meta name="author" content="Barand">
<SCRIPT>
var xmlhttp=false;
try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
  xmlhttp = false;
  }
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}

function handleMessages (url, resultID) {
  var obj;
  xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
            obj = document.getElementById(resultID);
            obj.innerHTML = xmlhttp.responseText;
      }
  }
xmlhttp.send(0);
return 0;
}

function manChange(val) {
var url = "prod_select.php?man=" + val;
handleMessages (url, "prod");
}
</SCRIPT>
</head>
<body>
<form>
<table>
<tr>
  <td>
    Manufacturer
  </td>
  <td>
    <select name='manufacturer' onchange='manChange(this.value)'>
    <option value='0'>- select mfr -</option>
    <?php echo manOptions($man) ?>
    </select>
  </td>
</tr>
<tr>
  <td>
    Product
  </td>
  <td>
  <DIV id='prod'>
    <select name='product'>
    <option>- select product -</option>
    </select>
    <DIV>
  </td>
</tr>
</table>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/14321-php-select-problem/#findComment-58166
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.