Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-29-2007, 11:46 PM   PM User | #1
amylou111770
New Coder

 
Join Date: Aug 2007
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
amylou111770 is an unknown quantity at this point
Question problems deleting from database

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
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.
amylou111770 is offline   Reply With Quote
Old 08-30-2007, 12:06 AM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
What are the errors you're getting?
Inigoesdr is offline   Reply With Quote
Old 08-30-2007, 12:11 AM   PM User | #3
amylou111770
New Coder

 
Join Date: Aug 2007
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
amylou111770 is an unknown quantity at this point
Question

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.

Last edited by amylou111770; 08-30-2007 at 12:15 AM.. Reason: fixed that error
amylou111770 is offline   Reply With Quote
Old 08-30-2007, 12:25 AM   PM User | #4
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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.
Inigoesdr is offline   Reply With Quote
Old 08-30-2007, 12:32 AM   PM User | #5
amylou111770
New Coder

 
Join Date: Aug 2007
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
amylou111770 is an unknown quantity at this point
here is the code that does the sql query and shows the results with check boxes. the delete is a link

Code:
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
Code:
 td>
  		 <a href=\"index.php?id\">DELETE </a></td>
is there a tutitorial that shows how to delete this way with check boxes?
amylou111770 is offline   Reply With Quote
Old 08-30-2007, 02:17 AM   PM User | #6
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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.
PHP Code:
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).
PHP Code:
if(!empty($_POST['deleted_items']))
{
    foreach(
$_POST['deleted_items'] as $id => $on)
    {
        
$query 'DELETE FROM `table` WHERE `id` = ' $id;
        
// mysql_query()
    
}

Inigoesdr is offline   Reply With Quote
Old 08-30-2007, 02:27 AM   PM User | #7
amylou111770
New Coder

 
Join Date: Aug 2007
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
amylou111770 is an unknown quantity at this point
i tried that and its still telling me that no selection is made when i did.
amylou111770 is offline   Reply With Quote
Old 08-30-2007, 02:36 AM   PM User | #8
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Your inputs need to be wrapped in:
Code:
<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:
<?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.
Inigoesdr is offline   Reply With Quote
Old 08-30-2007, 03:04 AM   PM User | #9
amylou111770
New Coder

 
Join Date: Aug 2007
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
amylou111770 is an unknown quantity at this point
can i use this even though the delete code is on a different page then the code for the check boxes
amylou111770 is offline   Reply With Quote
Old 08-30-2007, 03:07 AM   PM User | #10
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Yes, as long as the delete page is set in the <form action=""
Inigoesdr is offline   Reply With Quote
Old 08-30-2007, 03:31 AM   PM User | #11
amylou111770
New Coder

 
Join Date: Aug 2007
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
amylou111770 is an unknown quantity at this point
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
amylou111770 is offline   Reply With Quote
Old 08-30-2007, 03:44 AM   PM User | #12
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Try echo'ing mysql_affected_rows() and $query:
PHP Code:
$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.
Inigoesdr is offline   Reply With Quote
Users who have thanked Inigoesdr for this post:
amylou111770 (08-30-2007)
Old 08-30-2007, 03:51 AM   PM User | #13
amylou111770
New Coder

 
Join Date: Aug 2007
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
amylou111770 is an unknown quantity at this point
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 is offline   Reply With Quote
Old 08-30-2007, 03:53 AM   PM User | #14
amylou111770
New Coder

 
Join Date: Aug 2007
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
amylou111770 is an unknown quantity at this point
and if i select all of them to delelte the
Quote:
requestId = 0
becomes a 3 and the records deleted stays the same
amylou111770 is offline   Reply With Quote
Old 08-30-2007, 04:12 AM   PM User | #15
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Ok, so $requestId isn't being set on the delete page. Did you make that loop I posted earlier? 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.
Inigoesdr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:29 AM.


Advertisement
Log in to turn off these ads.