Jump to content

Can't delete rows from database


phpnewbie1979

Recommended Posts

Hi everyone

 

I am in the process of creating a simple cms for my site and I ran into a problem deleting rows from the database.

I created a script that shows all rows in the database and then tried to create a delete button that would delete the row if clicked. I'm obviously doing something wrong and I can't figure it out. Any help would be greatly appreciated. I've been at it for a few days with no solution. The script i'm using is to show all rows and place the delete link is:

 


<?php
require_once "connect_to_mysql.php";
$query="SELECT * FROM table_name";
$result=mysql_query($query);

$num=mysql_num_rows($result) or die(mysql_error());

mysql_close();
?>

<div>
<table border="0" cellspacing="0" cellpadding="0">

<?php
$i=0;
while ($i < $num) {
$c0=mysql_result($result,$i,"fieldone");
$c1=mysql_result($result,$i,"fieldtwo");
$c2=mysql_result($result,$i,"fieldthree");
$c3=mysql_result($result,$i,"fieldfour");
$c4=mysql_result($result,$i,"fieldfive");
$c5=mysql_result($result,$i,"fieldsix");
$c6=mysql_result($result,$i,"fieldseven");
$c7=mysql_result($result,$i,"fieldeight");
$c8=mysql_result($result,$i,"fieldnine");
$c9=mysql_result($result,$i,"fieldten");
?>



<tr><td class="td_border_left" width="50"><?php echo $id; ?></td>
<td class="td_border_right" width="300">
<strong><?php echo $c1; ?></strong><br />
<?php echo $c2; ?><br />
<?php echo $c3; ?>, <?php echo $c4; ?><br /><br />
<strong>Program:</strong> <?php echo $c5; ?><br /><br />
<strong>Waiting List:</strong> <?php echo $c6; ?><br />
<strong>Type:</strong <?php echo $c7; ?><br />
<strong>Application Deadline:</strong> <?php echo $c8; ?><br />
<strong>Application Deadline:</strong> <?php echo $c9; ?><br />
</td>

<form id="form" name="form" method="post" action="delete.php">
<td class="td_border_right" valign="top">
<input type="hidden" name="fieldname" value="<?=$_Post['$c0']; ?>"/>
<input type="submit" value="delete" />
</td>
</form>
</tr>

<?php
$i++; 
}
?>
</table>
</div>

 

and the delete script I'm using is:

 

<?php

$c0 = $_POST['$c0'];

include_once "connect_to_mysql.php";
// Add the updated info into the database table
$query="DELETE FROM table_name WHERE c0='$c0'"; 

echo($query);

mysql_query($query) or die(mysql_error());
?>

 

The error I'm getting is

DELETE FROM affhousing WHERE id=''

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

<?=$_Post['$c0']; ?>

 

should be

 

<?=$_POST[$c0]; ?>

 

or maybe

 

<?=$_POST['c0']; ?>

 

(can't be sure without seeing more of your code)

 

For the other file

 

$c0 = $_POST['$c0'];

 

should be

 

$c0 = $_POST['fieldname'];

 

probably

Thanks I figured it out. in the first script I put

 

<input type="hidden" name="fieldname" value="<?=$_Post[$'c0']; ?>"/>

 

I should have put:

 

<input type="hidden" name="fieldname" value="<?php echo $c0; ?>"/>

 

and in the delete script i changed

 

$c0 = $_POST['$c0'];

 

to just

 

$_POST['$c0'];

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.