Forum Moderators: open
this is my sql tables.
CREATE TABLE `mandal_page` (
`id` int(4) unsigned NOT NULL auto_increment,
`name` varchar(100) default NULL,
`time` varchar(60) default NULL,
`offphone` int(10) default NULL,
`category` varchar(50) NOT NULL default 'Select',
`offname` varchar(150) default NULL,
`count` varchar(4) NOT NULL default '1',
`public` char(3) NOT NULL default 'no',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
CREATE TABLE `mandal_category` (
`id` int(4) unsigned NOT NULL auto_increment,
`name` varchar(50) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;
how can i get data from the chosen list.
sorry for my english. Help me any one to me.
THANK YOU IN ADVANCE
First thing I would do is change this
`category` varchar(50) NOT NULL default 'Select',
to this
`category` int(6) NOT NULL default 0,
Reason being, if you store category as textual values, and decide to change the title of one of your category names, you'd have to change all instances in the other table as well. By just storing the category ID, this becomes automatic. Second, use numeric data types wherever you can, it makes things run much faster.
With that in mind, basically you start like this:
$select = "select * from mandal_category order by name asc";
Which gives you an array of two items.
$selectList = '
<select name="category" id="category">
<option value="">select</option>';
$result = mysql_query($select);
while ($row=mysql_fetchrow_array($result)) {
$selectList .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
}
$selectList .= '</select>';
You now use $selectList in your form. On submit, you should now have the category id, not the name, in post/get/request.
echo $_POST['category']; // a number, the id, like 5 or 7
So from there,
// a little error check, make sure it's a number
if (! ($_POST['category'] > 0)) {
die("Oops, category is invalid");
exit;
}
$query = "select * from mandal_page where category=" . $_POST['category'] . " order by name asc";
Should give you all the items in the selected category. Note I DID NOT quote ' ' the value in the query, which doesn't hurt anything. Quoting a numeric query is not necessary. If $_POST[category'] is null, it will error, which is good - tells you something is wrong in your programming.
<snip>
once again thank you rocknbil.
[edited by: bill at 11:12 am (utc) on Dec. 27, 2009]
[edit reason] No links to your example sites please [/edit]
<?php
mysql_connect('localhost','user','pass');
mysql_select_db('db');
?>
<? if($error!=""){ ?>
<br>
<table width="75%" border="1" cellpadding="5" cellspacing="0" bordercolor="#000099" bgcolor="#E1FFFF">
<tr>
<td align="center" class="text"><strong><?=$error?></strong></td>
</tr>
</table>
<br>
<? } ?>
<FORM method="GET" ENCTYPE="multipart/form-data" name="uploadform" id="uploadform">
<br> <h1 align="left">View mandals</h1>
<hr color="#666666"> <br>
<table width="95%" height="40" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000099" bgcolor="#FFFFFF">
<tr bgcolor="#17A8FF">
<td bgcolor="#E1FFFF" class="text1"> <b>mandal Categories</b>
<select name='category' id='category' onChange="submit()">
<option value="">Select A Mandal Here</option>
<?
$result=mysql_query("select * from mandal_category");
while($myrow = mysql_fetch_array($result)){
print "<option value='".$myrow['id']."'>".$myrow['name']."</option>";
}
?>
</select> </td>
</tr>
</table>
</FORM>
<?php
if($id==""){
$query1 ="SELECT * FROM mandal_page WHERE id = '$category' && public = 'yes'";
}
else{
$query1 ="SELECT * FROM mandal_page WHERE id = '$category' && public = 'yes'";
}
$results=mysql_query($query1) or die(mysql_error());
echo "<table align='center' bgcolor='#hhhhfff' class='reference' width='60%' border='1' cellpadding='0' cellspacing='0' >
<tr bgcolor='#ffffff'>
<th>id</th>
<th>Office Name</th>
<th>offphone No.</th>
<th>Updated On</th>
<th>Updated By</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'>" . $row['id'] . "</td>";
echo "<td align='center'>" . $row['offname'] . "</td>";
echo "<td align='center'>" . $row['offphone'] . "</td>";
echo "<td align='center'>" . $row['time'] . "</td>\n";
echo "<td align='center' >" . $row['name'] . "</td>\n";
echo "</tr>";
}
echo "</table>";
?>
If you suggest any easy method I follow your code.
$query1 ="SELECT * FROM mandal_page WHERE id = '$category' && public = 'yes'";
}
else{
$query1 ="SELECT * FROM mandal_page WHERE id = '$category' && public = 'yes'";
}
The if and else are identical!
Second, mentioned above:
$query1 ="SELECT * FROM mandal_page WHERE id = '$category' && public = 'yes'";
I told you, if you quote q numeric field, and there's no value, it will do this:
$query1 ="SELECT * FROM mandal_page WHERE id = '' && public = 'yes'";
And there will be no field with value '', so no results. Had you done this,
$query1 ="SELECT * FROM mandal_page WHERE id = $category && public = 'yes'";
You would get a mysql error. Why? $category has not been set anywhere.
I also don't see where $id is set, but since the if/else does the same thing, let's forge on.
Try this. Note also you had results as the resources but result as the fetch_array.
if ($isset($_POST['category']) and ($_POST['category'] > 0)) {
$category=$_POST['category'];
$query1 ="SELECT * FROM mandal_page WHERE id = $category and public = 'yes'";
$result=mysql_query($query1) or die(mysql_error());
echo '<table align="center" bgcolor="#hhhhfff" class="reference"
width="60%" border="1" cellpadding="0" cellspacing="0">
<tr bgcolor="#ffffff">
<th>id</th>
<th>Office Name</th>
<th>offphone No.</th>
<th>Updated On</th>
<th>Updated By</th>
</tr>
';
while($row = mysql_fetch_array($result)) {
echo '
<tr>
<td align="center">' . $row['id'] . '</td>
<td align="center">' . $row['offname'] . '</td>
<td align="center">' . $row['offphone'] . '</td>
<td align="center">' . $row['time'] . '</td>
<td align="center">' . $row['name'] . '</td>
</tr>
';
}
echo "</table>";
} // end if
else { echo '<p>Oops no category selected.</p>'; }
I tried that code not working at.
where can i use java text code. In this script.
if u have a time recode my script. sorry for troubling.
I have searched for how can retrieve data from drop down list. I didn't find any solution for my script its easy but i can not understanding calling query correctly.
if (isset($_POST['category']) and ($_POST['category'] > 0)) {
......
Are you using GET method in your form? Malformed html in the form not posting "category?"
Are your category select list values still text values? See post #2, if you did not change the categories to a numeric value, text will always evaluate to 0. So if this is the case, change it to
if (isset($_POST['category']) and ($_POST['category'] != '')) {
$result=mysql_query($query1) or die(mysql_error());
... and ...
while($row = mysql_fetch_array($result)) {
2. Echo the select statement.
$query1 ="SELECT * FROM mandal_page WHERE id = $category and public = 'yes'";
echo "$query1\n";
(or, if it's still a text category, "... where id='$category' ...)
3. Copy it, go into whatever you use for a direct query of your database, either command line, phpMyAdmin->SQL link, whatever. Paste the select statement in. Do you get results?
But method="GET" where that get value "$name" I am used
that mean
$name = addslashes(strip_tags($_GET['name'])); where can i used this correctly.
I think this is main problem.
for example :
$query1 ="SELECT * FROM mandal_page WHERE category = '".$name."' and public = 'yes'";
that $name didn't getting from drop down list
rock are you under standing my English. sorry for my poor english.