使用mySQL INSERT INTO的PHP Foreach循环问题

时间:2011-05-03 16:13:38

标签: php loops foreach

我有一个大问题。 这是我第一次起诉foreach,我甚至不知道这是否正确使用。 我有一个textarea,我的成员可以添加一些文本。 他们还拥有发送已发布文本的所有帐户。 帐户有两种类型F和T. 它们显示为复选框。 因此,当成员键入“提交”时,应在每个所选帐户的特定表中插入文本。我认为php foreach是正确的。但我不确定了。 请记住,我对foreach和数组一无所知。所以请在帮助我时考虑提供修改后的代码= D.非常感谢你!

<?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());
}
?>

3 个答案:

答案 0 :(得分: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());
}

请将开始和结束括号放到foreach循环中,并尝试我没有阅读整个代码但只是发现你错过了括号。希望对你有所帮助。

答案 1 :(得分:1)

我想我知道你在做什么......

你需要这样做:

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
   }
}

我没有测试它,但它应该是这样的。

答案 2 :(得分:0)

我的建议是

创建字段名称为t [](数组) onchecked值将在下一页传递

表单复选框字段应该是那样的

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

并在提交表单时

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());


}

}
? >