Jump to content

Delete from mysql


Goon

Recommended Posts

Using the following code I can retrieve information from a mysql table and have the results displayed on the same page.

 

Is it possible to add a delete option next to each of the returned results to allow user to delete unwanted records

 

<h3>Please select your vehicle type</h3>

 

 

 

<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

Please select your vehicle type: 

<select name="vehicle">

<option value="car">Car</option>

<option value="van">Van</option>

<option value="truck">Truck</option>

<option value="bus">Bus</option>

</select>

<br/> <br>

 

<input type="submit" value ="Search for tyres"/>

 

</form>

 

<?php

 

$vehicle = $_POST['vehicle'] ;

 

if ( $vehicle == '' )

{

// print "<br>no vehicle selected ..." ;

}

else

{

$host = "localhost";

$user = "root";

$password = "*****";

$database = "*****";

$table = "tyre";

 

$connection = mysql_connect($host, $user, $password)

or die ("Couldn't connect to server");

 

$db = mysql_select_db($database, $connection)

or die ("Couldn't connect to database");

 

$result = mysql_query("SELECT * FROM $table WHERE vehicle = '" . $vehicle . "'")

or die(mysql_error()); 

 

print "<br>Available tyres for: $vehicle<br><br>" ;

 

echo "<table border='1'>";

echo "<tr> <th>Vehicle</th> <th>Size</th> <th>Brand</th> <th>Price</th> </tr>";

 

// keeps getting the next row until there are no more to get

while($row = mysql_fetch_array( $result )) {

// Print out the contents of each row into a table

echo "<tr><td>";

echo $row['Vehicle'];

echo "<td>";

echo $row['Size'];

echo "<td>";

echo $row['Brand'];

echo "</td><td>";

echo $row['Price'];

echo "</td></tr>";

}

 

echo "</table>";

 

}

Link to comment
Share on other sites

Is it possible to add a delete option next to each of the returned results to allow user to delete unwanted records

 

Yup. I suggest you output a link to a delete page and pass the ID of the vehicle (which you hopefully have) to the page in the URL. It'll then be a case of something like this:

 

<?php
$id = (int) $_GET['id'];
mysql_query("DELETE FROM yourtable WHERE id=$id");
echo 'Vehicle successfully deleted
?>

 

Of course, you may wish to use checkboxes instead so that more than one vehicle can be deleted at a time.

 

Also, please remember to use


tags around your 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.