Jump to content

DELETE statement trouble


MjM8082

Recommended Posts

Can't figure out why my form is not deleting a user from database anymore. It was working a hour ago. Now it's not working, and I have no idea why. I've tried everything. Am I missing something?

 

 

Here is the delete page...

 

 

 

<html>
<head>
<title>Update User</title>
</head>
<body>

<?php
$dbc = mysqli_connect('localhost', 'se266_user', 'pwd', 'se266') 
	or die(mysql_error());
//delete users
echo '<b>Delete or Update User</b>.<br />';
if (isset($_POST['remove']))
{          
	foreach($_POST['delete'] as $delete_id)
	{             
		$query = "DELETE FROM users WHERE course_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)
	{             
		$course_id 	= $_POST['course_id'];
		$course_name = $_POST['course_name'];
		$student_id = $_POST['student_id'];

		$query = "UPDATE `users` SET `course_name` = '$course_name' WHERE `course_id` = '$course_id' AND 'student_id' = '$student_id'";        
		mysqli_query($dbc, $query) or die ('can\'t update course');     

		$update_count = $db->exec($query);

	}				
	echo 'course has been updated.<br />';   
}


//display users info with checkbox to delete  
$query = "SELECT * FROM users";  
$result = mysqli_query($dbc, $query);  
while($row = mysqli_fetch_array($result))
{      
	echo '<input type="checkbox" value="' .$row['course_id'] . '" name="delete[]" />'; 
	echo ' ' .$row['course_name'] .' '. $row['student_id'];
	echo '<br />';  
}
mysqli_close($dbc);

?>	

<form method="POST" action="update_user2.php">		
<label for="course_id">Course ID:</label> 
<input type="text" id="course_id" name="course_id" /><br />		
<label for="course_name">Course Name:</label>
<input type="text" id="course_name" name="course_name" /><br />

<label for="course_name">Student ID:</label>

<input type="text" id="student_id" name="student_id" /><br />		



</form>
<form method="post" action="update_user2.php">
<input type="submit" name="remove" value="Remove" />
<input type="submit" name="update" value="Update" />
<br>
<br>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/245321-delete-statement-trouble/
Share on other sites

Good call, thanks a lot. Another question...

 

My update statement doesnt work... in that same code. I fill out the form and then click update. and the information in the database does not get updated. Also not sure why this is happening.

 

Is my form coded correctly to work with the update button?

 

 

 

The error I'm getting is...

 

 

"Could not query the database - :

Access denied for user ''@'localhost' (using password: NO)"

Good call, thanks a lot. Another question...

 

My update statement doesnt work... in that same code. I fill out the form and then click update. and the information in the database does not get updated. Also not sure why this is happening.

 

Is my form coded correctly to work with the update button?

 

 

 

The error I'm getting is...

 

 

"Could not query the database - :

Access denied for user ''@'localhost' (using password: NO)"

 

This error happen when your database connection fail.

Try to check your database connection.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.