delete from a select menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NOVI
Forum Newbie
Posts: 7
Joined: Thu Sep 22, 2005 3:32 pm

delete from a select menu

Post by NOVI »

Hi
I don't speak well the English
The problem is:
I have a select menu as this
Image
how can I cancel from my DB a selected item?
I for example select "New York"
and with a key delete, would like that he was cancelled by my DB.
Many thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

8O I have no idea what you're asking specifically.. I get the really general idea, sorta.. Maybe you should post it in your spoken language (and tell us what language that is.) :)
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

Je parlez francais!! If that helps any...
NOVI
Forum Newbie
Posts: 7
Joined: Thu Sep 22, 2005 3:32 pm

Post by NOVI »

Io parlo italiano.
NOVI
Forum Newbie
Posts: 7
Joined: Thu Sep 22, 2005 3:32 pm

Post by NOVI »

This is the code that produces the list

Code: Select all

echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">"; 
	echo "<select name=\"pippo\" size=\"4\">"; 
	$query = "SELECT * FROM `ordini` WHERE ID = '$PHPSESSID' ORDER BY prodotto";
	$dati = mysql_query($query, $DB); 
	    while($row1 = mysql_fetch_array($dati)) 
		  { 
	echo "<option value=\ prodotto: ".$row1['prodotto']." , quantit&agrave;:  ".$row1['quantita']." (".$row1['prezzo']." euro ".$row1['modalita'].") totale ".$row1['totale']." euro\">prodotto: ".$row1['prodotto']." , 
	quantit&agrave;:  ".$row1['quantita']." (".$row1['prezzo']." euro ".$row1['modalita'].")  totale ".$row1['totale']." euro</option>"; 
	        } 
	echo "</select>"; 
  	echo "</form>";
now I should do something type

Code: Select all

$query1 = "DELETE * FROM `ordini`
when I select "New York" (for example)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

okay.. let me see if I get this straight. You want to delete a record when a selection is made in the selectbox?
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

that's what i think he's trying to say but I still can't wrap my mind around how that code generates that drop down box...
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

Je ne comprende pas comment son code(c'est francais pour "code"?) productions la HTML.
Je crois il veut a effacer un rapport (record) quand il choisit "New York".


English:
Yes, I know my french is weak. It happens when you don't speak it ever. :-D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ermm... you need italian not French Charles ;)

OK I'm hoping that "prodotto" is the primary key in your database? Something like a "product ID". If it's not, you will need something else unique in your <option> value="" atrribute so that you can find a record in your database to delete :)

Code: Select all

preg_match('/^prodotto:(.*?),/', $_POST['pippo'], $matches); //Use regex to extract the product ID from the POST data

$prodotto = $matches[1]; //This is the product ID ??

mysql_query("DELETE FROM ordini WHERE prodotto = $prodotto LIMIT 1") or die(mysql_error()); //Run the query to delete the record

//show a confirmation message
EDIT | Looking at your SELECT query I'm confused why you use the session ID as the ID in the database which suggests to me to be a primary key (ID) ? Or perhaps you don't want to delete an entire row, you only want to delete one value?
NOVI
Forum Newbie
Posts: 7
Joined: Thu Sep 22, 2005 3:32 pm

Post by NOVI »

Thanks d11wtq
the select of the city was as example.
I want to cancel a whole line, not only a value,a whole line is composed from more values
I want:
1)to select a line
2)to press a button that cancels the whole line
excuse but yesterday I have gone to sleep
Many thanks.
NOVI
Forum Newbie
Posts: 7
Joined: Thu Sep 22, 2005 3:32 pm

Post by NOVI »

Thanks feyd,
exact
I want to delete a record when a selection is made in the selectbox
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

okay.. things you need: a delete page (could be the same script as the form is on)
the use of the onchange event attached to the selectbox.
That's about it...

here's a start

Code: Select all

<form action="foo.php" method="post">
<input type="hidden" name="mode" value="delete" />
<select name="id" onchange="this.form.submit()">
<option value="12">New York</option>
<option value="6">Washington</option>
....
</select>
The idea is you emit the id record of the table to delete from as the value of each option. The text is whatever you wish...
NOVI
Forum Newbie
Posts: 7
Joined: Thu Sep 22, 2005 3:32 pm

Post by NOVI »

Thanks feyd
"I have many problems with the English, Sorry"
the select of the city was as example.
I want to cancel a whole line, not only a value,a whole line is composed from more values
I want:
1)to select a line
2)to press a button that cancels the whole line
3)I don't want redirect in another page
I want use
<form action=\"".$_SERVER['PHP_SELF']."\"
is it possible?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

NOVI wrote:the select of the city was as example.
right, it was the easiest example at hand for me to work with, since your code posted is marginally scary :lol:
NOVI wrote:I want to cancel a whole line, not only a value,a whole line is composed from more values
That's the idea behind what I posted.
NOVI wrote:I want:
1)to select a line
2)to press a button that cancels the whole line
okay, that basically removes the onchange event and leaves it as normal.
NOVI wrote:3)I don't want redirect in another page
I want use
<form action="".$_SERVER['PHP_SELF'].""
is it possible?
absolutely.. you simple have to code the script such that when a submission is detected it does the deletion (although I would recommend adding a confirm screen between the two)
NOVI
Forum Newbie
Posts: 7
Joined: Thu Sep 22, 2005 3:32 pm

Post by NOVI »

Code: Select all

echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">
	<input type=\"hidden\" name=\"mode\" value=\"delete\">
	<select name=\"id\" onchange=\"this.form.submit()\" size=\"4\">"; 
	$query1 = "SELECT `prodotto`, `quantita`, `modalita`,`prezzo`,`totale` FROM `ordini` ORDER BY prodotto";
	$dati1 = mysql_query($query1, $DB); 
	    while($row1 = mysql_fetch_array($dati1)) 
		  { 
	echo "<option value=\ prodotto: ".$row1['prodotto']." , quantit&agrave;:  ".$row1['quantita']." (".$row1['prezzo']." euro ".$row1['modalita'].") totale ".$row1['totale']." euro\">prodotto: ".$row1['prodotto']." , 
	quantit&agrave;:  ".$row1['quantita']." (".$row1['prezzo']." euro ".$row1['modalita'].")  totale ".$row1['totale']." euro</option>"; 
	        } 
	$query3=("DELETE FROM ordini LIMIT 1");
		$dati = mysql_query($query3, $DB);	
	echo "</select>"; 
	echo "</form>";
It cancels, but it doesn't insert the data anymore.
Post Reply