0

I have a query which is doing something strange. This checkbox is in a while loop and it correctly lists out everything it needs to:

<input type='checkbox' name='rep[]' value='$invoiceID'>Reference Number: $invoiceID
<input type='hidden' name='billablehours[]' value='$billableTotal'>

When the form is submitted the values are inserted into the database using:

foreach ($_POST['rep'] as $index => $id) {
$sql2="INSERT into b_sale_basket (QUANTITY,LID,NAME)
VALUES
('".$_POST['billablehours'][$index]."','s1','".$_POST['rep'][$index]."')";

if (!mysqli_query($con,$sql2))
{
 die('Error: ' . mysqli_error($con));
}

}

It inserts everything as it should do except billablehours. I have outputted the value of $billableTotal on each checkbox on the form page and the value is correct. For example it might equal 25 but when the button is pressed it inputs 37.5 which is another value of a checkbox.

Strange. Can anybody identify an issue?

1
  • Only checked checkboxes are submitted. So if you only check one, then rep array will only have one element 0 but thebillablehours array will have as many as are defined on the page. Commented Dec 12, 2013 at 22:57

4 Answers 4

2

Problem is that if checkbox is not checked it's not passed to server, so if u have 3 checkboxes and select first and third, on server you got them with 0, 1 index.

in your case just use specific identifiers:

<input type='checkbox' name='rep[$id]' value='$invoiceID'>Reference Number: $invoiceID
<input type='hidden' name='billablehours[$id]' value='$billableTotal'>

Assuming you know this $id is connected corresponding $invoiceID and $billableTotal, it may be entry id from database.

And when using: $_POST['billablehours'][$index] if checkbox is not checked it gots empty...

Note: my exmplanation is just to understand the point, not 100% working example maybe, because I've got no full code what you do.

Just remember: It's the key when working with checkboxes to correctly reference the data passed from server to client and vs.

Sign up to request clarification or add additional context in comments.

Comments

0

Only checked checkboxes are submitted. So if you only check one, then rep array will only have one element 0 but thebillablehours array will have as many as are defined on the page.

$i = 0;
//loop
    echo "<input type='checkbox' name='rep[$i]' value='$invoiceID'>Reference Number: $invoiceID";
    echo "<input type='hidden' name='billablehours[$i]' value='$billableTotal'>";
    $i++;
//end loop

So the rep and billablehours indexes will match regardless of how many checkboxes were checked.

Comments

0

You are wide open to SQL injection attacks with your script. You either need to use prepared statements (preferable) or run your $_POST values through mysqli_real_escape_string;

Leaving my incorrect answer below, but not deleting this answer because I think the SQL injection vulnerability needs to be addressed.


That being said, your issue is that you aren't echoing the value:

value='<?php echo $billableTotal; ?>'>

4 Comments

I'd be willing to bet he's echo'ing the entire thing in double quotes, so the variables are being printed correctly and that isn't the issue. I could be wrong, though.
I don't think the problem comes from echo, I actually think everything he showed us already is inside a echo "..." so variables are processed
It is already inside an echo. I put the $totalBillable at the side of each checkbox and it displayed the correct figure that it should be but when inserted it displays a different number...
Thanks Rob. This will certainly be addressed!
0

Make these changes:

name="billablehours[$invoiceID]"

$_POST['billablehours'][$_POST['rep'][$index]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.