Current location: Hot Scripts Forums » Programming Languages » PHP » two questions....deleting mysql values and adding array value through forms


two questions....deleting mysql values and adding array value through forms

Reply
  #1 (permalink)  
Old 03-13-04, 10:49 PM
mlbpa2 mlbpa2 is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
two questions....deleting mysql values and adding array value through forms

ok first thing is on my main page news update id like to make it so i have a page that displays each news thing with a checkbox next to it so i can delete it. Also for the array addition id like to have a text box thing so it adds to this array.

$sections = array("Section1", "Section2", "Section3");
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-14-04, 01:09 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
This would be the script that builds the form. Use remove[] to build the array $remove:
PHP Code:

<form action="remove.php" method="post">


<?php

$query 
mysql_query("SELECT id,name FROM table");
    while (
$rows mysql_fetch_assoc($query))
    echo 
"<input type=\"checkbox\" name=\"remove[]\" value=\"" $rows['id'] . "\" /> " $rows['name'] . "<br />\n";

?>

<input type="submit" name="submit" value="Remove">
</form>
Then in the processing file "remove.php":
PHP Code:

<?php


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

    foreach (
$_POST['remove'] as $id) {
    
mysql_query("DELETE FROM table WHERE id = '" $id "'");

echo 
"Selected items have been removed.<br />";

}
else
echo 
"No items were selected.<br />";

?>
I think that's what you're aiming for, hope it helps.

Last edited by Keith; 03-14-04 at 01:25 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-14-04, 09:18 AM
mlbpa2 mlbpa2 is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
yeah thats exactly what i need but im getting a parse error on the else line.

found the error you were missing a }

Last edited by mlbpa2; 03-14-04 at 09:32 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-14-04, 03:17 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Yeah, whoops... take out the { after the foreach(). I didn't forget one, just stuck one where it shouldn't be

remove.php:
PHP Code:

 <?php


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

    foreach (
$_POST['remove'] as $id)
    
mysql_query("DELETE FROM table WHERE id = '" $id "'");

echo 
"Selected items have been removed.<br />";

}
else
echo 
"No items were selected.<br />";

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-17-04, 03:56 PM
ciggie ciggie is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
I am new to PHP and have been trying to get this feature to work as well by using your code Keith. But everytime I try it I get the no items selected return. Any explanations of what I am doing wrong would be much appreciated:

<form action="removeteach.php" method="post">
<?php
...
while ($get_info = mysql_fetch_row($result)) {
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><input type=\"checkbox\" name=\"remove[]\" value=\"" . $rows['teachid'] . "\" /></td>\n";
print "\t<td>$field</td>\n";
...
?>

removeteach.php file
<?php

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

foreach ($_POST['remove'] as $teachid)
mysql_query("DELETE FROM teacherid WHERE teachid = '" . $teachid . "'") or die ('Can\'t delete : ' .

mysql_error());

}
else
echo "No items selected";

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 03-17-04, 06:09 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
What version of PHP are you running? In removeteach.php, try changing all occurences of
$_POST['remove']
to
$HTTP_POST_VARS['remove']
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-17-04, 08:11 PM
ciggie ciggie is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Got the same result as last time, where it says No Items Selected. I am running php 4.3.3.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 03-18-04, 01:00 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Aha, I see what you have wrong...
PHP Code:

<form action="removeteach.php" method="post">

<?php
...
while (
$get_info mysql_fetch_row($result)) {
print 
"<tr>\n";
foreach (
$get_info as $field)
print 
"\t<td><input type=\"checkbox\" name=\"remove[]\" value=\"" $rows['teachid'] . "\" /></td>\n";
print 
"\t<td>$field</td>\n";
...
?>
change $rows['teachid'] to $get_info['teachid']

Last edited by Keith; 03-18-04 at 01:02 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 03-18-04, 09:56 AM
ciggie ciggie is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Still a no go Keith. Thank you for your continuing help though, any other ideas?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 03-18-04, 10:06 AM
Infinite_Hackers's Avatar
Infinite_Hackers Infinite_Hackers is offline
Coding Addict
 
Join Date: Dec 2003
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
If the script below doesn't work, could you please show us your $result and the script you use to connect to mysql ;D


PHP Code:

<form action="removeteach.php" method="post">

<?php
...
while (
$row mysql_fetch_row($result)) {
         
$get_info[] = $row;
}
          
print 
"<tr>\n";
foreach (
$get_info as $field) {
print 
"\t<td><input type=\"checkbox\" name=\"remove[]\" value=\"" $field['teachid'] . "\" /></td>\n";
print 
"\t<td> What goes here ?? we need some variable.. you can't put ' field'</td>\n";

...
?>
What goes here ?? we need some variable.. you can't put ' field'
you cannot put $field there... you need $field['some row name']

Last edited by Infinite_Hackers; 03-18-04 at 10:11 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Uploaded Name To Mysql CarBoffin PHP 0 02-27-04 04:32 PM
Problem with adding content to mysql from php HasansWeb PHP 3 01-10-04 06:26 PM


All times are GMT -5. The time now is 11:04 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.