PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old August 12th, 2002, 10:05 PM
alifyag alifyag is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 16 alifyag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
checkboxes (URGENT)

hi,
its on checkboxes again...i want to accept multiple values from the checkboxes and insert in in the table..i tried the $_POST and array as suggested in the forum but i got the following error.

Warning: Invalid argument supplied for foreach() in /home/alifya/public_html/wedprac/edit.php on line 26

someone please help me...i am posting my entire code

<?
// This page will handle both creation of new records, and update
// of existing records.
include "connection.php";
// Here is where you process EDIT/UPDATE commands.
if ($update)
{
$query = "UPDATE users SET firstName="$firstName", lastName="$lastName", dateModified=now(),
primaryEmail="$primaryEmail", idValue="$idValue", idType="$idType" where ID=$ID;";
$res = db_query($query);

foreach($_POST['groupID'] as $value) {
$query5="INSERT into users_in_groups values ($ID, $value);";
$res5 = db_query($query5);

}

//header("Location: view.php?ID=$ID&feedbackCode=3");
exit();
}
//execute sql command to update the id (remember that we get the ID from the hidden variable
else if ($add)
{
$query = "INSERT into users(firstName,lastName, dateCreated, idValue, primaryEmail, idType) values
("$firstName", "$lastName", now(), "$idValue", "$primaryEmail", "$idType" );";
$res = db_query($query);

$lastID = mysql_insert_id();
header("Location: view.php?ID=$lastID&feedbackCode=2");
exit();
}



$new=false;
if ($ID) {
$query = "SELECT firstName, lastName,primaryEmail, dateCreated, dateModified, idValue, idType
FROM users WHERE ID=$ID;";
$res = db_query($query);

$query1="SELECT userID, ID, groupID from users, users_in_groups where userID=ID and ID=$ID;";
$res1 = db_query($query1);

while ($row=db_fetch_array($res)) {
extract($row);
while ($row1=db_fetch_array($res1)) {
extract($row1);
echo $groupID;
if ($groupID==1){
$gflag="checked";}
else if ($groupID==2){
$gflag2="checked";}
else if ($groupID==3){
$gflag3="checked";}
else
{$flag="";}

}
}

} else {
$new=true;
}


?>

<H1><?=($new==true?"Create new record":"Editing record $ID")?></H1>

<FORM action="<?=$PHP_SELF?>" method="get">

<!-- here, send a hidden variable with the ID (if it's not a new record)-->
<INPUT type="hidden" name="ID" value="<?=(($new==true)?"":$ID)?>">
<INPUT type="hidden" name="idType" value="<?=(($new==true)?"":$idType)?>">
First Name : <INPUT type="text" name="firstName" value="<?=(($new==true)?"Enter new First Name ":$firstName)?>"><BR><BR>
Last Name : <INPUT type="text" name="lastName" value="<?=(($new==true)?"Enter new Last Name ":$lastName)?>"><BR><BR>
Primary Email : <INPUT type="text" name="primaryEmail" value="<?=(($new==true)?"Enter new Email Address ":$primaryEmail)?>"><BR><BR>
ID Number : <INPUT type="text" name="idValue" maxlength="9" size="9" value="<?=(($new==true)?"Enter new ID ":$idValue)?>"><BR><BR>

<?
if ($idType==1){
$flag="checked";}
else if ($idType==2){
$flag2="checked";}
else if ($idType==3){
$flag3="checked";}
else
{$flag="";}

?>

ID TYPE : <INPUT type="radio" name="idType" value="1" <? echo $flag;?>>BRUINID
<INPUT type="radio" name="idType" value="2" <? echo $flag2;?>>PASSPORT
<INPUT type="radio" name="idType" value="3" <? echo $flag3;?>>DRIVERS LICENSE
<BR><BR>


GROUP TYPE : <INPUT type="checkbox" name="groupID[]" value="1" <? echo $gflag;?>>STUDENT
<INPUT type="checkbox" name="groupID[]" value="2" <? echo $gflag2;?>>PROFESSOR
<INPUT type="checkbox" name="groupID[]" value="3" <? echo $gflag3;?>>VISITING FACULTY

<br><br>
<input type="submit" name="<?=($new==true?"add":"update")?>" value="<?=($new==true?"Add new record":"Update this record")?>">
</FORM>
<FORM action="search.php" method="get">
<INPUT type="submit" name= "RetSearch" value="Return to Search Page">
</FORM>




Reply With Quote
  #2  
Old August 12th, 2002, 10:15 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 3
RE: checkboxes (URGENT)

Checkboxes only have a value if they are checked, so if none of the groupID[] checkboxes are checked, $_POST['groupID'] will be NULL and not an array.

Reply With Quote
  #3  
Old August 12th, 2002, 11:11 PM
alifyag alifyag is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 16 alifyag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: checkboxes (URGENT)

hello,
but they are checked....i still get the error
alifya

Reply With Quote
  #4  
Old August 13th, 2002, 12:31 AM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: checkboxes (URGENT)

again, see this tutorial...

www.phpbuilder.com/columns/laflamme20001016.php3

Reply With Quote
  #5  
Old August 13th, 2002, 05:42 AM
alifyag alifyag is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 16 alifyag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: checkboxes (URGENT)

hi zombie,
i tried the tutorial..it gives me an error at foreach...dunno why???

Reply With Quote
  #6  
Old August 14th, 2002, 12:00 AM
alifyag alifyag is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 16 alifyag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: checkboxes (URGENT) thanks codegirl

hi code,
it worked great!! however it works only for EDIT...
when i use the same technique for adding a fresh one it does not work...
what should i do??
thanks

Reply With Quote
  #7  
Old August 14th, 2002, 05:35 AM
alamsie alamsie is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 13 alamsie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: checkboxes (URGENT)

hi,
even i am havign the same problem.
was trying the tutorial and checkboxes and dont know how to get add to work

Reply With Quote
  #8  
Old August 14th, 2002, 10:06 PM
alifyag alifyag is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 16 alifyag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: checkboxes (URGENT)

hi,
heres the users table defination:
CREATE TABLE `users` (
`ID` int(11) NOT NULL auto_increment,
`username` varchar(16) NOT NULL default '',
`password` varchar(32) NOT NULL default '',
`firstName` varchar(255) NOT NULL default '',
`lastName` varchar(255) NOT NULL default '',
`primaryEmail` varchar(255) NOT NULL default '',
`dateCreated` datetime NOT NULL default '0000-00-00 00:00:00',
`dateModified` datetime NOT NULL default '0000-00-00 00:00:00',
`idType` int(11) NOT NULL default '0',
`idValue` varchar(64) NOT NULL default '',
PRIMARY KEY (`ID`)
) TYPE=MyISAM

i have another table called users_in_groups which takes in the ID of the user and also the groupID as the professor or student...as i told you...it worked great for edit...but when i add a user as a professor using the same functionality as edit it does not do it.
thanks

Reply With Quote
  #9  
Old August 20th, 2002, 03:37 AM
alifyag alifyag is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 16 alifyag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: checkboxes (URGENT)

i am still stuck. just cant gt add to work....any solutions

Reply With Quote
  #10  
Old August 20th, 2002, 05:49 AM
alifyag alifyag is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 16 alifyag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: checkboxes (URGENT)

the problem is the insert works fine with the other fields...but when i insert into the checkboxes it doesnt get inserted but when i edit the same record it works again.
the following is the code i am using. Edit works great but add doesnt.
<?
// This page will handle both creation of new records, and update
// of existing records.
include "connection.php";
// Here is where you process EDIT/UPDATE commands.
if ($update)
{
$query = "UPDATE users SET firstName="$firstName", lastName="$lastName", dateModified=now(),
primaryEmail="$primaryEmail", idValue="$idValue", idType="$idType" where ID=$ID;";
$res = db_query($query);
$query2= "DELETE FROM users_in_groups where userID=$ID;";
$res2 = db_query($query2);

if (is_array($_POST['groupID'])) {
foreach($_POST['groupID'] as $value) {
$query6="INSERT into users_in_groups values ($ID, $value);";
$res6 = db_query($query6);

}
}
header("Location: view.php?ID=$ID&feedbackCode=3");
exit();
}
//execute sql command to update the id (remember that we get the ID from the hidden variable
else if ($add)
{
$query = "INSERT into users(firstName,lastName, dateCreated, idValue, primaryEmail, idType) values
("$firstName", "$lastName", now(), "$idValue", "$primaryEmail", "$idType" );";
$res = db_query($query);

if (is_array($_POST['groupID'])) {
foreach($_POST['groupID'] as $value) {
$query7="INSERT into users_in_groups values ($ID, $value);";
$res7 = db_query($query7);

}
}

$lastID = mysql_insert_id();
header("Location: view.php?ID=$lastID&feedbackCode=2");
exit();
}


$new=false;
if ($ID) {
$query = "SELECT firstName, lastName,primaryEmail, dateCreated, dateModified, idValue, idType
FROM users WHERE ID=$ID;";
$res = db_query($query);

$query1="SELECT userID, ID, groupID from users, users_in_groups where userID=ID and ID=$ID;";
$res1 = db_query($query1);

while ($row=db_fetch_array($res)) {
extract($row);
while ($row1=db_fetch_array($res1)) {
extract($row1);
echo $groupID;
if ($groupID==1){
$gflag="checked";}
else if ($groupID==2){
$gflag2="checked";}
else if ($groupID==3){
$gflag3="checked";}
else
{$flag="";}

}
}

} else {
$new=true;
}


?>

<H1><?=($new==true?"Create new record":"Editing record $ID")?></H1>

<FORM action="<?=$PHP_SELF?>" method="POST">

<!-- here, send a hidden variable with the ID (if it's not a new record)-->
<INPUT type="hidden" name="ID" value="<?=(($new==true)?"":$ID)?>">
<INPUT type="hidden" name="idType" value="<?=(($new==true)?"":$idType)?>">
First Name : <INPUT type="text" name="firstName" value="<?=(($new==true)?"Enter new First Name ":$firstName)?>"><BR><BR>
Last Name : <INPUT type="text" name="lastName" value="<?=(($new==true)?"Enter new Last Name ":$lastName)?>"><BR><BR>
Primary Email : <INPUT type="text" name="primaryEmail" value="<?=(($new==true)?"Enter new Email Address ":$primaryEmail)?>"><BR><BR>
ID Number : <INPUT type="text" name="idValue" maxlength="9" size="9" value="<?=(($new==true)?"Enter new ID ":$idValue)?>"><BR><BR>

<?
if ($idType==1){
$flag="checked";}
else if ($idType==2){
$flag2="checked";}
else if ($idType==3){
$flag3="checked";}
else
{$flag="";}

?>

ID TYPE : <INPUT type="radio" name="idType" value="1" <? echo $flag;?>>BRUINID
<INPUT type="radio" name="idType" value="2" <? echo $flag2;?>>PASSPORT
<INPUT type="radio" name="idType" value="3" <? echo $flag3;?>>DRIVERS LICENSE
<BR><BR>


GROUP TYPE : <INPUT type="checkbox" name="groupID[]" value="1" <? echo $gflag;?>>STUDENT
<INPUT type="checkbox" name="groupID[]" value="2" <? echo $gflag2;?>>PROFESSOR
<INPUT type="checkbox" name="groupID[]" value="3" <? echo $gflag3;?>>VISITING FACULTY

<br><br>
<input type="submit" name="<?=($new==true?"add":"update")?>" value="<?=($new==true?"Add new record":"Update this record")?>">
</FORM>
<FORM action="search.php" method="get">
<INPUT type="submit" name= "RetSearch" value="Return to Search Page">
</FORM>




Reply With Quote
  #11  
Old August 20th, 2002, 05:51 AM
alifyag alifyag is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 16 alifyag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: checkboxes (URGENT)

What i mean is when i update the record the checkbox as well as th eother fields work perfect but when i add only the other fields insert statement works ...the checkbox thing doesnt even thought i am using the same code as in edit
thanks in advance...i am really stuck at this one

Reply With Quote
  #12  
Old August 20th, 2002, 07:48 AM
bakertrg's Avatar
bakertrg bakertrg is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Scottsdale AZ, US
Posts: 2,253 bakertrg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 45 sec
Reputation Power: 4
Send a message via Yahoo to bakertrg
RE: checkboxes (URGENT)

it could be not working because of the checkbox not showing up when not checked. If you check every box and insert does it work?

B

Reply With Quote
  #13  
Old August 20th, 2002, 11:22 PM
alifyag alifyag is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 16 alifyag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: checkboxes (URGENT)

even when i check every checkbox and add..it doesnt get added....
however edit works...like if there was one checkbox already checked and i uncheck that and check another one..then it works fine

Reply With Quote
  #14  
Old August 20th, 2002, 11:46 PM
bakertrg's Avatar
bakertrg bakertrg is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Scottsdale AZ, US
Posts: 2,253 bakertrg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 45 sec
Reputation Power: 4
Send a message via Yahoo to bakertrg
RE: checkboxes (URGENT)

echo your submit value so you can see what you are passing and also place an echo inside each if to check if you are actually reaching the appropriate section.

B

Reply With Quote
  #15  
Old August 22nd, 2002, 01:12 AM
alifyag alifyag is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 16 alifyag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: checkboxes (URGENT)

yes..all sections r workign fine...except the checkbox multiple insert in add

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > checkboxes (URGENT)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread: