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 07-07-2008, 09:00 PM   #1 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default insert into db with incrimented rows in js

hi,

look at this page

Add Row v2 (Javascript)

you'll notice that when you click on add row javascript adds another row into the table. what im wondering is how do i insert the data into the table when more than 1 row is added?

i was thinking of doing a loop but its really confusing me to how to add the data into the database.

thanks
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 07-07-2008, 09:26 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Didn't you ask this before? I can't find the thread, but one of the ways you can do this is by inserting the data into an array.

Code:
<input type="text" id="row1" name="row[]" value="" />
<input type="text" id="row2" name="row[]" value="" />
Values from both of those inputs could be found in the array $_POST['row']
-m
delayedinsanity is offline  
Reply With Quote
Old 07-08-2008, 05:56 AM   #3 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

hi, here is the page sample im working off of.
http://www.gothostin.com/row.php

can you please write a simple easy to understand php statement that shows atleast one line of cells being inserted into the db. because for some reason my head is not being able to understand how to do this.

thanks.

when i click on add row what i cant figure out is where do these textboxe variables going? so that i can see the textboxes names being created.
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 07-09-2008, 04:41 PM   #4 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

common please cant anyone help me?
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 07-09-2008, 05:24 PM   #5 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

You're putting values inside of the brackets for your input array. You can do this, but because you want an unlimited number of potential rows, you should omit it and let the array take care of itself.

Here's a quick and dirty example of what happens with the data when you do this. On submit the script will display the $_POST array as it looks in it's entirety and also the smaller array that is created from the input fields. This should give you an idea of how to put the data where you want it, what it'll look like when it gets there, and how to then deal with it afterwards;

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
    
if (isset($_POST['submit']))
    {
        echo 
'<pre>';
        
print_r($_POST);
        echo 
'<br /><br />';
        
print_r($_POST['array']);
        echo 
'</pre>';
    }
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="rowtest">

<fieldset>
<input type="text" id="one" name="array[]" value="" />
<input type="text" id="two" name="array[]" value="" /><br />

<input type="text" id="three" name="array[]" value="" />
<input type="text" id="four" name="array[]" value="" /><br />
</fieldset>

<button type="submit" name="submit" value="true">submit</button>
</form>

</body>
</html>
-m
delayedinsanity is offline  
Reply With Quote
Old 07-09-2008, 07:35 PM   #6 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

this is what i did but it doesnt work can you see what i did wrong?

thanks

Code:
<?php

include("inc/config.php");


for($z=0;$z<=count($name);$z++) {
	for($b=0;$b<=count($quantity); $b++) {
		



$query = mysql_query("insert into tbl_addrow values(null, '$names[$z]','$quan[$b]')");
	}
}

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="inc/addrow1.js"></script>

</head>

<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table>
<tr><th>Name</th><th>Quantity</th></tr>
<tr class="row_to_clone">
  <td><input type="text" name="name[0]"/></td>
  <td><input type="text" name="quantity[0]"/></td>

</tr>


</table>

<div><input type="submit" /></div>

<div><a onclick="addRow(); return false;" href="#">Add Row</a></div>

</form>



</body>
</html>
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 07-09-2008, 07:47 PM   #7 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

nm, got it to works thanks!!! this is what i did with the for loop

since the array variable names index will always match the quantity array index.

i did this

Code:
for($a=0;$a<=count($name);$a++) {

		
$query = mysql_query("insert into tbl_addrow values(null, '$name[$a]','$quantity[$a]')");
	
}
and it worked great, only problem i saw was that an additional empty row was inserted into the table cant see why or how that got there..
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 07-09-2008, 08:11 PM   #8 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Change this line:

PHP Code:
for($a=0;$a<=count($name);$a++) {

// to


for ($a 0$a count($name); $a++) {

// or better yet

$count count($name);
for (
$a 0$a $count$a++) { 
A numeric array begins at 0, so when it has 4 records, the last record is at index 3, like so:

Code:
$array[0] = '..';
$array[1] = '..';
$array[2] = '..';
$array[3] = '..';
So, four records, but the last number is three. When you run count() on it, it tells you that there are four records. When you run the for loop without subtracting one from the result of count() you're actually running the for loop up to index [4] which doesn't exist.
-m
delayedinsanity 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


All times are GMT. The time now is 02:40 AM.

 
     

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