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 05-30-2012, 04:03 PM   PM User | #1
AzzyDude24601
New Coder

 
Join Date: Apr 2012
Posts: 59
Thanks: 12
Thanked 0 Times in 0 Posts
AzzyDude24601 is an unknown quantity at this point
Not sure what type of loop to use

Hi everyone, I've got this code set up where basically it 'while loops' out this list of people in a database (first name, second name, etc.) and the idea is that you can organize these people into groups.

So this translates to having a form field for whatever you want to call the group, then you tick the boxes next to each person you want in this new group. And when you click SAVE, it updates the "group" column for each of those people in the database with the new group name.

So I think I understand how to do this in theory. This is the PHP MySQL I have written so far:

mysql_query("UPDATE `Participant` SET `group`='".$_POST["group"]."' WHERE `idParticipant`='".$_POST["check"]."';");

So I believe I need some kind of loop so that it runs this query for each POST'd idParticipant so it updates each one in turn. I'm just not sure which loop to use and what the syntax would look like. I'm gonna continue researching it online but if you have any advice, please let me know.
AzzyDude24601 is offline   Reply With Quote
Old 05-30-2012, 04:07 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
No don't loop it like this; limit SQL to as few requests as you possibly can. In the case of an UPDATE, you can use an IN clause for the WHERE condition.
What is the $_POST['group'] to $_POST['check'] relationship? As in, it sounds like you have many perticipant's available, and potentially many groups, so what does that look like structurally from your HTML?
Fou-Lu is offline   Reply With Quote
Old 05-30-2012, 04:13 PM   PM User | #3
AzzyDude24601
New Coder

 
Join Date: Apr 2012
Posts: 59
Thanks: 12
Thanked 0 Times in 0 Posts
AzzyDude24601 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
No don't loop it like this; limit SQL to as few requests as you possibly can. In the case of an UPDATE, you can use an IN clause for the WHERE condition.
What is the $_POST['group'] to $_POST['check'] relationship? As in, it sounds like you have many perticipant's available, and potentially many groups, so what does that look like structurally from your HTML?
This is the body of my HTML:

<body>

<form name="myform" id="myform" action="" method="POST">

<label for="group" id="group_label">Group Name: </label>
<input type="text" name="group" maxlength="45" id="group" value=""/>
<br><br>

Member List:<br><br>

<?php

$query = mysql_query('SELECT * FROM `Participant`');
if (!$query)
{
die('Query failure: '.mysql_error());
}

while($rows = mysql_fetch_array($query)):

$id = $rows['idParticipant'];
$fname = $rows['firstName'];
$sname = $rows['secondName'];
$facility = $rows['facility'];

echo "<input type='checkbox' name='check' id='$id' value='$id' /><br>";
echo "First Name: $fname<br>";
echo "Second Name: $sname<br>";
echo "Facility: $facility<br><br>";

endwhile;

?>

<input type="submit" name="submit" value="Save New Group">

</form>

<form>

<input type="button" value="Back" onclick="window.location.href='home.php'">

</form>

</body>

Not sure what you mean by the relationship between $_POST['group'] and $_POST['check']? We need the $_POST['group'] so we know what to insert into the group column of the chosen participants in the list. And we need the $_POST['check'] so we know which participants need to be updated. That make sense?
AzzyDude24601 is offline   Reply With Quote
Old 05-30-2012, 04:28 PM   PM User | #4
AzzyDude24601
New Coder

 
Join Date: Apr 2012
Posts: 59
Thanks: 12
Thanked 0 Times in 0 Posts
AzzyDude24601 is an unknown quantity at this point
Maybe I've got this all wrong in my head >_< ...what's the RIGHT way to do this?
AzzyDude24601 is offline   Reply With Quote
Old 05-30-2012, 04:46 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
No this is sufficient. The relationship is many participants to one group from your form.
The query will be very simple. Use the checkbox array to create an IN clause:
PHP Code:
if (isset($_POST['check']) && count($_POST['check']) > 0)
{
    
$sGroup mysql_real_escape_string($_POST['group']);    
    
$aParticipants array_map('mysql_real_escape_string'$_POST['check']);
    
$sIn implode("', '"$aParticipants);
    
$sQry "UPDATE Participant SET group = '$sGroup' WHERE idParticipant IN ('$sIn')";
    
mysql_query($sQry);

You may want to comment out that query and print the $sQry to verify that it looks correct first. But that looks like it will work.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
AzzyDude24601 (05-30-2012)
Old 05-30-2012, 04:55 PM   PM User | #6
AzzyDude24601
New Coder

 
Join Date: Apr 2012
Posts: 59
Thanks: 12
Thanked 0 Times in 0 Posts
AzzyDude24601 is an unknown quantity at this point
Hey thanks for getting back to me. My PHP knowledge is quite inferior to yours so I apologize that I can't debug this myself. The following two lines returned the following errors respectively:

$aParticipants = array_map('mysql_real_escape_string', $_POST['check']);
$sIn = implode("', '", $aParticipants);

Warning: array_map() [function.array-map]: Argument #2 should be an array in C:\wamp\www\CURRENT\process_group.php on line 8
Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\CURRENT\process_group.php on line 9

As I said you know more than me...but I think the problem is it's expecting an array but all it's getting is some POST data. Would we not have to convert all the POST data into an array first...?

Last edited by AzzyDude24601; 05-30-2012 at 05:43 PM..
AzzyDude24601 is offline   Reply With Quote
Old 05-30-2012, 05:46 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Change the HTML for the input name to 'check[]'. That should fix that.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
AzzyDude24601 (05-30-2012)
Old 05-30-2012, 06:14 PM   PM User | #8
AzzyDude24601
New Coder

 
Join Date: Apr 2012
Posts: 59
Thanks: 12
Thanked 0 Times in 0 Posts
AzzyDude24601 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
Change the HTML for the input name to 'check[]'. That should fix that.
Yeah, that did the trick! But now it doesn't seem to like our MySQL syntax. It said the following:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group = 'test123' WHERE idParticipant IN ('4', '7')' at line 1

This is good news; I think we're super close. However I don't have much experience using the IN clause. Can you find a mistake there?
AzzyDude24601 is offline   Reply With Quote
Old 05-30-2012, 06:18 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Did I spell the property names correctly?
Its also possible that your SQL is strict. Since there are numbers, they should be treated as such:
PHP Code:
    $sIn implode(", "$aParticipants);
    
$sQry "UPDATE Participant SET group = '$sGroup' WHERE idParticipant IN ($sIn)";
    
mysql_query($sQry) or die(mysql_error()); 
Edit:
Lol, the problem is that GROUP is a reserved word (ie: GROUP BY). Add backticks around the group word.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
AzzyDude24601 (05-30-2012)
Old 05-30-2012, 06:26 PM   PM User | #10
AzzyDude24601
New Coder

 
Join Date: Apr 2012
Posts: 59
Thanks: 12
Thanked 0 Times in 0 Posts
AzzyDude24601 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
Edit:
Lol, the problem is that GROUP is a reserved word (ie: GROUP BY). Add backticks around the group word.
LOL, yes, that was it. Perfect. Thanks for all your help! ^_^
AzzyDude24601 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 03:46 PM.


Advertisement
Log in to turn off these ads.