here i am again with an amateur question about executing a query statement for multiple textboxes.
I made the following code, standard there are 4 textboxes but with javascript the user has a option to add more textboxes, which will be having continued naming following productg[4]
so productg[5]
etc. I need this to work with these 4 for now, thus the reason i have not posted the javascript part.
When executing this code, i do get a result as echo with all the textboxesvalues that were filled in, but in my database only 1 row is added, when i expect there to be 4.
If more information is needed to give me sufficient advise, i´ll provide it as best as i can.
<div id="productgroepen">
<div>
<label>Product groep 1</label>
<input type="text" name="productg[1]" />
</div>
<div>
<label>Product groep 2</label>
<input type="text" name="productg[2]" />
</div>
<div>
<label>Product groep 3</label>
<input type="text" name="productg[3]" />
</div>
<div>
<label>Product groep 4</label>
<input type="text" name="productg[4]" />
</div>
</div>
$total = 4;
for($i=1; $i<=$total; $i++)
{
if(isset($_POST['productg']) && isset($_POST['productg'][$i]))
{
$product = $_POST['productg'][$i];
$productquery = mysqli_query($conn, "INSERT INTO productgroepen SET id = 'LAST_INSERT_ID()' , levid ='$hidresult' , productgroep = '$product'");
if($productquery === false)
{
throw new Exception('Query failed: ' . mysqli_error($conn) );
}
}
}
Try this:
$total = 4;
for($i=1; $i<=$total; $i++){
if(isset($_POST['productg']) && isset($_POST['productg'][$i])){
//still needs some check whether $_POST['productg'][$i] contains an acceptable value
$result = mysqli_query("INSERT INTO productgroepen SET levid='".mysqli_real_escape_string($conn, $hidresult)."', productgroep='".mysqli_real_escape_string($conn, $_POST['productg'][$i])."'");
if($result === false)
throw new Exception('Query failed: '.mysqli_error($conn) );
}
}
The answer provided by Wimpie has solved my problem, and gave me a more insight on sql errorcatching so i've updated my question with the answer, if anyone is interested in the javascript side to add more textboxes, i'll be happy to post it.
The issue at hand was that i made the levid primary in my database, which doesn't allow duplicate entries.