...

View Full Version : delete a row from a table



woodplease
01-28-2010, 08:42 PM
I'm trying to delete a row from my table using a checkbox, but the problem is my code doesn't seem to work. Any ideas would be great.



<?php
$db = pg_connect("host=database.dcs.aber.ac.uk

port=5432 dbname=***** user=***** password=*********");
?>
<?php
$sql="SELECT * FROM quiz";
$result=pg_query($sql);

$count=pg_num_rows($result);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in sql</strong> </td>
</tr>
<tr>

<td align="center" bgcolor="#FFFFFF"><strong>Quiz No</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Title</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Created By</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Delete</strong></td>
</tr>
<?php
while($rows=pg_fetch_array($result)){
?>
<tr>

<td bgcolor="#FFFFFF"><? echo $rows['quizref']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['createdby']; ?></td>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['quizref']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?

if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM quiz WHERE quizref='$del_id'";
$result = pg_query($sql);
}


if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=quizlisttest.php\">";
}
}
pg_close();
?>
</table>
</form>
</td>
</tr>
</table>

PappaJohn
01-28-2010, 09:36 PM
$del_id = $checkbox[$i];
You are relying on register_globals being on. This is a bad idea as it has been off by default for several versions and will be completely removed in PHP 6.

You will need to retrieve the variables from the $_POST array before using them in your code.

During development, it's also a good idea to turn on error reporting - let php tell you where the problem is.

c0d3pRaDa
01-28-2010, 09:45 PM
I'm not too sure about the pg_connect and so forth but try to get the logics and change it to suit yourself




<?php

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

/////////////////////////////////////////
$db = pg_connect("host=database.dcs.aber.ac.uk

port=5432 dbname=***** user=***** password=*********");
/////////////////////////////////////////

$chkbox = $_POST['chkbox'];
$count = count($chkbox);
$i = 0;
while ($i < $count) {
$sql="DELETE FROM quiz WHERE quizref ='$chkbox[$i]'";
$result=pg_query($sql);
$i++;
}
}
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in sql</strong> </td>
</tr>
<tr>

<td align="center" bgcolor="#FFFFFF"><strong>Quiz No</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Title</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Created By</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Delete</strong></td>
</tr>
<?php
while($rows=pg_fetch_array($result)){
?>
<tr>

<td bgcolor="#FFFFFF"><? echo $rows['quizref']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['createdby']; ?></td>
<td align="center" bgcolor="#FFFFFF"><input name="chkbox[]" type="checkbox" id="chkbox[]" value="<? echo $rows['quizref']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=quizlisttest.php\">";
}
}
pg_close();
?>
</table>
</form>
</td>
</tr>
</table>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum