...

View Full Version : Deleting form



Killermud
03-01-2009, 06:53 PM
What im trying to do is delete data from my SQL database from a SQL query list. Then select from that list what you would like to delete, i am able to generate the list but i cant manage to delete what i select.

I have 2 files, the list and the delete file.

List file ->


<?php

/**
* @author Killermud
* @copyright 2009
*/

require './connect.php';

if(!$con){
die('Could not connect: ' . mysql_error());
}

$tbl_name="forum_question";
mysql_select_db("$dbname")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result = mysql_query($sql);
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="10%" bgcolor="#F8F7F1"><strong>Select</strong></td>
<td width="15%" bgcolor="#F8F7F1"><strong>Topic</strong></td>



<?php
while($rows = mysql_fetch_array($result)){ ?>
<tr>
<td width="10%" bgcolor="#F8F7F1"><form action="delete.php">
<input type=checkbox name="<?php $rows['id']; ?>">
</td>
<td width="15%" bgcolor="#F8F7F1"><?php echo $rows['topic']; ?></td>
</tr>

<?php
}
?>
</tr>
</tr>
</table>
</table>
<input type="submit" name="submit" value="Delete">
</form>

Then this is where it goes wrong when i hit submit. Here is my delete file.



<?php

/**
* @author Killermud
* @copyright 2009
*/

require './connect.php';

if(!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$dbname")or die("cannot select DB");
$sql="SELECT id FROM users where id={$rows['id']}";
$result = mysql_query($sql) or die('Delete album failed. ' . mysql_error());

if (mysql_num_rows($result) == 1) {
$row = mysql_fetch_assoc($result);
$sql = "DELETE FROM $tbl_name WHERE id ={$rows['id']}";

mysql_query($sql) or die('Delete album failed. ' . mysql_error());
}
if(mysql_num_rows($results)){?>
<html>
<head>
<title>Go Back to previous Page</title>

<script language="javascript">
<!--

function function1() {

history.back(1);

}
//-->
</script>


</head>

<body>

<a href="javascript:function1()">Back</a><br>

</body>

</html>
<?php
}else
{
$msg = mysql_error();
echo $msg;
}
?>


The error i get is :


Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in c:\domains\opjeplaats.nl\wwwroot\darkness\webtemp\delete.php

Help please.

mlseim
03-01-2009, 06:59 PM
Your delete.php file does not know what this is .. (in red)

$sql = "DELETE FROM $tbl_name WHERE id ={$rows['id']}";

You define it in "list.php", but not in "delete.php".

In the list.php form, use a hidden variable with the table name:
<input type="hidden" name="table_name" value="$tbl_name">

Then in "delete.php" ...

$tbl_name=$_POST['table_name'];

Now it knows the name of the table.

Killermud
03-01-2009, 07:15 PM
Its got rid of that error to bring a new one,


Delete album failed. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

I made a few changes to my delete.php as well to :


<?php

/**
* @author Killermud
* @copyright 2009
*/

require './connect.php';

if(!$con){
die('Could not connect: ' . mysql_error());
}
$tbl_name=$_POST['table_name'];
mysql_select_db("$dbname")or die("cannot select DB");
$sql="SELECT id FROM users where id={$rows['id']}";
$result = mysql_query($sql) or die('Delete album failed. ' . mysql_error());

if (mysql_num_rows($result) == 1) {
$row = mysql_fetch_assoc($result);
$sql = "DELETE FROM $tbl_name WHERE id ={$rows['id']}";
}
if(@mysql_query($sql)){
echo "Deleted";
}
else
{
$msg = mysql_error();
echo $msg;
}
?>

masterofollies
03-01-2009, 08:05 PM
Not sure if it matters too much, but try this.


$sql = mysql_query("SELECT id FROM users WHERE id='".$rows['id']."'");

Killermud
03-01-2009, 08:11 PM
Hmm keep getting new errors ok now the error is :


Delete album failed. Query was empty

CFMaBiSmAd
03-01-2009, 08:21 PM
Your delete.php code is using $rows['id'] in the query(ies). Your form is not setting that variable when it submits. You should be developing and debugging code on a system where error_reporting is set to E_ALL and display_errors is set to ON so that undefined variables, like $rows['id'] will be reported.

Killermud
03-01-2009, 09:07 PM
Is there any other way of doing it?

CFMaBiSmAd
03-01-2009, 09:53 PM
I don't know, it's your code. What method did you define and design it to use to get the id from the form to the form processing code?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum