www.webdeveloper.com
Results 1 to 3 of 3

Thread: Insert Multple arrays in database with signle query

  1. #1
    Join Date
    Oct 2011
    Location
    Pakistan
    Posts
    16

    Insert Multple arrays in database with signle query

    Hello there i am creating hosting details table for different hosting companies having problem
    I want to insert multiple arrays in my database with single query and than i want to update the record when the update query runs here is the code i am using

    <?php

    require_once("database.php");
    require_once("header.php");


    if(isset($_GET['id'])){
    $bc_host_plan_id =$_GET['id'];
    }
    else{
    $bc_host_plan_id = $_POST["host_id"];
    }

    $bc_host_plan_id = $_POST["host_id"];
    $bc_feature_name = $_POST["feature_name"];
    $bc_feature_1 = $_POST["feature_1"];
    $bc_feature_2 = $_POST["feature_2"];
    $bc_feature_3 = $_POST["feature_3"];





    $frmID = $_GET["id"];

    $action1 = isset($_POST["bc_form_action"]) ? $_POST["bc_form_action"] : "";

    $action = "save";
    $sucMessage = "";

    $errors = array();

    if ($bc_feature_name == "")
    $errors[] = "Feature name can not be empty";


    $err = '<table border="0" width="90%"><tr><td class="error" ><ul>';
    for ($i=0;$i<count($errors); $i++) {
    $err .= '<li>' . $errors[$i] . '</li>';
    }
    $err .= '</ul></td></tr></table>';

    if (isset($_POST["submit"]) ) {



    if (!count($errors)) {



    if ($action1 == "save") {



    $bc_feature_name = $_POST["feature_name"];
    $bc_feature_1 = $_POST["feature_1"];
    $bc_feature_2 = $_POST["feature_2"];
    $bc_feature_3 = $_POST["feature_3"];


    if(is_array($bc_feature_name) && is_array($bc_feature_1) && is_array($bc_feature_2) && is_array($bc_feature_3)){



    for($i=0;$i<count($bc_feature_name);$i++){
    $feature_name = $bc_feature_name[$i];
    $feature_1 = $bc_feature_1[$i];
    $bc_feature_2 = $bc_feature_2[$i];
    $bc_feature_3 = $bc_feature_3[$i];
    $res =mysql_query("INSERT INTO `host_plan_details` (`host_plan_id`, `feature_name`, `feature_1`, `feature_2`, `feature_3`) VALUES ('". $bc_host_plan_id ."', '". $feature_name ."', '". $feature_1 ."', '". $bc_feature_2 ."', '". $bc_feature_3 ."')");
    if ($res) {
    $sucMessage = "Record Successfully inserted.";
    } else {
    $sucMessage = "Error: Please try Later";
    } // end if res


    }

    }
    $frmID = mysql_insert_id();
    } // end if

    if ($action1 == "edit") {

    $bc_host_plan_id = $_GET['id'];

    mysql_query("delete from host_plan_details where host_plan_id='".$bc_host_plan_id."'");


    $bc_feature_name = $_POST["feature_name"];
    $bc_feature_1 = $_POST["feature_1"];
    $bc_feature_2 = $_POST["feature_2"];
    $bc_feature_3 = $_POST["feature_3"];



    if(is_array($bc_feature_name) && is_array($bc_feature_1) && is_array($bc_feature_2) && is_array($bc_feature_3)){


    for($i=0;$i<count($bc_feature_name);$i++){

    $feature_name = $bc_feature_name[$i];
    $feature_1 = $bc_feature_1[$i];
    $bc_feature_2 = $bc_feature_2[$i];
    $bc_feature_3 = $bc_feature_3[$i];

    $res = mysql_query("INSERT INTO `host_plan_details` (`host_plan_id`, `feature_name`, `feature_1`, `feature_2`, `feature_3`) VALUES ('". $bc_host_plan_id ."', '". $feature_name ."', '". $feature_1 ."', '". $bc_feature_2 ."', '". $bc_feature_3 ."')");

    if ($res) {
    $sucMessage = "Record Successfully inserted.";
    } else {
    $sucMessage = "Error: Please try Later";
    } // end if res


    }

    }

    } // end if

    } // end if errors

    else {
    $sucMessage = $err;
    }
    } // end if submit
    if(isset($_GET['id'])){
    $bc_host_plan_id =$_GET['id'];
    }
    $sql = "select * from host_plan_details where host_plan_id=$bc_host_plan_id order by id ASC";
    $res = mysql_query($sql);
    if ($res) {
    if ($row = mysql_fetch_assoc($res) ) {
    $id = $row["id"];
    $bc_host_plan_id = $row["host_plan_id"];
    $bc_feature_name = $row["feature_name"];
    $bc_value = $row["value"];


    //$bc_feature_name = explode(",",$row["feature_name"]);
    $bc_value = explode(",",$row["value"]);

    } // end if row
    $action = "edit";
    } // end if




    ?>
    <form method="post" name="bc_form" enctype="multipart/form-data" action="" >
    <input type="hidden" name="bc_form_action" class="bc_input" value="<?php echo $action; ?>"/>
    <table width="100%" border="0" cellspacing="0" cellpadding="5" align="center">
    <tr class="bc_heading">
    <td colspan="5" align="left" >Host Plan Details</td>
    </tr>
    <tr>
    <td colspan="2" align="center" class="success" ><?php echo $sucMessage; ?></td>
    </tr>
    <tr>

    <td align="left" class="bc_input_td">
    <span class="bc_label">Host Name :</span>
    <select name="host_id" id="host_id" class="bc_input" >
    <option></option>
    <?php

    $select = mysql_query("select * from home");
    while($row2 = mysql_fetch_assoc($select))
    {

    $id_host = $row2['id'];

    ?>
    <option class="hosting_id" id="<?php echo $row2['id']; ?>" value="<?php echo $row2['id']; ?>" <?php if($bc_host_plan_id == $id_host){ ?>selected="selected"<?php }?> ><?php echo $row2['host_name']; ?> </option>
    <?php } ?>
    </select>
    </td>
    </tr>

    <td align="left" class="bc_input_td" colspan="3">
    <label class="bc_label">Feature Value :</label>

    <input type="text" name="field_values" id="field_values" disabled="disabled" value="<?php echo $bc_field_values; ?>"><br>
    <input type="hidden" name="hidden_val" value="<?php echo $bc_field_values; ?>">


    <span id="more_img"></span>
    <div align="right" style="width:300px; float:right font-weight:bold" id="add_more_video_btn"><a href="javaScript:void(0)" onclick="add_more_image(0)">Add More Fields....</a></div>

    </td>
    </tr>

    <tr>
    <td align="left">
    <input name="submit" type="submit" value="Save" class="bc_button" />
    </td>
    </tr>
    </table>

    <table border="1">
    <tr>
    <th>Features</th>
    <?php
    if(isset($_GET['id'])){
    $bc_host_plan_id =$_GET['id'];
    }
    $qry = mysql_query("select * from headings where host_id ='".$bc_host_plan_id."'");
    while($get = mysql_fetch_assoc($qry))
    {
    $heading_new = $get['headings'];
    $heading_new = explode (",",$heading_new);

    if(is_array($heading_new)){
    foreach($heading_new as $value_1){

    ?>
    <th><?php echo $value_1; ?></th>
    <?php }} }?>


    </tr>
    <?php
    if(isset($_GET['id'])){
    $bc_host_plan_id =$_GET['id'];
    }

    $qry_2 = mysql_query("select * from host_plan_details where host_plan_id ='".$bc_host_plan_id."'");
    while($get_data = mysql_fetch_assoc($qry_2))
    {

    $feture_name = $get_data['feature_name'];
    $bc_feature_1 = $get_data["feature_1"];
    $bc_feature_2 = $get_data["feature_2"];
    $bc_feature_3 = $get_data["feature_3"];

    ?>

    <tr>

    <td><b><input type="text" value="<?php echo $feture_name; ?>" name="feature_name[]"></b></td>
    <td><b><input type="text" value="<?php echo $bc_feature_1; ?>" name="feature_1[]"></b></td>
    <td><b><input type="text" value="<?php echo $bc_feature_2; ?>" name="feature_2[]"></b></td>
    <td><b><input type="text" value="<?php echo $bc_feature_3; ?>" name="feature_3[]"></b></td>


    </tr>

    <?php

    } // while loop

    ?>

    <tr>



    </tr>

    </table>


    </form>


    would be waiting urgently needed

  2. #2
    Join Date
    May 2003
    Posts
    599
    What is the exact problem you are having? If you want to work directly with arrays, There is a class you can look at HERE. The documentation shows how to insert multiple arrays as records in example 5-A.
    We do precision guesswork

  3. #3
    Join Date
    Oct 2011
    Location
    Pakistan
    Posts
    16
    I actually want to insert all the arrays into the database while the submit button clicked and than also want to update all the single rows in the database when the action is edit

    what i am actually doing is inserting data on submit and when update button is cliked all the data related to the id
    is been deleted and new data is inserted into the database.
    does this make sense to you? thast what im looking for

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles