Reputation: 3546
I need to insert data from below inputs to database. Whole <div class="clone">
can be cloned with jquery and it also adds +1 to names, for example next created div will have inputs named name="npm[]1", name="apm[]1" etc.
<div class="list" id="sub">
part list subcategories of shops that can be checked (shop can have multiple subcategories) and they also get +1 on names. Is it ok like that?
<div class="clone">
<label>Shop name</label><input type="text" name="npm[]0" id="npm" required>
<label>Address</label><input type="text" name="apm[]0" id="apm" required>
<label>City</label><input type="text" name="mipm[]0" id="mipm" required>
<label>Working hours</label><input type="text" name="rv[]0" id="rv" required>
<label>Tel</label><input type="text" name="ktf[]0" id="ktf" required>
<label>Mob</label><input type="text" name="ktm[]0" id="ktm" required>
<label>E-mail</label><input type="text" name="kea[]0" id="kea" required>
<label>Contact person</label><input type="text" name="ipko[]0" id="ipko" required>
<a id="select" class="ctpct">Select shop subcategory</a>
<div class="list" id="sub">
<?php
$a="select a.id,a.cat,b.x,b.subcat,b.cat_id from cat a
inner join
subcat b
on a.id = b.cat_id
where b.cat_id !=1
order by a.id";
$res = mysql_query($a) or die(mysql_error());
$cat = '';
while ($re = mysql_fetch_array($res))
{
if( $cat === '' )
{
$cat = $re["cat"];
echo "<div class='catdis'>";
echo "<h2>$cat</h2>";
}
elseif( $cat !== $re["cat"] )
{
$cat = $re["cat"];
echo "</div>";
echo "<div class='catdis'>";
echo "<h2>$cat</h2>";
}
$subcat= $re["subcat"];
$id= $re["x"];
echo "<input type='checkbox' name='subcat[]0' value='$id'> $subcat<br />";
}
?>
</div>
</div>
PHP part:
foreach ($_POST as $key => $value) {
$values = mysql_real_escape_string($value);
$query2 = "INSERT INTO shops(n, a, c, wh, tel, mob, mail, con) VALUES ('$values')";
$rs2=mysql_query($query2) or die(mysql_error());
}
Now, php part that i have prints this "Column count doesn't match value count at row 1", it may be because under that clone div i have few more inputs, normal ones.
I'am php newbie so can you please help me with this. I need it to insert data for every shop from input 'Shop name' to 'Contact person' in table 'shops' and selected subcategories in table 'shop_subcat' with subcategory_id and shop_id
Thank you!
Upvotes: 0
Views: 636
Reputation: 782653
You should give the input fields names like name="apm[]"
. PHP treats names that end with []
specially, it turns all the inputs into an array. So you can do:
foreach ($_POST['apm'] as $apm) {
...
}
to process all those inputs.
Your name="apm[]0"
doesn't follow this pattern, so it doesn't work.
Also, remove the id
attributes from your inputs. IDs are required to be unique, and when you clone the DIV you're creating duplicates.
To solve the SQL error, you need to fix your query. You list 7 columns to insert into, so you have to provide 7 values.
$input_count = count($_POST['npm']);
for ($i = 0; $i < $input_count; $i++) {
$n = mysql_real_escape_string($_POST['npm'][$i]);
$a = mysql_real_escape_string($_POST['apm'][$i]);
// Repeat above pattern for all the other inputs
$query2 = "INSERT INTO shops(n, a, c, wh, tel, mob, mail, con)
VALUES ('$n', '$a', '$c', '$wh', '$mob', '$mail', '$con')";
$rs2=mysql_query($query2) or die(mysql_error());
}
Upvotes: 1