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
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 06-26-2008, 11:14 PM   #1 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 254
Thanks: 39
sarmenhb is on a distinguished road
Default how to insert data from a incrimented table row into the db

if i have a table whose rows are incrimented when the user clicks on the link "add row" how can i add the data put into each field into the db?

here is an example of what i mean
Tryit Editor v1.4

if there were textboxes in each new row that was created, how do i pass those values into a db?

thanks
__________________
http://www.sarmenhb.com
Send a message via AIM to sarmenhb Send a message via MSN to sarmenhb Send a message via Yahoo to sarmenhb Send a message via Skype™ to sarmenhb
sarmenhb is offline  
Reply With Quote
Old 06-26-2008, 11:24 PM   #2 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

Assign them to an array, and loop through the array when you add the data to the table.

HTML Code:
<input type="text" name="row_data[]" value="" />
When you use [] in the input fields name value, it adds all data to an array of the same name. Then you could use a foreach or for loop to spin through them and add them to the database however you like.
-m
delayedinsanity is offline  
Reply With Quote
The Following 2 Users Say Thank You to delayedinsanity For This Useful Post:
codefreek (06-26-2008), sarmenhb (06-27-2008)
Old 06-27-2008, 12:45 AM   #3 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 254
Thanks: 39
sarmenhb is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
Assign them to an array, and loop through the array when you add the data to the table.

HTML Code:
<input type="text" name="row_data[]" value="" />
When you use [] in the input fields name value, it adds all data to an array of the same name. Then you could use a foreach or for loop to spin through them and add them to the database however you like.
-m

thanks but what if this is my code

HTML Code:
<input type="text" name="row_data[1]" value="" />
<input type="text" name="row_data[2]" value="" />
<input type="text" name="row_data[3]" value="" />
<input type="text" name="row_data[4]" value="" />
<input type="text" name="row_data[5]" value="" />
what kind of php do i use to loop through this and to the db?

would it look liek this? its just a guess

Code:
for($i=0;$i<count($row_data);$i++) {

$query = mysql_query("insert into table values(null,'$_POST['row_data[i]]'");
}
__________________
http://www.sarmenhb.com
Send a message via AIM to sarmenhb Send a message via MSN to sarmenhb Send a message via Yahoo to sarmenhb Send a message via Skype™ to sarmenhb
sarmenhb is offline  
Reply With Quote
Old 06-27-2008, 02:15 AM   #4 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

Don't worry about putting the numbers in there, though you can, PHP will automagically assign them a number. I actually did this just recently on a page where I was setting configuration values, and the code looked like (after performing sanitization of the data elsewhere and error checking it as well):

PHP Code:
// Get the database connection
$this->pSQLconn $pSQL->returnSQL();

// Add the new data to the database
foreach ($aData as $key => $value)
{
    
$q sprintf("UPDATE `".TBL_PREFIX."config` SET cfg_value='%s' WHERE cfg_name='%s'"$this->pSQLconn->real_escape_string($value), strtoupper($key));
    
$bSuccess = ($this->pSQLconn->query($q)) ? true false;
    if (
$bSuccess === false) break; // If there were any problems along the way, drop out
}

return (bool) 
$bSuccess
However I tend to favour foreach when I can over for loops, but you could do it just like you were saying too. Note that this was from an associative array, from a numerical array I would just use a foreach ($row_data as $value) .. since you probably don't need the number unless you're using that in the database table.
-m
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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


All times are GMT. The time now is 05:57 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0