•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,567 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,595 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 916 | Replies: 5 | Solved
![]() |
This is what I have so far but it doesn't work.
This code is at the very top of my page and is supposed to delete the row from the database that has been selected from the drop down list. The thing is, even if it did work it would delete all rows in the database with the same heading and I can't figure out how to delete via primary key.
This is the code I have in the body of my page that fetches all the event headings and displays them in a drop down list
Confused and somewhat frustrated
This code is at the very top of my page and is supposed to delete the row from the database that has been selected from the drop down list. The thing is, even if it did work it would delete all rows in the database with the same heading and I can't figure out how to delete via primary key.
<?php
// Delete event data
if ($_REQUEST['delete']){
// open connection to MySQL server
$connection = mysql_connect('localhost:80', 'username', 'password')
or die ('Unable to connect!');
//select database
mysql_select_db('test') or die ('Unable to select database!');
//define variables
$eventSelect = $_POST['eventSelect'];
//create and execute query
$query = "DELETE FROM event WHERE heading = '$eventSelect'";
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
// close connection
mysql_close($connection);
}
?>This is the code I have in the body of my page that fetches all the event headings and displays them in a drop down list
<td>
<?php
// open connection to MySQL server
$connection = mysql_connect('localhost:80', 'username', 'password')
or die ('Unable to connect!');
//select database
mysql_select_db('test') or die ('Unable to select database!');
//create and execute query
$query = 'SELECT heading FROM event ORDER BY heading';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='events' value='-'>\n";
echo "<option value='Select event'>Select an event to be edited\n";
while($row = mysql_fetch_row($result))
{
$eventSelect = $row[0];
echo "<option value='$eventSelect'>$eventSelect</option>\n";
}
echo "</select>"
?>
<input name="edit" type="submit" class="submitForm" value="Edit">
<input name="delete" type="submit" class="submitForm" value="Delete">
</td>Confused and somewhat frustrated
Last edited by Venom Rush : Oct 24th, 2007 at 4:42 am.
to delete only one row from database, set your primary key as the value for the select like the following:
Then in your sql that deletes from database, add LIMIT 1 to the end and that will only affect one row.
Also change the following:
To:
This allows only one row to be deleted and uses the events id (the value of the selection in the dropdown box) to be deleted. You'll need to change the id= to match the primary key in your database and change the first block of code at the comment to match the primary id.
php Syntax (Toggle Plain Text)
//create selection list echo "<select name='events'>\n"; echo "<option value='Select event'>Select an event to be edited\n"; while($row = mysql_fetch_row($result)) { $eventSelect = $row[0]; $eventid = $row[1]; //set this to the column of th primary key echo "<option value='$eventid'>$eventSelect</option>\n"; } echo "</select>"
Then in your sql that deletes from database, add LIMIT 1 to the end and that will only affect one row.
Also change the following:
php Syntax (Toggle Plain Text)
//define variables $eventSelect = $_POST['eventSelect']; //create and execute query $query = "DELETE FROM event WHERE heading = '$eventSelect'";
To:
php Syntax (Toggle Plain Text)
//define variables $eventSelect = $_POST['events']; //create and execute query $query = "DELETE FROM event WHERE id = '$eventSelect' LIMIT 1";
This allows only one row to be deleted and uses the events id (the value of the selection in the dropdown box) to be deleted. You'll need to change the id= to match the primary key in your database and change the first block of code at the comment to match the primary id.
Always do what you wish you could..
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Create a cross-browser compatible, single-level, drop-down menu (Web Design Tutorials)
- HELP with CSS/Javascript Drop down menu from Dynamic Drive (HTML and CSS)
- opopulate textfields based on drop down menu selection (ColdFusion)
- Drop down menu with images (JavaScript / DHTML / AJAX)
- Filtering My sql through Php using drop down menu and text field (PHP)
- Is it possible to pass hidden value in <select> drop down menu? (PHP)
- Drop Down Menu (ASP.NET)
- Drop Down Menu Help! (Web Browsers)
Other Threads in the PHP Forum
- Previous Thread: PHP and adding Forum
- Next Thread: Update database with SMS



Linear Mode