Jump to content

How To Update A Record In My Table


MjM8082

Recommended Posts

Not sure how to update a record in my table here. I got my insert and delete statement working. Just can't get the update to work correctly. I'm not really sure how to do it.

 

 

 

Here is my code for my page, and my update and delete page

 

 

 

<?php

ini_set ("display_errors", "1");
error_reporting(E_ALL);

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




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



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

	$statement	= $db->prepare($query);
	$statement->bindValue (':student_id', $_SESSION['student_id']);
	$statement->bindValue (':grade_id', $_SESSION['grade_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();





	$grade_type = $_POST['grade_type'];

	if ($grade_type == 'Lab') {


	$final *= .60;
}

	echo $final;


	$grade_type = $_POST['grade_type'];

	if ($grade_type == 'Mid-Term') {


	$final *= .20;
}

	echo $final;



	$grade_type = $_POST['grade_type'];

	if ($grade_type == 'Final') {


	$final *= .20;
}

	echo $final;


}

?>
<!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 />";
}





?>

	<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="delete_grade.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>

 

 

 

 

My delete and update page

 

 

 

 

<html>
<head>
<title>Delete Grade</title>
</head>
<body>
<form method="post" action="delete_grade.php">

<?php


			ini_set ("display_errors", "1");
			error_reporting(E_ALL);

	$dbc = mysqli_connect('localhost', 'se266_user', 'pwd', 'se266') 
	or die(mysql_error());

	//delete grades

	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');          
}        
	echo 'user has been deleted.<br />';   
}

	if (isset($_POST['update']))
{          
	foreach($_POST['update'] as $update_id)
	{             
		$query = "UPDATE grades SET grade_id = $grade_id";             
		mysqli_query($dbc, $query) or die ('can\'t update user');    
       
	}
	}

	//Display grade info with checkbox to delete  
	$query = "SELECT * FROM grades";  
	$result = mysqli_query($dbc, $query);  
	while($row = mysqli_fetch_array($result))
{      
	echo '<input type="checkbox" value="' .$row['grade_id'] . '" name="delete[]" />'; 
	echo ' ' .$row['grade_type'] .' '. $row['grade_name'];
	echo '<br />';  
}
	mysqli_close($dbc);

?>

<input type="submit" name="remove" value="Remove" />
<input type="submit" name="update" value="Update" />

</form>
</body>
</html>

Link to comment
Share on other sites

Thanks for the reply. I fixed that part in my code.

 

How do I make my update statement work with my check boxes? The delete button works great with the check boxes but the update won't.

 

Also I keep getting this error when I click the update button... "invalid argument supplied for foreach()"

 

which is this line in my code....      foreach($_POST['update'] as $update_id)

 

 

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.