Page 1 of 1

delete from a select menu

Posted: Thu Sep 22, 2005 3:55 pm
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

Posted: Thu Sep 22, 2005 4:06 pm
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.) :)

Posted: Thu Sep 22, 2005 4:07 pm
by Charles256
Je parlez francais!! If that helps any...

Posted: Thu Sep 22, 2005 4:08 pm
by NOVI
Io parlo italiano.

Posted: Thu Sep 22, 2005 4:18 pm
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)

Posted: Thu Sep 22, 2005 4:27 pm
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?

Posted: Thu Sep 22, 2005 4:28 pm
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...

Posted: Thu Sep 22, 2005 4:53 pm
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

Posted: Thu Sep 22, 2005 6:43 pm
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?

Posted: Fri Sep 23, 2005 8:10 am
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.

Posted: Fri Sep 23, 2005 8:14 am
by NOVI
Thanks feyd,
exact
I want to delete a record when a selection is made in the selectbox

Posted: Fri Sep 23, 2005 8:19 am
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...

Posted: Fri Sep 23, 2005 8:48 am
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?

Posted: Fri Sep 23, 2005 8:58 am
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)

Posted: Fri Sep 23, 2005 10:12 am
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.