Jump to content

Recommended Posts

hello everyone actually i'm trying to delete data from the database that is been queried in the drop down menu....

The code is shown below...

 

<?php
require_once 'myconn.php';
$sql = "SELECT course_id, course_name FROM course";
$result = mysql_query($sql);
echo "<form method='post' action=''>";
echo "<label>Delete Course</label>";
echo "<select>";
while($row = mysql_fetch_array($result)){
echo "<option value='{$row['course_id']}'>{$row['course_name']}</option>";
}
echo "</select>";
echo "<input type='submit' name='submitbtn' value='Delete'>";
if(isset($_REQUEST['submitbtn'])){
   $sql1= "DELETE course_name FROM course WHERE course_id='{$row['course_id']}'";
   $reslut1= mysql_query($sql1);
   if(mysql_affected_rows()>0){
    header("Location:".$_SERVER['php_SELF']);
   }

}
?>

 

When i press delete button the page get refresh but nothing gets deleted from the list...

 

Can any one help me with it...

Link to comment
https://forums.phpfreaks.com/topic/272751-delete-from-database/
Share on other sites

To elaborate on that, your problem lies in this code:

 

if(isset($_REQUEST['submitbtn'])){
       $sql1= "DELETE course_name FROM course WHERE course_id='{$row['course_id']}'";

 

You are trying to delete $row['course_id'], but that is not the value that has been submitted. The value that has been submitted will be part of the $_POST array.

To elaborate on that, your problem lies in this code:

 

if(isset($_REQUEST['submitbtn'])){
$sql1= "DELETE course_name FROM course WHERE course_id='{$row['course_id']}'";

 

You are trying to delete $row['course_id'], but that is not the value that has been submitted. The value that has been submitted will be part of the $_POST array.

no actually i'm trying to delete the 'course_name' not the 'course_id'...

 

If possible can you plz show me... I'm not getting it...

Thanks...

You cannot DELETE column_name. You DELETE a whole row. It's DELETE FROM table ...

 

I did what you told but still its not working....

echo "<input type='submit' name='submitbtn' value='Delete'>";
if(isset($_REQUEST['submitbtn'])){
   $sql1= "DELETE FROM course WHERE course_id='{$row['course_id']}'";
   $reslut1= mysql_query($sql1);
   if(mysql_affected_rows()>0){
    header("Location:".$_SERVER['php_SELF']);
   }

}
?>

Have you taken care of this? It will never work until you do.

 

Your <select> field needs a name= attribute, which you would then use to get the value of the <option> from the $_POST array, validate it, and use that value in the delete query.

<?php

require_once 'myconn.php';

$sql = "SELECT course_id, course_name FROM course";

$result = mysql_query($sql);

echo "<form method='post' action=''>";

echo "<label>Delete Course</label>";

echo "<select>";

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

echo "<option value='{$row['course_id']}'>{$row['course_name']}</option>";

}

echo "</select>";

echo "<input type='submit' name='submitbtn' value='Delete'>";

if(isset($_REQUEST['submitbtn'])){

$sql1= "DELETE FROM course WHERE course_id='{$row['course_id']}'";

$reslut1= mysql_query($sql1);

if(mysql_affected_rows()>0){

header("Location:".$_SERVER['php_SELF']);

}

 

}

 

?>

You have not given a name attribute to the select element, so you will not have a named index for it in the POST array. Though, not that it matters since you're not using it anyway.

 

What you need to do is to realize that PHP and HTML are two completely separate processes: PHP may generate the HTML, but it has no knowledge of what that HTML code means. Same with HTML, it does not know that PHP exists and as such cannot alter anything in the PHP code does.

What you can do, however, is to use regular HTTP requests to send data to the server. Which the PHP code picks up, and then use said data to determine what it should do.

 

The best way to accomplish this mental model is by "physically" separating the HTML and PHP code, so that all of your PHP code is located at the very top of the file. Then use variables to hold the generated HTML content, and then echo it out wherever you need it.

This will make your script look something like this:

<?php

// Retrieve list of courses.

if (is_submitted ()) {
   // Retrieve and validate data

   if (!$valid) {
       // Generate error message.
       // Repopulate the form with the selected value
       // Show the form w/error message and abort the deletion process.
   }

   // Delete the selected row from the DB
   // Redirect user to confirmation screen.
   die ();
}

// Not submitted, generate clean form.

?>
<doctype html>
<html>
<head>

<title>Your page</title>

</head>
<body>

<!-- Your HTML code here. -->
<?php echo $form; ?>

</body>
</html>

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.