4
Contributors
5
Replies
50
Views
3 Years
Discussion Span
Last Post by diafol
0

Hi,

It would help, if you can show us your codes. It does not matter if it is working or not.

-1
                   <?php
                   for($n=0;$n<3;$n++)
{
  if(isset($_POST['roll']))
  {
    echo "<br>Updating";

    $telugu=$_POST['telugu'];
    $hindi=$_POST['hindi'];
    $english=$_POST['english'];
    $maths=$_POST['maths'];
    $science=$_POST['science'];
    $social=$_POST['social'];
    $gk=$_POST['gk'];
    $class = $_POST['class'];


    $sql="insert into student_mark (student_id,student_class,telugu,hindi,english,maths,science,social,gk) 
    VALUES ('$student_id','$class','$telugu','$hindi','$english','$maths','$science','$social','$gk')";
    $res=mysql_query($sql);
  } 

}

                   ?>               

                 <input type="hidden" value="<?php echo "$class"; ?>" name="class"  />
                  </div>


                 <div class="bodytext4" id="table2">
                 <span class="bodytext9">
                  <?php   echo "$class";  ?>
                  </span><br />
                 </div>
                 <div class="bodytext4"  style="width:50px;" id="table2">
                 <span class="bodytext9"><input type="text" name="telugu" style="width:25px;"  /></span>                 </div>
                 <div class="bodytext4" style="width:50px;" id="table2">
                 <span class="bodytext9"><input type="text" name="hindi" style="width:25px;"   /></span>                 </div>
                 <div class="bodytext4" style="width:50px;" id="table2">
                 <span class="bodytext9"><input type="text" name="english" style="width:25px;"   /></span>                 </div>
                 <div class="bodytext4" style="width:50px;" id="table2">
                 <span class="bodytext9"><input type="text" name="maths" style="width:25px;"   /></span>                 </div>
                 <div class="bodytext4" style="width:50px;" id="table2">
                 <span class="bodytext9"><input type="text" name="science" style="width:25px;"   /></span>                 </div>
                 <div class="bodytext4" style="width:50px;" id="table2">
                 <span class="bodytext9"><input type="text" name="social" style="width:25px;"   /></span>                 </div>
                 <div class="bodytext4" style="width:50px;" id="table2">
                 <span class="bodytext9"><input type="text" name="gk" style="width:25px;"   /></span>                 </div>



               </div>


          <?php  


          } ?>   

        <input type="submit" name="roll" value="insert" class="signin" />
               </form> 
             </div>
0

Your form seems to only support a single record post. I'd suggest a javascript-driven "add row" on the form which should enable you to dynamically add new rows.

Get the js to insert these into the form, e.g. on 'add' button press:

<input name="telugu[]" />
<input name="hindi[]" />
(etc)

You then pick up the post data as an array of arrays. For example, say you posted 3 rows...

$val = array();
$count = count($_POST['telugu']);
for($x=0;$x<$count;$x++)
{
    //make $_POST vars safe, e.g.
    //$telugu = trim(mysql_real_escape_string($_POST['telugu'][$x]));
    //etc
    $val[] = "('$student_id','$class','$telugu','$hindi','$english','$maths','$science','$social','$gk')";
}

Then you can implode the $val array to create your VALUES clause

There are a number of ways to do this. This is just one. I haven't included any validation.

Edited by diafol

0

you can use this code:

you just need to instantiate EE. So for your form targets a result page/template as normal html, for example: <form action="/form-result"> (or whatever), then the quick and dirty way would be simply to include PHP in the template receiving the form.

On that page (receiving the form), have the following (template must have PHP enabled):

<?php
$EE =& get_instance();
$postUsers = $EE->input->post('user');

foreach($postUsers as $user) {
    // Extra Cleaning
    $user = $EE->db->escape_str($user);
    $user = $EE->security->xss_clean($user);
    // Fields and values to save to DB
    $dbFields = array(
        'user' => $user['name'],
        'party' => $user['party'],
        'marks' => $user['marks'],
        'comments' => $user['comments']
    );
    // Prefix variable used for good practice, but could hard code
    $EE->db->insert($EE->db->dbprefix . 'mytable', $dbFields);
}
?>

I've shown the long method of recreating the array to send to the DB in case you want to modify or add more fields, but you could simply just pass $user (after cleaning) to the database as the array will have the same structure.

If it's multiple POST variables sent by the form (instead of the array you imply), you'd need something like:

foreach($_POST as $var) {
    $postUsers = $EE->input->post($var);

A more ideal solution would be developing a plugin that would be called from the resulting template to trigger the above, or an extension that 'listened' for the appropriate form to be submitted, possibly by using the ACT security ability built into EE. But that's probably added an unnecessary level of complexity for your requirement.

best of luck.

0

Are you talkign about ExpressionEngine? If so, why would you need to implement that just in order to add a few records to a DB?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.