Page 1 of 1

Deleting record from database, using drop down list-user pic

Posted: Fri Dec 23, 2011 5:57 pm
by TheDumbNerd
The user has to select one of the records from the drop down list and whatever one they select, will get deleted from the database? How would I do that?

Code: Select all

<?php
@$record_action=$_POST['select'];
@$id_select=$_POST['idselect'];



If ($record_action=='Delete')

?>
<h1> Delete Record </h1>

<form name='confirmdeleteform' action='' method='POST'>
<input type='submit' name='select' value='Okay'/>
</form>
Contacts:

<select name="contacts" >

	 <option value="0"><- Select a Contact -></option>

	 <?php

		 // Connect to database. If this fails output message.

		 $db=mysql_connect("localhost","root") or die(mysql_error());

					

		 // Select a database to use. If this fails, output error message.						

		 mysql_select_db("project6",$db) or die (mysql_error());

					

		 // Define SQL statement.

		 $SQL="SELECT ID, First_Name, Last_Name FROM contacts ORDER by Last_Name"; 

					

		 // Run SQL statement in database. If this fails display error message.

		 $result=mysql_query($SQL) or die(mysql_error());

					

		 // Count the number of records that match the query.

		 $num_results=mysql_num_rows($result);

					

		 // Fill the City drop down list box with values from the database

		 for ($i=0;$i<$num_results;$i++)

			 {

			 $row=mysql_fetch_array($result);

			 echo "<option value=\"".$row["ID"]."\">".$row["First_Name"].", ".$row["Last_Name"]."</option>";
			 
			 

			 }

		

		 // Close databse connection.

		 mysql_close($db);
		 
if ($record_action=='Okay') {
echo "<script>alert('Confirm')</script>";
echo "<script>navigate('p06_main.php')</script>";
exit();
} 

Well the alert pops up, and they say okay to the alert , then the item that they have selected from the database would get deleted. How would you do that?

Re: Deleting record from database, using drop down list-user

Posted: Fri Dec 23, 2011 8:08 pm
by Christopher
Move the connect() and select_db() calls to the top of the file. Change your if to show the form if 'select' is not set. Add an else that calls query() to do the delete and show a page that displays the result.