Jump to content

Recommended Posts

New to php so I'm having trouble with deleting a record from my php table.

 

 

This is my code for my page where the records are being displayed. I have a delete statement that is not working properly. Also, I was thinking maybe I need a array to delete records? Please help!

 

 

 

 

 

<?php
require_once('database.php');
session_start();




if (isset($_POST['add_grade']))
{


	$query		= "INSERT INTO grades (student_id, grade_type, grade_name, grade_points) ";
	$query		.= "VALUES (:student_id, :grade_type, :grade_name, :grade_points) ";

	$statement	= $db->prepare($query);
	$statement->bindValue (':student_id', $_SESSION['student_id']);
	$statement->bindValue (':grade_type', $_POST['grade_type']);
	$statement->bindValue (':grade_name', $_POST['grade_name']);
	$statement->bindValue (':grade_points', $_POST['grade_point']);

	$statement->execute();

	$statement->closeCursor();


	if (isset($_POST['remove']))
{

	foreach($_POST['delete'] as $delete_id)
	{

	$query		= "DELETE FROM grades WHERE grade_id = $delete_id";
	mysqli_query($dbc, $query) or die ('can\'t delete user'); 

	}
}


	}








?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View Course Grades</title>

<link rel="stylesheet" type="text/css" href="main.css" />
</head>

<body>


    



<?php

$student_name = $_SESSION['student_name'];
$student_id = $_SESSION['student_id'];
$query		= "SELECT * FROM grades WHERE student_id = :student_id ";

$statement	= $db->prepare($query);
$statement->bindValue (':student_id', $student_id);
$statement->execute();
$grades		= $statement->fetchAll();
$statement->closeCursor();


echo "<h1>Show Grades for $student_name </h1>";

	foreach ($grades as $grade)
{

	echo $grade['grade_type'] . " " . $grade['grade_name']. " " . $grade['grade_points'] . "<br />";
}

	$query = "select*from se266";  
	$result = mysql_query($dsn, $query);  
	while($row = mysql_fetch_array($result))
{      
	echo '<input type="checkbox" value="' .$row['grade_id'] . '"name="delete[]" />';
	echo ' ' .$row['grade_type']; 
                                echo ' ' .$row['grade_name'];
	echo '<br />';  

	}


?>

	<div id="content">
            <!-- display a table of products -->
            
            <table>
                <tr>
                    <th>Grade Type</th>
                    <th>Grade Name</th>
                    <th>Grade Points</th>
                    <th>Remove</th>
                </tr>
                <?php foreach ($grades as $grade) : ?>
                <tr>
                    <td><?php echo $grade['grade_type'];   ?></td>
                    <td><?php echo $grade['grade_name'];   ?></td>
                    <td><?php echo $grade['grade_points']; ?></td>
                    <td><form action="grades.php" method="post">
                        
                        <input type="submit" name="remove" value="Delete" />




                    </form></td>
                </tr>
                <?php endforeach; ?>
            </table>
            
        </div>
    </div>

    <div id="footer">
       
    </div>






<form name="grades" method="post" action="grades.php">

	<p>Grade Type<SELECT NAME="grade_type">
	<OPTION VALUE="Mid-Term">Mid-Term
	<OPTION VALUE="Final">Final
	<OPTION VALUE="Lab">Lab
	</SELECT>
	<br>







	Grade Name:<input type="text" name="grade_name" value=""><br />
	Grade Points:<input type="text" name="grade_point" value="">

	<input type="submit" name="add_grade" value="Add Grade">


	</form>

</table>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/247304-how-to-delete-a-record/
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.