TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   insert into db with incrimented rows in js (http://www.talkphp.com/absolute-beginners/3082-insert-into-db-incrimented-rows-js.html)

sarmenhb 07-07-2008 09:00 PM

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

delayedinsanity 07-07-2008 09:26 PM

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

sarmenhb 07-08-2008 05:56 AM

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.

sarmenhb 07-09-2008 04:41 PM

common please cant anyone help me?

delayedinsanity 07-09-2008 05:24 PM

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

sarmenhb 07-09-2008 07:35 PM

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>


sarmenhb 07-09-2008 07:47 PM

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..

delayedinsanity 07-09-2008 08:11 PM

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


All times are GMT. The time now is 11:54 PM.

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