TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-26-2010, 01:49 PM   #1 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default Multi insert from form data

Am a bit lost on how to get the data from a form and put it into a database (not update)

The mysql side easy enough..
Code:
insert into (col1, col2,col3) VALUES ($col1, $col2,$col3), ($col1, $col2,$col3). ($col1, $col2,$col3);
But it's getting the data from the form fields that am having problems getting my head round ..

So Multiple instances of col1, col2, col3 .. On Submit are they summitted as an array ?
e.g

Code:
<form id="form1" name="form1" method="post" action="<? $_SERVER['PHP_SELF']; ?>">
  <label>col1
  <input type="text" name="col1" id="col1" />
  </label>
  <label>col2
  <input type="text" name="col2" id="col2" />
  </label>
  <label>col3
  <input type="text" name="col3" id="col3" />
  </label>
  <p>
    <label>col1
    <input type="text" name="col4" id="col4" />
    </label>
    <label>col2
    <input type="text" name="col4" id="col5" />
    </label>
    <label>col3
    <input type="text" name="col4" id="col6" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col5" id="col7" />
    </label>
    <label>col2
    <input type="text" name="col5" id="col8" />
    </label>
    <label>col3
    <input type="text" name="col5" id="col9" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col6" id="col10" />
    </label>
    <label>col2
    <input type="text" name="col6" id="col11" />
    </label>
    <label>col3
    <input type="text" name="col6" id="col12" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col7" id="col13" />
    </label>
    <label>col2
    <input type="text" name="col7" id="col14" />
    </label>
    <label>col3
    <input type="text" name="col7" id="col15" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col8" id="col16" />
    </label>
    <label>col2
    <input type="text" name="col8" id="col17" />
    </label>
    <label>col3
    <input type="text" name="col8" id="col18" />
    </label>
</p>

 <p><input type="submit" name="Submit" id="Submit" value="Submit" /></p>

</form>
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 12-26-2010, 02:46 PM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

Ok, so I now know how to get the form data into arrays...

Code:
<body>
<form id="form1" name="form1" method="post" action="<? $_SERVER['PHP_SELF']; ?>">
  <label>col1
  <input type="text" name="col1[]" id="col1" />
  </label>
  <label>col2
  <input type="text" name="col2[]" id="col2" />
  </label>
  <label>col3
  <input type="text" name="col3[]" id="col3" />
  </label>
  <p>
    <label>col1
    <input type="text" name="col1[]" id="col4" />
    </label>
    <label>col2
    <input type="text" name="col2[]" id="col5" />
    </label>
    <label>col3
    <input type="text" name="col3[]" id="col6" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col1[]" id="col7" />
    </label>
    <label>col2
    <input type="text" name="col2[]" id="col8" />
    </label>
    <label>col3
    <input type="text" name="col3[]" id="col9" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col1[]" id="col10" />
    </label>
    <label>col2
    <input type="text" name="col2[]" id="col11" />
    </label>
    <label>col3
    <input type="text" name="col3[]" id="col12" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col1[]" id="col13" />
    </label>
    <label>col2
    <input type="text" name="col2[]" id="col14" />
    </label>
    <label>col3
    <input type="text" name="col3[]" id="col15" />
    </label>
</p>
  <p>
    <label>col1
    <input type="text" name="col1[]" id="col16" />
    </label>
    <label>col2
    <input type="text" name="col2[]" id="col17" />
    </label>
    <label>col3
    <input type="text" name="col3[]" id="col18" />
    </label>
</p>

 <p><input type="submit" name="Submit" id="Submit" value="Submit" /></p>

</form>
So I now have arrays col1[] col2[] col3[]

Hmm...

so have created a mutlidimentional array
Code:
$new_array = array(($_REQUEST['col1']),($_REQUEST['col2']),($_REQUEST['col3']));
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 12-26-2010, 02:53 PM   #3 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

Now to show itterate though the array and show the data...

Code:
for ($i=0; $i < count($_REQUEST['col1']); $i++) {
    echo $_REQUEST['col1'][$i]." -- ";
    echo $_REQUEST['col2'][$i]." -- ";
    echo $_REQUEST['col3'][$i]."<br />";
}
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 12-26-2010, 03:14 PM   #4 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

Time to build the sql ...
Code:
$sql = "INSERT INTO table(col1, col2, col3) VALUES ";

$columns = array(($_REQUEST['col1']),($_REQUEST['col2']),($_REQUEST['col3']));


for ($i=0; $i < count($_REQUEST['col1']); $i++) {
	$sql .= "(";
    $sql .= $_REQUEST['col1'][$i]. ",";
    $sql .= $_REQUEST['col2'][$i]. ",";
	$sql .= $_REQUEST['col3'][$i]. "),";
}

// Lets have a look at the final sql..
echo $sql . "<br />";
So, need to take the , off the end of the sql...

Code:
echo substr($sql, 0, -1);

And I think thats it about done..

Sorry for going on, but I thought I would ask the question, then figured out how to do it, I hope someone else gets some use out of this


P.S. Not tried it yet so don't know if it works !
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading data form table to table? code_junkie Absolute Beginners 5 11-06-2008 03:34 PM
Venerable methods and the applications they are commonly trusted in. Village Idiot Tips & Tricks 7 11-06-2008 07:36 AM
Retrving data from database to form knvuppula General 2 09-10-2008 04:33 AM
how to insert data from a incrimented table row into the db sarmenhb Absolute Beginners 3 06-27-2008 01:15 AM
Insert data into two tables with one query CMellor MySQL & Databases 5 12-10-2007 10:46 AM


All times are GMT. The time now is 09:07 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design