Search
Left Quote    Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: FOREACH holds you back untill you master it!
  This user is offline  b321618
  Subject: "FOREACH holds you back untill you m..." Posted: @ 9:00 pm on Feb 15 2010   
Member #: 946
Rank: User - (7)
Since: 05/28/09
Posts: 7

So this code works fine in all but one spot, if you look below you see Im deleting some information from some database and then im inserting some information to another. $addmember is a id, which is an array. now since the delete portion of it uses the $addmember array, it of course stops at whatever checkbox I selected to be deleted and it deletes. But I cant see how I can use that id to insert a new member in the second half of the action. It just loops through the code and does the insert on the rest of the array instead of stopping on whatever I have checked...


Code:

if (isset($_POST[' add']))
{
if($_POST['id'] == "") {

}
else
{
foreach ($_POST['id'] as $addmember)
{
if (!mysql_query(& quot;DELETE FROM memberrequest WHERE id='$addmember' ") || !mysql_query(&q uot;INSERT INTO members VALUES ('','".$_P OST['$smplnm']. "','" .$_POST['$user' ]."','&quo t;.$_POST['$pro ject']."', '1','$year')&qu ot;))
{
echo mysql_error();< br /> }
}
}
}




    Viewed: 10,801 Times | Reply to This | To top
  This user is offline  b321618
  Subject: "re: FOREACH holds you back untill y..." Posted: @ 9:16 pm on Feb 15 2010    
Member #: 946
Rank: User - (7)
Since: 05/28/09
Posts: 7

thanks for reading if you need more information let me know, ill be actively checking this post throughout the day!




    Viewed: 10,795 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: FOREACH holds you back untill y..." Posted: @ 12:16 am on Feb 16 2010    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA




-bs0d | AllSyntax.com

    Viewed: 10,789 Times | Reply to This | To top
  This user is offline  b321618
  Subject: "re: FOREACH holds you back untill y..." Posted: @ 12:25 am on Feb 16 2010    
Member #: 946
Rank: User - (7)
Since: 05/28/09
Posts: 7

ok let me give you some more code here. paint a better picture sorry.
this is the whole section, including what was above.
Thanks again bs0d!


Code:


if ($_GET['reques t'] == 'request')
{


?>
<center>< br /> <form name="add box" action=" ".$_SERVER ['PHP_SELF']. "" method="P OST">< br /> <input type='submit' name='add' value='Accept ' />
<input type='submit' name='deny'va lue='Deny'/&g t;
<input type='submit' name='block'v alue='Block'/ >
</center>
<?

$procheck = mysql_query(&q uot;SELECT * FROM members WHERE user='$getuser name' AND level='5'&qu ot;);
while($prodispl ay = mysql_fetch_arr ay($procheck))< br /> {
echo "<cent er><a href='project. php?id=". $prodisplay['s implename'].& quot;'>&qu ot;.$prodisplay ['project']. "</a> ;<br>< /center>&qu ot;;

$requestproname = $prodisplay['s implename']; $reqprocheck = mysql_query(&q uot;SELECT * FROM memberrequest WHERE simplename='$r equestproname' AND block='0'&qu ot;);
$rowcount = mysql_num_rows( $reqprocheck);< br /> if($rowcount == 0)
{
echo "<cent er>No requests</ce nter><br& gt;";
}
else
while($reqdispl ay = mysql_fetch_arr ay($reqprocheck ))
{

$addmember = $reqdisplay['i d'];
$smplnm = $reqdisplay['s implename']; $user = $reqdisplay['u ser'];
$proj = $reqdisplay['p roject'];
echo "<cent er>".$ reqdisplay['us er']."&l t;/center>&l t;br>" ;

?>
<center>< br /> <input type='checkbox ' name='id[]' value='<? echo $addmember; ?>'>< /input>
</center>

if (isset($_POST[ 'add']))
{
if($_POST['id '] == "")
{

}
else
{
foreach ($_POST['id'] as $addmember)
{
if (!mysql_query( "DELETE FROM memberrequest WHERE id='$addmember '") || !mysql_query(& quot;INSERT INTO members VALUES ('','" .$_POST['$smpl nm']."' ,'".$_PO ST['$user']. "','&qu ot;.$_POST['$p roject'].&quo t;','1','$y ear')"))
{
echo mysql_error();< br /> }
}
}
}
<?




    Viewed: 10,786 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: FOREACH holds you back untill y..." Posted: @ 8:29 pm on Feb 16 2010    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

I wonder if you tried using a different variable name in the foreach statement? Since $addmember is defined earlier as: $addmember = $reqdisplay['i d']; Also try IF(isset()) with $_POST['id'] instead of that if condition...Is the while() loop displaying a checkbox for each member? Is it checked by default?




-bs0d | AllSyntax.com

    Viewed: 10,773 Times | Reply to This | To top
  This user is offline  b321618
  Subject: "re: FOREACH holds you back untill y..." Posted: @ 9:21 pm on Feb 16 2010    
Member #: 946
Rank: User - (7)
Since: 05/28/09
Posts: 7

yes its displaying a check box for each memberrequest, and no they aren't check. It interesting how it will delete the row that is checked. and insert into members, but with the insert into members it just goes through the loop inserting every request. But only deletes the one row because I can tell it which one to delete specifically.




    Viewed: 10,769 Times | Reply to This | To top
  This user is offline  b321618
  Subject: "re: FOREACH holds you back untill y..." Posted: @ 9:53 pm on Feb 16 2010    
Member #: 946
Rank: User - (7)
Since: 05/28/09
Posts: 7

oop! I figured it out! I had to add another while loop to fill in the INSERT section using the id from the memberrequest! Its always something so simple! Thanks bs0d for you time and help! couldn't have done it without you!




    Viewed: 10,767 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: FOREACH holds you back untill y..." Posted: @ 11:40 pm on Feb 16 2010    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Ok, sorry I couldn't be more help. Logical errors can be tricky to debug. Good luck with your script




-bs0d | AllSyntax.com

    Viewed: 10,761 Times | Reply to This | To top
Viewing Page: 1 of 1


1 |

You must be logged in to post on the forums. Login or Register








"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank