0

I am having a big issue. This is the first time I sue a foreach and I do not even know if it's the right thing to use. I have a textarea where my members can add some text. They also have all the accounts where to send the posted text. Accounts are of two types F and T. They are shown as checkboxes. So when a member types "submit" the text should be INSERTED in a specific table for EACH of the selected accounts. I thought php foreach was the right thing. But I am not sure anymore. Please take in mind I do not know anything about foreach and arrays. So please when helping me, consider to provide the modded code =D . Thank you so much!

<?php
require_once('dbconnection.php');
$MembID = (int)$_COOKIE['Loggedin'];
?>
<form action="" method="post">
    <p align="center"><textarea id="countable1" name="addit" cols="48" rows="10" style="border-color: #ccc; border-style: solid;"></textarea>
    <br>
<?
$DB = new DBConfig();
$DB -> config();
$DB -> conn();
$on="on";
    $queryF ="SELECT * FROM `TableF` WHERE `Active`='$on' AND `memberID`='$MembID' ORDER BY ID ASC";
    $result=mysql_query($queryF) or die("Errore select from TableF: ".mysql_error());
$count = mysql_num_rows($result); 
if ($count > 0)
    {
    while($row = mysql_fetch_array($result)) {
    ?><div style="width:400px; height:100px;margin-bottom:50px;"><?
    $rowid = $row['ID'];
    echo $row['Name'] . '</br>';
    $checkit = "checked";
    if ($row['Main'] == "")
    $checkit = "";
    if ($row['Locale'] =="")
    $row['Locale'] ="None" . '</br>';
    echo $row['Locale'] . '</br>';
    if ($row['Link'] =="")
    $row['Link'] ="javaScript:void(0);";
    ?>
    <!-- HERE WE HAVE THE "F" CHECKBOXES. $rowid SHOULD BE TAKEN AND INSERTED IN THE FOREACH BELOW FOR ANY SELECTED CHECKBOX IN THE FIELD "Type".SEE BELOW -->
    <input type="checkbox" name="f[<?php echo $rowid?>]" <?php echo $checkit;?>>
    </div>
    <?
}//END WHILE MYSQL
}else{
echo "you do not have any Active account";
}
$DB = new DBConfig();
$DB -> config();
$DB -> conn();
    $queryTW ="SELECT * FROM `TableT` WHERE `Active`='$on' AND `memberID`='$MembID' ORDER BY ID ASC";
    $result=mysql_query($queryTW) or die("Errore select TableT: ".mysql_error());
$count = mysql_num_rows($result); 
if ($count > 0)
    {
    while($row = mysql_fetch_array($result)) {
    ?><div style="width:400px; height:100px;margin-bottom:50px;"><?
    $rowid = $row['ID'];
    echo $row['Name'] . '</br>';
    $checkit = "checked";
    if ($row['Main'] == "")
    $checkit = "";
    if ($row['Locale'] =="")
    $row['Locale'] ="None" . '</br>';
    echo $row['Locale'] . '</br>';
    if ($row['Link'] =="")
    $row['Link'] ="javaScript:void(0);";
    ?>
        <!-- HERE WE HAVE THE "T" CHECKBOXES. $rowid SHOULD BE TAKEN AND INSERTED IN THE FOREACH BELOW FOR ANY SELECTED CHECKBOX IN THE FIELD "Type".SEE BELOW -->
    <input type="checkbox"  name="t[<?php echo $rowid?>]" <?php echo $checkit;?> >
    </div>
    <?
}//END 2° WHILE MYSQL
}else{
echo "you do not have any Active account";
}
?>
<input type="submit" value="submit">
</form>
<?
//WHEN CHECKBOXES "F" ARE FOUND, FOR EACH CHECKBOX IT SHOULD INSERT INTO TableG THE VALUES BELOW, FOR EACH SELECTED "F" CHECKBOX
if(!empty($_POST['addit']) && !empty($_POST['f'])){
$thispostF = $_POST['f'];
$f="F";
foreach ($thispostF as $valF)
//THE MOST IMPORTANT FIELD HERE IS "Type". THE ARRAY INSERT "on", I NEED INSTEAD THE VALUE $rowid AS ABOVE
$queryaddF="INSERT INTO TableG (ID,memberID,Type,IDAccount,Tuitting) VALUES (NULL,".$MembID.",'".$f."','".$valF."', '".$_POST['addit']."')";
$resultaddF=mysql_query($queryaddF) or die("Errore insert G: ".mysql_error());
}
//WHEN CHECKBOXES "T" ARE FOUND, FOR EACH CHECKBOX IT SHOULD INSERT INTO TableG THE VALUES BELOW, FOR EACH SELECTED "T" CHECKBOX
if(!empty($_POST['addit']) && !empty($_POST['t'])){
$thispostT = $_POST['t'];
$t="T";
foreach ($thispostT as $valF)
//THE MOST IMPORTANT VALUE HERE IS "Type". THE ARRAY GIVES "on", I NEED INSTEAD THE VALUE $rowid AS ABOVE
$queryaddT="INSERT INTO TableG (ID,memberID,Type,IDAccount,Tuitting) VALUES (NULL,".$MembID.",'".$t."','".$valF."', '".$_POST['addit']."')";
$resultaddF=mysql_query($queryaddF) or die("Errore insert G: ".mysql_error());
}
?>
8
  • 2
    Looking at the code you posted, do you think it's easy for people here to read it and understand it without you using formatting tools given to us by good creators of Stack Overflow? ;) Commented May 3, 2011 at 16:14
  • 3
    "I do not know anything about foreach and arrays" I would probably start here Commented May 3, 2011 at 16:19
  • Sorry guys but I do not understand how to use the Stack Overflow tools for the code. Sorry about that. Any help with my problem instead? I would like to know how to use this foreach, but I am not a PHP programmer, so this is difficult for me. Help please! =D Commented May 3, 2011 at 16:24
  • 1
    You really should at least learn how to create functions , and apply procedural programming methods to your code ( OOP here is a unachievable dream ). Right now the architecture you have is called "big ball of mud". Only a masochist ( or a wordpress developer ) would be happy to debug this. Commented May 3, 2011 at 16:28
  • What does the generated html look like? Commented May 3, 2011 at 16:31

3 Answers 3

6
 foreach ($thispostT as $valF)
{

$queryaddT="INSERT INTO TableG (ID,memberID,Type,IDAccount,Tuitting) VALUES (NULL,".$MembID.",'".$t."','".$valF."', '".$_POST['addit']."')";
$resultaddF=mysql_query($queryaddF) or die("Errore insert G: ".mysql_error());
}

please put start and ending bracket to your foreach loop and try i have not read the whole code but just found you missing the brackets. hope that helps you.

Sign up to request clarification or add additional context in comments.

Comments

1

I think I know what you're doing...

You're going to need to do:

foreach($_POST as $key => $value) {
   $type = substr($key,0,1);
   $id = substr($key,1);
   if($type == 't') {
      // do insert for t table here
   } else if($type == 'f') {
      // do insert for f table here
   }
}

I didn't test it but it should be something like this.

Comments

0

My suggestion is

create field name as t[] (array) onchecked value will be passed on the next page

the form checkbox field should be like that

<input type="checkbox"  name="t[]" value="< ?php echo $rowid?>" <?php echo $checkit;? > >

and when you Submit the form

GET THE VALUE and insert in to database;
< ?
if($_POST['t'])
{
foreach($_POST['t'] as $v)
{
queryaddT="INSERT INTO TableG (ID,memberID,Type,IDAccount,Tuitting) VALUES (NULL,".$MembID.",'".$t."','".$valF."', '".$_POST['addit']."')";
$resultaddF=mysql_query($queryaddF) or die("Errore insert G: ".mysql_error());


}

}
? >

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.