Jump to content

Recommended Posts

My code to delete a row(s) from DB isn't working. Instead it's redirecting from:

http://mywebsite.com/user/ihear/yt/deletedevice.php

to

http://user/ihear/yt/deletedevice.php

 

<?php 
# Script 3.4 - index.php
ob_start();
require_once('model_db_config_inc.php');
// Set the page title and include the HTML header.
$page_title = 'Delete Device';
#include ('./header.inc');
?>
<table width="90%" border="0" cellspacing="2" cellpadding="4" align="center">
  <tr bgcolor="#333333"> 
    <td> 
   <table width="100%" border="0" cellspacing="0" cellpadding="4">
        <tr> 
          <td bgcolor="#FFFFFF"> ! </td>
          <td width="100%"> <font color="#CCCCCC"> <b>Delete Model List</b></font></td>
        </tr>
      </table></td>
  </tr>
</table>
<table width="90%" border="0" cellspacing="4" cellpadding="4" align="center">
  <tr> 
    <td width="70%" valign="top"> <p><b>Delete A Model from iHear</b></p>
      <hr /> 
      <p>	  
  <?php	  
	  
if(isset($_POST['Submit'])){

unset($_POST['Submit']);
// useful debugging if needed
#echo "<pre>";
#echo var_dump($_POST);
#echo "</pre>";

#For each value in the delete array, we'll call a SQL delete statement.

foreach($_POST['delete'] as $value)
   	{	
	//ensure that form variable is securely handled.
	$id = (int)$value;

	// build the query
 	$query = "DELETE FROM ihear_models WHERE id = " . $id;

	//debugging 
	//echo $query;
	$result = mysql_query($query) or die('Could not delete the Model.');	
	if($result){
		echo "You've successfully deleted the Model at ID " . $id."<br/>";
	}else{
		echo "Something went horribly wrong when attempting to delete ID " . $id ."<br/>";			
	}
   	}

// LETS SHOW THE RECORDS AGAIN TO CONFIRM DB RECORD HAS BEEN DELETED. SORT BASED ON ID DESENDING
##########$newquery = "SELECT id, CONCAT(last_name, ', ', first_name) AS name, size, cost FROM ihear_models ORDER BY id DESC";
  $newquery = "SELECT id, name, size, cost FROM ihear_models ORDER BY id DESC";		
  $newresult = mysql_query ($newquery); // Run the query.

  if ($newresult) { // If it ran OK, display the records.
	echo '
	<table align="center" cellspacing="2" cellpadding="2" id="tablesorter" class="tablesorter">
	<thead> 
	<tr> 
		<th>ID</th>
	    <th>Name</th> 
	    <th>Start</th> 
	    <th>End</th>
	</tr> 
	</thead>';
	// Fetch and print all the records.
	while ($row = mysql_fetch_array($newresult, MYSQL_ASSOC)) {
		echo "<tr><td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["size"]."</td><td>".$row["cost"]."</td></tr>\n";
	}
	echo '</table>';
	}
  
  
  }else{  // ELSE STATEMENT FOR SUBMIT FORM
  
  
  $query = "SELECT id, name, size, cost FROM ihear_models ORDER BY id";		
  $result = mysql_query ($query); // Run the query.
  if ($result) { // If it ran OK, display the records.
  
  	?>
	<form name="form1" form action="/<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<?		
	echo '
	<table align="center" cellspacing="2" cellpadding="2" id="tablesorter" class="tablesorter">
	<thead> 
	<tr> 
		<th>ID</th> 
	    <th>Name</th> 
	    <th>Size</th> 
	    <th>Cost</th>
	    <th>Delete</th>	 
	</tr> 
	</thead>';

	// Fetch and print all the records.
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		echo "<tr><td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["size"]."</td><td>".$row["cost"]."</td><td><input name=\"delete[]\" type=\"checkbox\" value=". $row["id"] ."></td></tr>\n";
	}

	echo '</table>';
	echo '<table align="center" cellspacing="2" cellpadding="2"><tr><td align="left">';
	echo "<input type=\"submit\" name=\"Submit\" value=\"Delete selected!\" class=\"butt\" />";
	echo '</td></tr></table>';
	echo '</form>';
	mysql_free_result ($result); // Free up the resources.	

} else { // If it did not run OK.
	echo '<p>The Models could not be displayed due to a system error. We apologize for any inconvenience.</p><p>' . mysql_error() . '</p>'; 
}

mysql_close(); // Close the database connection.
  
}//END OF SUBMIT IF \ ELSE CONDITIONAL
  ?>
  </p>
      </td>
  </tr>
</table>
<?php
#include ('./footer.inc'); // Include the HTML footer.
?>

Link to comment
https://forums.phpfreaks.com/topic/205678-cant-delete-rows-from-db/
Share on other sites

Well perhaps why it is redirecting to the root is this problem:

 

action="/<?php echo $_SERVER['PHP_SELF']; ?>

 

Should be:

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

 

I did not look at the rest of the code, so there could be other issues.

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.