Inserting multiple arrays from a dynamic form

I am trying to work with this code to get two arrays to insert into sql db. The data comes from a form created from a sql table. My original intention was to update the spreadsheet where the form was formed from questions and answers. However, I believe it would be easier to just insert them into a new table, and when the information is needed, I can call from that table. In any case, although there are two important pieces of information that need to be conveyed and lined up accurately. This is the string in which the question is raised and the ID of the interview to which the question relates. Here is my code.

 $result = mysql_query("SELECT * FROM interviews WHERE id='$int'");
                        while($row = mysql_fetch_array($result)){ $a = $row[1]; $b =   $row[2]; $c = $row[3]; $d = $row[4]; $e = $row[5]; $f = $row[7]; $g = $row[8]; $h = $row[9]; }

                    $i = array();   
                    $result = mysql_query("SELECT * FROM dbinter WHERE interview_id='$int'");
                        while($row = mysql_fetch_array($result)){ $i[] = $row[2]; }

                    $l = array();   
                    $result = mysql_query("SELECT * FROM form_custom WHERE attached='$cid' && date='$e'");
                        while($row = mysql_fetch_array($result)){ $l[] = $row[0]; }

                    echo "<font style='font-size: 45px;'>$a $b</font>";

                                    echo "<p><font style='font-size: 20px;'>$c
                                    <br />$d</font></p>";

                    echo "<form method='POST' name='interview' action='interviewsheet.php?cid=$cid&int=$int&stat=proc'>";
                    echo "<div style='margin-top:50px;'><input type='submit' value='COMPLETE INTERVIEW'></div>";

                    foreach($i as $j){
                    $result = mysql_query("SELECT * FROM form WHERE id='$j'");
                    while($row = mysql_fetch_array($result)){ $k = $row[2]; $p = $row[0];}
                    echo "
                    <div style='margin: 20px;'>
                    <font styler='font-size: large; font-weight: bold;'>$k</font><br />
                    <textarea rows='5' cols='60' style='border: none;' name='q'></textarea>
                    <input type='hidden' name='qid' $value='$p'>
                    </div>
                    ";}

                    foreach($l as $m){
                    $result = mysql_query("SELECT * FROM form_custom WHERE id='$m'");
                    while($row = mysql_fetch_array($result)){ $n = $row[2]; $q = $row[0];}
                    echo "
                    <div style='margin: 20px;'>
                    <font styler='font-size: large; font-weight: bold;'>$n</font><br />
                    <textarea rows='5' cols='60' style='border: none;' name='qc'></textarea>
                    <input type='hidden' name='qcid' $value='$q'>
                    </div>
                    ";}
                    echo "</div>";

      

}

another code for the processing page:

    $a = array();
    $a[] = $_POST['q'];
    $b = array();
    $b[] =$_POST['qc'];
    $e = array();
    $e[] = $_POST['qid'];
    $f = array();
    $f[] = $_POST['qcid'];

    $frm = "f";
    $frmc = "fc";


    foreach($a as $key=>$value){
        $avalue = mysql_real_escape_string($value);
        $evalue = mysql_real_escape_string($e[$key]);
        $sql = "INSERT INTO answers (qid,cid,inta,frm,ans) VALUES ('$evalue','$cid','$int','$frm','$avalue')";
                if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}
    }

    foreach($b as $key=>$value){
        $bvalue = mysql_real_escape_string($value);
        $fvalue = mysql_real_escape_string($f[$key]);
        $sql = "INSERT INTO answers (qid,cid,inta,frm,ans) VALUES ('$fvalue','$cid','$int','$frmc','$bvalue')";
                if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}
    }





    header("Location: hr.php?act=int&stat=pend");

    }

      

+3


source to share


1 answer


What you are doing here is setting the array as the first element of the array.

$a = array();
$a[] = $_POST['q'];
$b = array();
$b[] =$_POST['qc'];
$e = array();
$e[] = $_POST['qid'];
$f = array();
$f[] = $_POST['qcid'];

      

So there $a

will be a single element of length and then an array equal to$_POST['q']



I agree with the comments above about the code used, etc., but by pasting this instead of the code above, you end up with a looped array ...

$a = (array)$_POST['q'];
$b = (array)$_POST['qc'];
$e = (array)$_POST['qid'];
$f = (array)$_POST['qcid'];

      

0


source







All Articles