Jump to content

Pull down list from mysql


NoDoze

Recommended Posts

First Question:
How do you do it? How do you get a pull down list to be the contents from a mysql database?

Second Question:
For example... You have a list of numbers in a drop down list, 101, 102, 103, 104, ect. that are getting pulled from the msql database...How do you make it so that when a user chooses, say 103 on the form, this number gets removed from the msql db list....so that the next person who shows up will then have 101, 102, 104, ect....?
Link to comment
Share on other sites

You can try this script:
[code]<select name="wordlist">
  <option value="1" selected>Select Word</option>
<?php
require("config.php");

$result = mysql_query("SELECT * FROM spell");
while($row = mysql_fetch_array($result))
{
    echo '<option value="' . $row['words'] . '"> ' . $row['words'] . "</option>\n  ";
}
?>
</select>[/code]
Link to comment
Share on other sites

I wouldnt delete it, but I personally hate deleting things.  Instead I would make another row and name it like 'active' and set it to 0 if it had been chosen, but make the defaut for it 1 so that they would begin active.

Anyways this should work...

[code=php:0]
<?
function form() { //function to output the form
echo "<form action=\"\" method=\"POST\">";

echo "<select name=\"wordlist\">
  <option value=\"1\" selected>Select Word</option>"; // i can never remember the syntax for drop down boxes so this is Gruzin's :p

$result = mysql_query("SELECT * FROM spell");
while($row = mysql_fetch_array($result))
{
    echo '<option value="' . $row['words'] . '"> ' . $row['words'] . "</option>\n  ";
}
echo "</select>";

}

function update_form($field) { //function to delete the rows
//$q = "UPDATE `spell` SET `active` = '0' WHERE `words` = '{$field}'";  //this will update the row like i said...
$q = "DELETE FROM `active` WHERE `words` = '{$field}'";
if(mysql_query($q)) {
return true;
}
else return false;
}
$link = @mysql_connect(); //set all your mysql crap
if(@mysql_select("dbname", $link) {
if(!$_POST) {
form();
}
else {
if(is_numeric($_POST['wordlist'])) {
//update the DB
if(update_form($_POST['wordlist'])) {
echo "Thanks for your input.";
}
else echo "Server error; Please try later";
}
else {
echo "Incorrect input";
}
}

}
else {
echo "Database error.  Please try again later.";
}
?>
[/code]

Link to comment
Share on other sites

ok, wow, thanks, but I think that was way too much info...heh...

This is what I have so far:

[code]
<select name="project_number" class="txtbox">
  <option value="1" selected></option>
<?php
$result = mysql_query("SELECT * FROM projnum");
while($row = mysql_fetch_array($result))
{
    echo '<option value="' . $row['project_number'] . '"> ' . $row['project_number'] . "</option>\n  ";
}

mysql_query("DELETE FROM projnum WHERE project_number = 'project_number'");

?>
</select>
[/code]

But it's not deleting the number....?

I know you're intentions wee good "corbin", but....
Let me give you some background....
This is in a html form posting to a mysql database and emailing the form.
I just need the delete line... :)

The changing the entry to active or not is an awesome idea, which I'm pretty sure I'll incorporate, but for now I just need to delete it.

Thanks a bunch guys!
Link to comment
Share on other sites

Also, just to add, and to try to answer some of the possible confusion...

The form is posting to on table and the project numbers are being pulled from a different table, but in both tables the row is called project_number...of course to make things confusing....

Hence why you see the form field being called project_number AND the options being called project_number...
Link to comment
Share on other sites

This is what I have so far:
[code]
<select name="project_number" class="txtbox">
  <option value="1" selected></option>
<?php
$result = mysql_query("SELECT * FROM projnum");
while($row = mysql_fetch_array($result))
{
    echo '<option value="' . $row['project_number'] . '"> ' . $row['project_number'] . "</option>\n  ";
}

mysql_query("DELETE FROM projnum WHERE project_number = 'project_number'");

?>
</select>
[/code]

So I know how to delete a table entry, but how do I get it to delete what the user chooses from the form?

Link to comment
Share on other sites

heh huh?

Number manually?
$project_number right before query?

Hmmmm, ya lost me....

[code]
<select name="project_number" class="txtbox">
  <option value="1" selected></option>
<?php
$result = mysql_query("SELECT * FROM projnum");
while($row = mysql_fetch_array($result))
{
    echo '<option value="' . $row['project_number'] . '"> ' . $row['project_number'] . "</option>\n  ";
}
mysql_query("DELETE FROM projnum WHERE project_number='$project_number'");
?>
</select>
[/code]

I think the issue is with $project_number I don't think it's picking up the chosen variable...perhaps?

Seams so simple, yet I'm having the hardest time with it!?! ...funny.
Link to comment
Share on other sites

Nope....

This is what I have so far:

[code]
<select name="project_number" class="txtbox">
  <option value="1" selected></option>
<?php
$result = mysql_query("SELECT * FROM projnum");
while($row = mysql_fetch_array($result))
{
    echo '<option value="' . $row['project_number'] . '"> ' . $row['project_number'] . "</option>\n  ";
}
mysql_query("DELETE FROM projnum WHERE project_number='$project_number'");
?>
</select>
[/code]
Link to comment
Share on other sites

You have to define the $project_number variable.

The form has to submit the variable so that the script can pick it up. Right now you are using an empy variable. Until there is some value associated with $project_number it will not delete anything in the table.
Link to comment
Share on other sites

Here is the code:

[code]
<form action="/distribution/phorm.php" method="post" name="form">
<table width="800" border="0">
<tr>
<td colspan="2"><span class="txt-ast">*</span>Initiation Date:&nbsp;
<input name="date" type="text" class="txtbox" size="11" maxlength="11" value="<?php echo date('m-d-Y');?>">
</td>
</tr>
<tr>
<td width="273">
<span class="txt-ast">*</span>Project Number:&nbsp;
<select name="project_number" class="txtbox">
  <option value="1" selected></option>
<?php
$result = mysql_query("SELECT * FROM projnum");
while($row = mysql_fetch_array($result))
{
    echo '<option value="' . $row['project_number'] . '"> ' . $row['project_number'] . "</option>\n  ";
}
mysql_query("DELETE FROM projnum WHERE project_number = 'project_number'") or die(mysql_error());
?>
</select>
</td>
<td width="517"><span class="txt-ast">*</span>Responsible Individual:&nbsp;
<input name="resposible_individual" type="text" class="txtbox" size="3" maxlength="3"><font size="1"><span class="style1">&nbsp; ( max 3 characters )</span></font></td>
</tr>
<tr>
<td colspan="2"><span class="txt-ast">*</span>Project Description:&nbsp;<input name="project_description" type="text" class="txtbox" size="30" maxlength="30">
<font size="1"><span class="style1">&nbsp; ( max 30 characters )</span></font></td>
</tr>
<tr>
<td colspan="2"><span class="txt-ast">*</span>County:&nbsp;<input name="county" type="text" class="txtbox" size="30" maxlength="30"><font size="1"><span class="style1">&nbsp; ( max 30 characters )</span></font></td>
</tr>
<tr>
<td height="32" colspan="2">State, City, River Basin:&nbsp;
<input name="region" type="text" class="txtbox" size="30" maxlength="30"></td>
</tr>
<tr>
<td height="32" colspan="2"><span class="txt-ast">*</span>Client:&nbsp;<input name="client" type="text" class="txtbox" size="30" maxlength="30"><font size="1"><span class="style1">&nbsp; ( max 30 characters )</span></font></td>
</tr>
<tr>
<td colspan="2" align="left" valign="top">&nbsp;</td>
</tr>
<tr>
<td colspan="2">
<center>
<INPUT TYPE=HIDDEN NAME="PHORM_CONFIG" VALUE="pilconfig.php">
<input name="add" type="submit" id="add" value="Add New Project">
</center>
</td>
</tr>
<tr>
<td colspan="2">
<center>
<span class="txt-ast">* <font size="1">are field that MUST be completed.</font></span>
</center>
</td>
</tr>
</table>
</form>

[/code]
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.