Maian Script World on Facebook Maian Script World on Twitter Maian Script World - Latest News Maian Script World on YouTube Maian Script World on LinkedIn


Please Read

You are required to register before you can post. This is via a custom verification system. Please post any problems you are encountering here and I`ll do my best to respond as soon as I can. Please note that a reply WILL be posted to all queries, so do not double post. There are no guarantees when a reply will appear but I do my best to answer all within 3/4 days max. Bumping topics won`t make the slightest difference.

IMPORTANT: Posts in English ONLY please, thank you! Any other languages will be ignored and your message deleted.
SEARCH: Use the search option to see if your question has been answered on the forum before. Or check the relevant script docs.
CUSTOMER: If you have purchased a commercial version of any of my software, using the contact option at the licence centre ensures a faster response.
AUTO DELETION: Accounts older than 5 days, with no posts or topics, are automatically deleted. Only register if you are thinking of posting.
PRIVATE MESSAGES: Private messages are currently disabled.
LINKS: Any links posted are 'NOFOLLOW' and will not be picked up by search engines.

You are not logged in.

#1 27-04-2012 07:02:09

duceduc
Members
From: Japan
Registered: 25-05-2010
Posts: 359

Inserting multiple rows into mysql using one form submit

Hi David. Here is another challenge I am stuck with again. This time, I am using the friend script as a guide but am stuck. I have a form that uses javascript to call up multiple fields box if needed to submit into mysql. Each set of field box consist of three text field.

How can I write up a mysql insert statement to have x number of field boxes selected (filled out) to be inserted into the db? So far I have the form created along with validation for each text field. What I have left to do is after the submit is clicked, the selected field will be inserted into the db.

My form looks like this.

<form  method="POST" action="index.php?cmd=add_image&amp;image=added">
<input class="formbox" type="text" name="cat[]" value="{cat_value}"  size="30">
<input class="formbox" type="text" name="description[]" value="{description_value}"  size="30">
<input class="formbox" type="text" name="extension[]" value="{extension_value}"  size="30">
<input type="hidden" name="num" id="num" value="" />
<input  type="submit" value="" />
</form>

Here is my main index.php and what I try to do that didn't work.

//-------------------
     // Case: Add Image
     //-------------------
    
     case "add_image":
    
     isWebmasterLoggedIn();

     if (isset($_GET['image']) && $_GET['image']=="added")
     {
       
    
     // Check for one set of field box input..
    for ($i=0; $i<$_POST['num']; $i++) {
      if ($_POST['cat'][$i]!='' || $_POST['description'][$i]!='' || $_POST['extension'][$i]!='') {
        $dataEntry = true;
      }
    }
    // Display error if no inputs..
    if (!isset($dataEntry)) {
      $error_string[] = $msg_images13;
    } else {
      // Now check other open field box..
      for ($i=0; $i<$_POST['num']; $i++) {
        if ($_POST['cat'][$i]='' || $_POST['description'][$i]='' || $_POST['extension'][$i]='') { 
            $dataInvalid = true;
         
        }

      }
    }
    // Display error for empty data..
    if (isset($dataInvalid)) {
      $error_string[] = $msg_images14;
    }
       if (!empty($error_string))
       {
         error($msg_script5,
               $error_string,
               $msg_script7,
               $msg_charset
               );
       }
      else
      {
foreach($_POST['cat'] as $i => $val) {
$cat = $val;
$description = $_POST['description'][$i];
$extension = $_POST['extension'][$i];
$sql = "insert into mg_images (pageid, name, addDate, imagepath, visits)
                                             values
                                            ('$cat','$description',".date("Y-m-d").",'$extension','0')";
                                             $result = mysql_query($sql);
}
      }
      
         updated($msg_images7,
                 'index.php?cmd=add_image',
                 $msg_script4,
                 $msg_script6,
                 $msg_charset
                 );
     }
    
// Render HTML boxes..
$renderHTML   = '';
for ($i=1; $i<=MAX_FIELDS; $i++) {
  $find        = array('{id}','{display}','{cat_number}','{cat_value}','{description}','{description_value}','{extension}','{extension_value}');
  $replace     = array($i,($i<=MIN_FIELDS ? '' : ' style="display:none"'),
                       $msg_images3,(isset($_POST['cat'][$i-1]) ? cleanData($_POST['cat'][$i-1]) : ''),
                       $msg_images4,(isset($_POST['description'][$i-1]) ? cleanData($_POST['description'][$i-1]) : ''),
                       $msg_images5,(isset($_POST['extension'][$i-1]) ? cleanData($_POST['extension'][$i-1]) : '')
                       );
  $renderHTML .= str_replace($find,$replace,file_get_contents(FOLDER_PATH.'data_files/html/field_boxes.tpl'));
}
    
    include(FOLDER_PATH.'inc/header.php');
    $tpl = new Savant2();
    $tpl->assign('MAX', MAX_FIELDS);
    $tpl->assign('MIN', MIN_FIELDS);
    $tpl->assign('ADD_IMAGE', $msg_images);
    $tpl->assign('DESCRIPTION', $msg_images2);
    $tpl->assign('CAT_NAME', $msg_images3);
    $tpl->assign('DESC', $msg_images4);
    $tpl->assign('EXTENSION', $msg_images5);
    $tpl->assign('ADD_IMAGE', $msg_images6);
    $tpl->assign('ADD_FIELDS', $msg_images11);
    $tpl->assign('REMOVE_FIELDS', $msg_images12);
    $tpl->assign('ADD_BOXES', $renderHTML);
    $tpl->display('data_files/add_image.php');     
    include(FOLDER_PATH.'inc/footer.php');
    break;

Last edited by duceduc (27-04-2012 11:42:35)

Offline

#2 28-04-2012 05:47:01

duceduc
Members
From: Japan
Registered: 25-05-2010
Posts: 359

Re: Inserting multiple rows into mysql using one form submit

Hi. Just wanted to know if you have looked at this post? I still can't figure it out.

Offline

#3 28-04-2012 07:45:29

msworld
Administrator
From: United Kingdom (Great Britain)
Registered: 09-05-2006
Posts: 7,063

Re: Inserting multiple rows into mysql using one form submit

duceduc wrote:

Hi. Just wanted to know if you have looked at this post? I still can't figure it out.

Yes, I just haven`t had time to respond yet.

Its not generally easily to view a code dump and see the issue. I can see the loop, so it seems ok. You say it doesn`t work, but you don`t say what happens instead? Generally having your error reporting set to report all errors can identify problems.

error_reporting(E_ALL);


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
David Ian Bennett - Lead Developer
www.maianscriptworld.co.uk

Offline

#4 28-04-2012 08:14:34

duceduc
Members
From: Japan
Registered: 25-05-2010
Posts: 359

Re: Inserting multiple rows into mysql using one form submit

Hi David. I got the error_reporting on. I don't see anything in the log though. The problem now is that it is inserting all my defined 'MAX' value (5) to the db when I only filled out 2 sets of field boxes. So it is half working.

Offline

#5 28-04-2012 09:11:20

msworld
Administrator
From: United Kingdom (Great Britain)
Registered: 09-05-2006
Posts: 7,063

Re: Inserting multiple rows into mysql using one form submit

The easy thing would be to check they have data? So for each iteration:

if ($extension && $description) {
}


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
David Ian Bennett - Lead Developer
www.maianscriptworld.co.uk

Offline

#6 28-04-2012 09:52:33

duceduc
Members
From: Japan
Registered: 25-05-2010
Posts: 359

Re: Inserting multiple rows into mysql using one form submit

That was a easy fix. big_smile

Thx!

Offline

#7 28-04-2012 12:32:22

msworld
Administrator
From: United Kingdom (Great Britain)
Registered: 09-05-2006
Posts: 7,063

Re: Inserting multiple rows into mysql using one form submit

duceduc wrote:

That was a easy fix. big_smile

Thx!

Ok, cool. smile


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
David Ian Bennett - Lead Developer
www.maianscriptworld.co.uk

Offline

Board footer

2Checkout.com is an authorized reseller of goods and services provided by Maian Script World.© 2003-2013 David Ian Bennett. All Rights Reserved | Forum: FluxBB

Privacy Policy | Refund Policy