...

View Full Version : problems deleting from database



amylou111770
08-30-2007, 12:46 AM
hi i am fairly new to php/mysql i have this program that does not want to delete from the database. at first it was telling me that the file had been delete and when i checked the database it was still there. no i get two different errors depending on what i do to the code.
the delete code



<?PHP
ini_set('display_errors', 1);
error_reporting(E_ALL);

include 'mysqlconnectsitel.inc.php';// this has my database connection stuff

if($_GET['m'] == 'del' )// one error i get is right here
{

$requestId = $_GET['id'];
//$last=$_GET['last'];
//$first=$_GET['first'];
$query ="DELETE FROM timeoff WHERE requestId = '$requestId' ";// if i change this to last ='$last' AND first ='$first' o get undefined variables not pulling the data

$result= mysql_query($query) or die(mysql_error());

echo" <center><font color=\"#000000\">Your Info Was Deleted!</font></center> ";
echo "<p><strong>To start at the begining page.<a href='members.php'>Click here</a></strong></p>";
echo "<p><strong>To check your dates again.<a href ='checkdates.php'>Click here</a></strong></p>";

}
else{

echo "No selection was made. please try again.";


echo "<p><strong>To start at the begining page.<a href='members.php'>Click here</a></strong></p>";
echo "<p><strong>To check your dates again.<a href ='checkdates.php'>Click here</a></strong></p>";
}
?>



this code is activated from a link on a page that has check boxes and a link for delete that uses this. please help have been trying for a few days to get this.

Inigoesdr
08-30-2007, 01:06 AM
What are the errors you're getting?

amylou111770
08-30-2007, 01:11 AM
the errors are the following


Notice: Undefined index: m in C:\apache2triad\htdocs\index.php on line 7 No selection was made. please try again.

i did have something selected

right now cause i just tried it, the above error is all that i am getting


i fixed the error i took that out a replaced it with something else, but now when i run it a check a ckeck box hit delete link it say no selection was made.

Inigoesdr
08-30-2007, 01:25 AM
Are you POST'ing the form? <form method="post"
If so, you should be using $_POST instead of $_GET.
If not, your variable isn't being passed in the URL or isn't the correct name.

amylou111770
08-30-2007, 01:32 AM
here is the code that does the sql query and shows the results with check boxes. the delete is a link




if(isset($_POST['action'])){$action=$_POST['action'];}else{$action="";}
if( $action == 'updated' )
{
extract ($_POST);
$todaysDate = date("Y-m-d",time());



// Need to query database to see if this request is valid


echo "<p><strong>These are the dates that you have requested off!!!!</strong></p>";


$sql ="SELECT first, last, requestedDate FROM timeoff WHERE last = '$last' and first = '$first' ";
$query=mysql_query($sql) or die("Query failed : " . mysql_error());
while($row = mysql_fetch_assoc($query)) {
echo "<p>";
echo $row['first'] ." ". $row['last']. " ". " ". $row['requestedDate'];
echo "<td><input type=\"checkbox\" name=\"deleted_items[]\" ";
echo "</p>";
}

$action="";

$_html ="<tr>
<td>$_rw->requestId</td>
<td>$_rw->first</td>
<td>$_rw->last</td>
<td>$_rw->requestedDate</td>
<td>
<a href=\"index.php?id\">DELETE </a></td>

</tr>";
echo $_html;


}
else

{


?>



here is the link for the delete code

td>
<a href=\"index.php?id\">DELETE </a></td>

is there a tutitorial that shows how to delete this way with check boxes?

Inigoesdr
08-30-2007, 03:17 AM
Well, that delete link isn't going to do anything because it's just passing "id" as the query string. If you want to use checkboxes you have to provide each with the id of the MySQL row you want to remove. ie.

echo '<td><input type="checkbox" name="deleted_items[' . $row['id'] . ']" />'; // just noticed you didn't close your tag either
And loop through them on the next page. $_POST['deleted_items'] should be an array of the checkboxes that were checked(or nothing if none were checked).


if(!empty($_POST['deleted_items']))
{
foreach($_POST['deleted_items'] as $id => $on)
{
$query = 'DELETE FROM `table` WHERE `id` = ' . $id;
// mysql_query()
}
}

amylou111770
08-30-2007, 03:27 AM
i tried that and its still telling me that no selection is made when i did.

Inigoesdr
08-30-2007, 03:36 AM
Your inputs need to be wrapped in:


<form action="page.php?m=del" method="post">

<!-- checkbox inputs here -->

<input type="submit" value="Delete" />
</form>

So the while loop would look something like this:


<?php
// code...
?>
<form action="page.php?m=del" method="post"> <!-- <-- change the page name.. -->
<?php

while($row = mysql_fetch_assoc($query))
{
echo '<p>' . $row['first'] . ' ' . $row['last'] . ' ' . $row['requestedDate'];
echo '<td><input type="checkbox" name="deleted_items[' . $row['id'] . ']" />'; // I put "id" here, you need to change this to your id field, and make sure you select it in your query
echo '</p>';
}
?>

<!-- checkbox inputs here -->

<input type="submit" value="Delete" />
</form>
<?php
// code...
?>

Also, whenever you make changes, please post the code so we can see what the latest version looks like, and be able to spot errors easier.

amylou111770
08-30-2007, 04:04 AM
can i use this even though the delete code is on a different page then the code for the check boxes

Inigoesdr
08-30-2007, 04:07 AM
Yes, as long as the delete page is set in the <form action=""

amylou111770
08-30-2007, 04:31 AM
it now tells me that it has been deleted but still not taking it out of the database i have changed what needed to be changed. any other ideas

thank you for all your help

Inigoesdr
08-30-2007, 04:44 AM
Try echo'ing mysql_affected_rows() and $query:


$query = ''; // your query

echo $query . '<br />';
$result = mysql_query($query) or die(mysql_error());
echo 'Records deleted: ' . mysql_affected_rows() . '<br />';
Post both here, and try to run the query in phpMyAdmin to see if you get any errors.

amylou111770
08-30-2007, 04:51 AM
DELETE FROM `timeoff` WHERE requestId = 0
Records deleted: 0

this is what i got when i echoed the query and the mysql_affected_rows

amylou111770
08-30-2007, 04:53 AM
and if i select all of them to delelte the
requestId = 0
becomes a 3 and the records deleted stays the same

Inigoesdr
08-30-2007, 05:12 AM
Ok, so $requestId isn't being set on the delete page. Did you make that loop I posted earlier (http://www.codingforums.com/showpost.php?p=605932&postcount=6)? Post the delete page.

Also, look at the source of the page with the checkboxes and see if there are numbers in the name="deleted_items[#]". Post that page too if there aren't any numbers. If there are numbers then all we need to fix is the loop on the delete page.

amylou111770
08-30-2007, 05:16 AM
yes i did the loop you suggested earlier.

the first code is the delete code page


<?PHP
ini_set('display_errors', 1);
error_reporting(E_ALL);

include 'mysqlconnectsitel.inc.php';// this has my database connection stuff
?>

<?php
if(!empty($_POST['deleted_items']))
{
foreach($_POST['deleted_items'] as $requestId => $on)
{
$query = 'DELETE FROM `timeoff` WHERE requestId = ' . $requestId;
// mysql_query()
}


echo $query . '<br />';
$result = mysql_query($query) or die(mysql_error());
echo 'Records deleted: ' . mysql_affected_rows() . '<br />';

echo" <center><font color=\"#000000\">Your Info Was Deleted!</font></center> ";
echo "<p><strong>To start at the begining page.<a href='members.php'>Click here</a></strong></p>";
echo "<p><strong>To check your dates again.<a href ='checkdates.php'>Click here</a></strong></p>";
}

else{

echo "No selection was made. please try again.";


echo "<p><strong>To start at the begining page.<a href='members.php'>Click here</a></strong></p>";
echo "<p><strong>To check your dates again.<a href ='checkdates.php'>Click here</a></strong></p>";
}
?>



</body>
</html>



this second code is the main code with checkboxes


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<?php
/*this program was designed to eliminate paper work for the sift supervisors and the csps that work at Sitel here at loring.
this program was written and developed by Amy Coppola on July 2007
*/
?>
<?php
include'mysqlconnectsitel.inc.php';
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Check The Dates</title>
<link href= "links.css" rel="stylesheet" type="text/css"/>
<link href= "general.css" rel="stylesheet" type="text/css"/>

<?php
function getmonth($m=0) {
return (($m==0 ) ? date('F') : date('F', mktime(0,0,0,$m)));
}
?>

</head>
<body>
<br>

<img src="33720001.jpg" alt="sitel" width="300" height="100" />

<?php
if(isset($_POST['action'])){$action=$_POST['action'];}else{$action="";}
if( $action == 'updated' )
{
extract ($_POST);
$todaysDate = date("Y-m-d",time());

?>
<form action="index.php?m=del" method="post"> <!-- <-- change the page name.. -->
<?php
// Need to query database to see if this request is valid


echo "<p><strong>These are the dates that you have requested off!!!!</strong></p>";


$sql ="SELECT first, last, requestedDate FROM timeoff WHERE last = '$last' and first = '$first' ";
$query=mysql_query($sql) or die("Query failed : " . mysql_error());
while($row = mysql_fetch_assoc($query))
{
echo '<p>' . $row['first'] . ' ' . $row['last'] . ' ' . $row['requestedDate'];
echo '<td><input type="checkbox" name="deleted_items[' . $row['requestId'] . ']" />'; // I put "id" here, you need to change this to your id field, and make sure you select it in your query
echo '</p>';


}

$action="";

$_html ="<tr>
<td>$_rw->requestId</td>
<td>$_rw->first</td>
<td>$_rw->last</td>
<td>$_rw->requestedDate</td>
<td>


</tr>";
echo $_html;
?>
<input type="submit" value="Delete" />
</form>
<?php
}
else

{


?>

<form name='checkdate' method='post' action='checkdates.php'>
<table width="500" border="0">
<tr>
<td>First Name </td>
<td><input name="first" type="text" size="20" maxlength="20"></td>
</tr>
<tr>
<td>Last Name </td>
<td><input name="last" type="text" size="30" maxlength="30"></td>
</tr>

<tr><td><b>Todays Date:</b></td>
<td>
<?php
$todaysDate = date("m-d-Y",time());
echo "$todaysDate";
?>
</td>
</tr>

<tr><td colspan="2" align="center">
<input type= 'hidden' name= 'action' value= 'updated'>
<input type = "submit" name = "submit" value= "Submit">
</td></tr></table>

</form>
<?php
}
?>
</body>
</html>

Inigoesdr
08-30-2007, 05:24 AM
Here, try these:


<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

include 'mysqlconnectsitel.inc.php';// this has my database connection stuff

if(!empty($_POST['deleted_items']))
{
foreach($_POST['deleted_items'] as $requestId => $on)
{
$requestId = (int) $requestId;
$query = 'DELETE FROM `timeoff` WHERE `requestId` = ' . $requestId;
echo $query . '<br />';
$result = mysql_query($query) or die(mysql_error());
}

echo 'Records deleted: ' . mysql_affected_rows() . '<br />';

echo " <center><font color=\"#000000\">Your Info Was Deleted!</font></center> ";
echo "<p><strong>To start at the begining page.<a href='members.php'>Click here</a></strong></p>";
echo "<p><strong>To check your dates again.<a href ='checkdates.php'>Click here</a></strong></p>";
}
else
{
echo "No selection was made. please try again.";
echo "<p><strong>To start at the begining page.<a href='members.php'>Click here</a></strong></p>";
echo "<p><strong>To check your dates again.<a href ='checkdates.php'>Click here</a></strong></p>";
}
?>
</body>
</html>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<?php
/*this program was designed to eliminate paper work for the sift supervisors and the csps that work at Sitel here at loring.
this program was written and developed by Amy Coppola on July 2007
*/
?>
<?php
include'mysqlconnectsitel.inc.php';
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Check The Dates</title>
<link href= "links.css" rel="stylesheet" type="text/css"/>
<link href= "general.css" rel="stylesheet" type="text/css"/>

<?php
function getmonth($m=0) {
return (($m==0 ) ? date('F') : date('F', mktime(0,0,0,$m)));
}
?>

</head>
<body>
<br>

<img src="33720001.jpg" alt="sitel" width="300" height="100" />

<?php
if(isset($_POST['action'])){$action=$_POST['action'];}else{$action="";}
if( $action == 'updated' )
{
extract ($_POST);
$todaysDate = date("Y-m-d",time());

?>
<form action="index.php?m=del" method="post">
<?php
// Need to query database to see if this request is valid


echo "<p><strong>These are the dates that you have requested off!!!!</strong></p>";


$sql ="SELECT requestId, first, last, requestedDate FROM timeoff WHERE last = '$last' and first = '$first' ";
$query=mysql_query($sql) or die("Query failed : " . mysql_error());
while($row = mysql_fetch_assoc($query))
{
echo '<p>' . $row['first'] . ' ' . $row['last'] . ' ' . $row['requestedDate'];
echo '<td><input type="checkbox" name="deleted_items[' . $row['requestId'] . ']" />'; // I put "id" here, you need to change this to your id field, and make sure you select it in your query
echo '</p>';
}

$action="";

$_html ="<tr>
<td>$_rw->requestId</td>
<td>$_rw->first</td>
<td>$_rw->last</td>
<td>$_rw->requestedDate</td>
<td>


</tr>";
echo $_html;
?>
<input type="submit" value="Delete" />
</form>
<?php
}
else

{


?>

<form name='checkdate' method='post' action='checkdates.php'>
<table width="500" border="0">
<tr>
<td>First Name </td>
<td><input name="first" type="text" size="20" maxlength="20"></td>
</tr>
<tr>
<td>Last Name </td>
<td><input name="last" type="text" size="30" maxlength="30"></td>
</tr>

<tr><td><b>Todays Date:</b></td>
<td>
<?php
$todaysDate = date("m-d-Y",time());
echo "$todaysDate";
?>
</td>
</tr>

<tr><td colspan="2" align="center">
<input type= 'hidden' name= 'action' value= 'updated'>
<input type = "submit" name = "submit" value= "Submit">
</td></tr></table>

</form>
<?php
}
?>
</body>
</html>

amylou111770
08-30-2007, 05:28 AM
thank you thank you that seems to be working.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum