Which values aren’t being used and where in the database are you wanting them to go?
Humm Starting all over again, I have invoice with dynamic javascripts like this:
<td><input type=“button” value=“Add Row” onClick=“addRow(‘dataTable’)” /></td>
<table align=“center” id=“dataTable”>
<tr>
<td>Qty: </td>
<td><input type=“text” name=“qty”></td>
<td>Description: </td>
<td><textarea name=“description” cols=“16” rows=“”></textarea></td>
<td>Unit Price: </td>
<td><input type=“text” name=“unit_price”></td>
<td>Line Total: </td>
<td><input type=“text” name=“line_total”></td>
</tr>
</table>
And I am currently using code given by SgtLegend:
<?PHP
if (isset($_POST[‘qty’]) && sizeof($_POST[‘qty’]) > 0)
{
for($i = 0, $maxi = count($_POST[‘qty’]); $i < $maxi; $i++)
{
$quantity = (isset($_POST[‘qty’][$i]) && !empty($_POST[‘qty’][$i])) ? mysql_real_escape_string($_POST[‘qty’][$i]) : 0;
$description = (isset($_POST[‘description’][$i]) && !empty($_POST[‘description’][$i])) ? mysql_real_escape_string($_POST[‘description’][$i]) : 0;
$unit_price = (isset($_POST[‘unit_price’][$i]) && !empty($_POST[‘unit_price’][$i])) ? mysql_real_escape_string($_POST[‘unit_price’][$i]) : 0;
$line_total = (isset($_POST[‘line_total’][$i]) && !empty($_POST[‘line_total’][$i])) ? mysql_real_escape_string($_POST[‘line_total’][$i]) : 0;
$sql = "INSERT INTO test (qty,description,unit_price,line_total) VALUES ('$quantity','$description','$unit_price','$line_total')";
mysql_query($sql);
print_r($sql);
}
}
else
{
exit(‘No values entered’);
}
?>
I want to insert values of all the dynamically created fields which can be any number.They have same name and same table is repeated just from the table tag when ever user clicks add more button.
That code is fine for a single row but not for all rows
Sounds to me like the dynamic table rows your adding have a numeric value with instead of them been empty brackets.
I think the best solution is using array. You can use the naming convention for the text fields as <input name="txt[<?=$count;?>]>
then on the next page you can retreive the values as
using simple for or foreach statement
for($i=0; $i<count($_POST[‘txt’]);$i++)
{
Insert into tablename value($txt[$i]);
}
Some thing this. Hope it will be simple for you now.
regards,
Usman Munir
<snip/>
One question. What is $count in <input name="txt[<?=$count;?>]>?
should I initialise it by 0??
Yuhu!!
Atlast its working!!!
Thx for your constant support gentlemen.
My new code is:
<?PHP
if (isset($_POST[‘qty’]) && sizeof($_POST[‘qty’]) > 0)
{
for($i = 0, $maxi = count($_POST[‘qty’]); $i < $maxi; $i++)
{
$quantity = (isset($_POST[‘qty’][$i]) && !empty($_POST[‘qty’][$i])) ? mysql_real_escape_string($_POST[‘qty’][$i]) : 0;
$description = (isset($_POST[‘description’][$i]) && !empty($_POST[‘description’][$i])) ? mysql_real_escape_string($_POST[‘description’][$i]) : 0;
$unit_price = (isset($_POST[‘unit_price’][$i]) && !empty($_POST[‘unit_price’][$i])) ? mysql_real_escape_string($_POST[‘unit_price’][$i]) : 0;
$line_total = (isset($_POST[‘line_total’][$i]) && !empty($_POST[‘line_total’][$i])) ? mysql_real_escape_string($_POST[‘line_total’][$i]) : 0;
$sql = "INSERT INTO test (qty,description,unit_price,line_total) VALUES ('".$quantity."','".$description."','".$unit_price."','".$line_total."')";
mysql_query($sql);
print_r($sql);
}
}
else
{
exit(‘No values entered’);
}
?>
Thanks again!! Cant tell you I am so happy!!
$count is the index of the text field. If you are generating objects dynamically on the form then you can increment on each object creation and use this incremented value in the array index. I recommend you to use sequential index for array index.
regards,
Usman
Now due to some unknown reasons, this same query is not working with multipal variables:
<?PHP
if (isset($_POST[‘qty’]) && sizeof($_POST[‘qty’]) > 0)
{
for($i = 0, $maxi = count($_POST[‘qty’]); $i < $maxi; $i++)
{
$quantity = (isset($_POST[‘qty’][$i]) && !empty($_POST[‘qty’][$i])) ? mysql_real_escape_string($_POST[‘qty’][$i]) : 0;
$description = (isset($_POST[‘description’][$i]) && !empty($_POST[‘description’][$i])) ? mysql_real_escape_string($_POST[‘description’][$i]) : 0;
$unit_price = (isset($_POST[‘unit_price’][$i]) && !empty($_POST[‘unit_price’][$i])) ? mysql_real_escape_string($_POST[‘unit_price’][$i]) : 0;
$line_total = (isset($_POST[‘line_total’][$i]) && !empty($_POST[‘line_total’][$i])) ? mysql_real_escape_string($_POST[‘line_total’][$i]) : 0;
$sql = "INSERT INTO test (qty,description,unit_price,line_total) VALUES ('".$quantity."','".$description."','".$unit_price."','".$line_total."')";
mysql_query($sql);
print_r($sql);
}
}
else
{
exit(‘No values entered’);
}
?>
Now I really want to die …
How it is possible that
<?PHP
if (isset($_POST[‘qty’]) && sizeof($_POST[‘qty’]) > 0)
{
for($i = 0, $maxi = count($_POST[‘qty’]); $i < $maxi; $i++)
{
$quantity = (isset($_POST[‘qty’][$i]) && !empty($_POST[‘qty’][$i])) ? mysql_real_escape_string($_POST[‘qty’][$i]) : 0;
$description = (isset($_POST[‘description’][$i]) && !empty($_POST[‘description’][$i])) ? mysql_real_escape_string($_POST[‘description’][$i]) : 0;
$unit_price = (isset($_POST[‘unit_price’][$i]) && !empty($_POST[‘unit_price’][$i])) ? mysql_real_escape_string($_POST[‘unit_price’][$i]) : 0;
$line_total = (isset($_POST[‘line_total’][$i]) && !empty($_POST[‘line_total’][$i])) ? mysql_real_escape_string($_POST[‘line_total’][$i]) : 0;
$sql = "INSERT INTO test (qty,description,unit_price,line_total) VALUES ('".$quantity."','".$description."','".$unit_price."','".$line_total."')";
mysql_query($sql);
print_r($sql);
}
}
else
{
exit(‘No values entered’);
}
?>
is giving o/p:
INSERT INTO test (qty,description,unit_price,line_total) VALUES (‘1’,‘2’,‘3’,‘4’)INSERT INTO test (qty,description,unit_price,line_total) VALUES (‘5’,‘6’,‘7’,‘8’)INSERT INTO test (qty,description,unit_price,line_total) VALUES (‘9’,‘10’,‘11’,‘12’)
and
<?PHP
if (isset($_POST[‘qty’]) && sizeof($_POST[‘qty’]) > 0)
{
for($i = 0, $maxi = count($_POST[‘qty’]); $i < $maxi; $i++)
{
$quantity = (isset($_POST[‘qty’][$i]) && !empty($_POST[‘qty’][$i])) ? mysql_real_escape_string($_POST[‘qty’][$i]) : 0;
$description = (isset($_POST[‘description’][$i]) && !empty($_POST[‘description’][$i])) ? mysql_real_escape_string($_POST[‘description’][$i]) : 0;
$unit_price = (isset($_POST[‘unit_price’][$i]) && !empty($_POST[‘unit_price’][$i])) ? mysql_real_escape_string($_POST[‘unit_price’][$i]) : 0;
$line_total = (isset($_POST[‘line_total’][$i]) && !empty($_POST[‘line_total’][$i])) ? mysql_real_escape_string($_POST[‘line_total’][$i]) : 0;
$sql = "INSERT INTO test (qty,description,unit_price,line_total) VALUES ('".$quantity."','".$description."','".$unit_price."','".$line_total."')";
mysql_query($sql);
print_r($sql);
}
}
else
{
exit(‘No values entered’);
}
?>
giving o/p:
INSERT INTO test (qty,description,unit_price,line_total) VALUES (‘1’,‘2’,‘3’,‘4’)
Why you are using sizeof($_POST[‘qty’]) ?? If you want to check if Qty is >0 then you can simply use if($_POST[‘qty’]>0). Debug the code and try to print values of varible like mentioned below
<?PHP
if (isset($_POST[‘qty’]) && sizeof($_POST[‘qty’]) > 0)
{
for($i = 0, $maxi = count($_POST[‘qty’]); $i < $maxi; $i++)
{
$quantity = (isset($_POST[‘qty’][$i]) && !empty($_POST[‘qty’][$i])) ? mysql_real_escape_string($_POST[‘qty’][$i]) : 0;
$description = (isset($_POST[‘description’][$i]) && !empty($_POST[‘description’][$i])) ? mysql_real_escape_string($_POST[‘description’][$i]) : 0;
$unit_price = (isset($_POST[‘unit_price’][$i]) && !empty($_POST[‘unit_price’][$i])) ? mysql_real_escape_string($_POST[‘unit_price’][$i]) : 0;
$line_total = (isset($_POST[‘line_total’][$i]) && !empty($_POST[‘line_total’][$i])) ? mysql_real_escape_string($_POST[‘line_total’][$i]) : 0;
$sql = “INSERT INTO test (qty,description,unit_price,line_total) VALUES ('”.$quantity.“‘,’”.$description.“‘,’”.$unit_price.“‘,’”.$line_total.“')”;
echo $sql;
mysql_query($sql);
print_r($sql);
}
}
else
{
exit(‘No values entered’);
}
?>
O/p:
INSERT INTO test (qty,description,unit_price,line_total) VALUES (‘1’,‘2’,‘3’,‘4’)INSERT INTO test (qty,description,unit_price,line_total) VALUES (‘1’,‘2’,‘3’,‘4’)
Still not working arusdevelopers
Open Phpadmin (mysql database) and run this query inside. Check if it is inserting data or not. May be you have some problem in spelling. Query seems to be fine or attach the two files.
regards,
Usman
instead of generating this –
INSERT INTO test (qty,description,unit_price,line_total) VALUES (‘1’,‘2’,‘3’,‘4’)INSERT INTO test (qty,description,unit_price,line_total) VALUES (‘1’,‘2’,‘3’,‘4’)
(which, by the way, should have a semi-colon separating the statements, but as far as i can recall php won’t handle multiple sql statements, only one at a time)
you should instead be generating this single statement –
INSERT INTO test (qty,description,unit_price,line_total)
VALUES (‘1’,‘2’,‘3’,‘4’),(‘5’,‘6’,‘7’,‘8’), …
Don’t mean to sound like an ass but qty for example is an array and using $_POST[‘qty’] wouldn’t return a length because all your saying is if the value of $_POST[‘qty’] is greater then 0 then proceed.
Also i don’t understand why you keep going on about counts when using just a simple array identifier works fine. Using a count complicates things since you have to seek out the array keys before you can do anything which to me is a waste of time since you can let the backend generate the keys for you.
@anita_86: The way i explained to use the code works fine as i have used the same method of code in previous scripts i have written and they all worked with perfect and no need to manage dynamically array keys within the HTML itself.
This thread hurts my eyes
I am very thankful to all replies.
Finally i got the solution for the issue.I just made it in single form tag and placed the javascript table at the end.
Thanks for your support.