PDA

View Full Version : Deleting data :S


shazzi2k
02-22-2009, 07:46 PM
Hi, bit stuck on this for a while now, cannot seem to figure out what is wrong. When i try to delete data with the button nothing seems to happen, just the refresh. But the record stays.

I have been trying to find fault in these areas.

////////////////////////
$data2 = $_POST[ 'data2' ] ;
$data3 = $_POST[ 'data3' ] ;
$data4 = $_POST[ 'data4' ] ;
$Submit = $_POST[ 'Submit'] ;
$Delete = $_GET [ 'Delete' ] ;
////////////////////////

if ($Delete) {
////////////////////////
$query = sprintf ("DELETE FROM table1 where field1_id='del' ");
mysql_query ($query) or die (mysql_error () );
////////////////////////

<input type="button" name="Submit2" value="Delete" onclick= "document.location.href='shazcorp.php?Delete= <?php echo $row ['field1_id'] ?> '"/>
</td>
</tr>
<?php }while ($row = mysql_fetch_array($result)); ?>

shazzi2k
02-22-2009, 07:55 PM
This is it all
<?php
require_once("../Connections/connection.php"); //database connection script//



////////////////////////
$data2 = $_POST[ 'data2' ] ;
$data3 = $_POST[ 'data3' ] ;
$data4 = $_POST[ 'data4' ] ;
$Submit = $_POST[ 'Submit'] ;
$Delete = $_GET [ 'Delete' ] ;
////////////////////////

if ($Delete) {
////////////////////////
$query = sprintf ("DELETE FROM table1 where field1_id='del' ");
mysql_query ($query) or die (mysql_error () );
////////////////////////
}

///////////////////////

if ($Submit && $data2 && $data3 && $data4) {

////////////////////////

$query = sprintf ("INSERT into table1 (field2, field3, field4) values ('$data2', '$data3', '$data4') ");
mysql_query ($query) or die (mysql_error () );

}elseif ($Submit) {
echo "A required field is empty!" ;

}

////////////////////////
$query = sprintf (" select * FROM table1");
$result = mysql_query ($query);
$row = mysql_fetch_array ($result);
////////////////////////

/*do {
echo $row ['field3']. "<br>";

} while ($row = mysql_fetch_array($result) )
*/
?>


<!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=iso-8859-1" />
<title>ShazCorp Insert data</title>
</head>

<body>
<p>&nbsp;</p>
<form id="form1" name="form1" method="post" action="<?php $_SERVER [ 'PHP_SELF'] ; ?>">
<table width="437" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="146" height="28"><div align="center">data2</div></td>
<td width="145"><div align="center">data3</div></td>
<td width="146"><div align="center">data4</div></td>
</tr>
<tr>
<td height="36"><label>
<input name="data2" type="text" id="data2" />
</label></td>
<td><input name="data3" type="text" id="data3" /></td>
<td><input name="data4" type="text" id="data4" /></td>
</tr>
<tr>
<td height="47" colspan="3"><label>
<div align="center">
<input type="submit" name="Submit" value="Submit" />
<input name="Submit" type="hidden" id="Submit" value="1" />
</div>
</label></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
<p>&nbsp;</p>
<table width="603" border="1" cellspacing="1" cellpadding="1">
<tr>
<td width="99" height="27">ID1</td>
<td width="135">data2</td>
<td width="135">data3</td>
<td width="135">data4</td>
<td width="71">&nbsp;</td>
</tr>
<?php do { ?>
<tr>
<td height="30"><?php echo $row ['field1_id']; ?></td>
<td><?php echo $row ['field2']; ?></td>
<td><?php echo $row ['field3']; ?></td>
<td><?php echo $row ['field4']; ?></td>
<td>
<input type="button" name="Submit2" value="Delete" onclick= "document.location.href='shazcorp.php?Delete= <?php echo $row ['field1_id'] ?> '"/>
</td>
</tr>
<?php }while ($row = mysql_fetch_array($result)); ?>
</table>
<label></label>
</body>
</html>

davidj
02-22-2009, 10:14 PM
where you have this


$query = sprintf ("DELETE FROM table1 where field1_id='del' ");


how do you know what record to delete when your not passing a numerical ID

unless you have a record which has a reference called del