473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how Do I Insert multiple values of Checkbox & textbox corresponding to it in database

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.                     include("database.php");
  3. $query2="select prodcode,prodname,id from product";
  4. $result2=mysqli_query($con,$query2);
  5. echo"<table border='2' align='center' cellpadding='0' cellspacing='0' style='width:50%'>";
  6. echo"<tr>";
  7. echo"<thead>";
  8. echo"<th>"."ProductCode"."</th>";
  9. echo"<th>"."ProductName"."</th>";
  10. echo"<th>"."Quantity"."</th>";
  11. echo"</thead>";
  12.                 while($row=mysqli_fetch_array($result2,MYSQL_ASSOC))
  13. {
  14. echo"<tr>";
  15. echo"<td>"."<input type='checkbox' id='chk' value='".$row['prodcode']."'name='checkbox1[]'/>".$row['prodcode']."</td>";
  16. echo"<td>".$row['prodname']."</td>";
  17. echo"<td>"."<input type='text' maxlength='4' id='quan' name='quantity[]'class ='quantity'/>"."</td>";
  18. echo"</tr>";
  19. }
  20. ?>
  21. <?php
  22. include("database.php");
  23. if(isset($_POST["btnsubmit"]))
  24. {
  25. $check=$_POST['checkbox1'];
  26. $quantity=$_POST["quantity"];
  27.  
  28. for ($i=0; $i<sizeof($check);$i++) 
  29. {
  30. $query="INSERT INTO elevconfigdetails(id,prodcode,quantity)
  31. VALUES ('$id','".$check[$i]."','".$quantity."' )";
  32. mysqli_query($con,$query) or die ('Error updating database'.mysqli_error($con));
  33. echo "</br>Record is inserted.";
  34. }
  35.  

when user click on submit button checkbox values(which is checked) and textbox values should be insert into mysql table row by row.i am successful to insert multiple checkbox values but the related textbox value is not inserted into database.
how i can fix this problem??
any help plzzzzzzz??
May 3 '16 #1

✓ answered by Dormilich

$quantity is an array, but you treat it as string, which obviously won’t work.

7 5357
Dormilich
8,658 Expert Mod 8TB
$quantity is an array, but you treat it as string, which obviously won’t work.
May 4 '16 #2
@Dormilich ya the name attribute of textbox is an array..i want to insert multiple textbox values into database
May 4 '16 #3
i am having multiple checkboxes that are coming from database and i have given a field(textbox) with every checkbox...now suppose 10 checkboxes are coming from database..now i want to insert values in database only of those checkboxes which are checked along with their textbox values.in textbox user insert the quantity.when i click on submit button the checkbox values are getting inserted right but the corresponding textbox values are not getting inserted..help me to solve this problem
May 4 '16 #4
Dormilich
8,658 Expert Mod 8TB
the problem is that there is no relation between the checkboxes and the text boxes. this is the first problem to solve.

I’d assign the text boxes using the product id. i.e.
Expand|Select|Wrap|Line Numbers
  1. <input type="number" min="0" name="qantity[{product-id}]" value="0">
where {product-id} is the product id.

then you can do in PHP
Expand|Select|Wrap|Line Numbers
  1. // using PDO for insert
  2. foreach ($products as $id) {
  3.     $stmt->execute([
  4.         ':id' => $id, 
  5.         ':quantity' => $quantities[$id],
  6.     ]);
  7. }
  8.  
May 4 '16 #5
@Dormilich thanks for reply..i am new in PHP.so dont know about PDO...please tell me your suggestion in (mysqli) so it is helpfull me to understand..how i can write the insert query.??
means the id field auto-increment and the last_id field is need to repeated.so tell me that..
May 4 '16 #6
Dormilich
8,658 Expert Mod 8TB
I find mysqli prepared statements awfully complicated, so I stick to PDO.

and for PDO you can check out the documentation: http://php.net/pdo
May 4 '16 #7
okk.thank you Dormilich..........
May 4 '16 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fareast Adam | last post by:
Here is my code: if(isset($_REQUEST)) { $nopwd= $_POST;//no of password $length = $_POST;//lenght of password $value1= $_POST;//abcdefghijklmnopqrstuvwxyz $value2=...
0
by: shantanu | last post by:
hi all I need to read the excel sheet for the values. The requirement is like: For the number of rows in the excel which are not null, i need to store the values of the row in a array and then...
5
parshupooja
by: parshupooja | last post by:
Hello I am trying to create to a Timesheet . I am intiating number of textboxes and label based on criteria chosen by admin. This is what i have written in pageload to generate timesheet for 2...
3
vijcbe
by: vijcbe | last post by:
Hello PHP veterans, I am new to PHP. I tried to insert the session id of a user into the database table. code: $xyz = "insert into table_name values(' ', '$PHPSESSID', ...)"; The issue is,...
2
by: rajsrmc | last post by:
Hello experts Let's say I have a gridview with some columns like, title, name, funding agency. Here's an example of what I want to do: I want to display all the records in the gridview to start....
1
by: dev singh | last post by:
Sir would like to know how to insert date, month and year selected from datepicker into mysql database thorugh php. I am trying to store the value of datepicker but it shows 00-00-00 kindly help...
3
by: ROXIT | last post by:
hello every one pls tell me how can i save multiple checkbox value into database. I have created one table into the database named Category_Table and another table named CategoryTitle. for e.x. ...
1
by: ahmad641 | last post by:
***> here is the code***. > when i search single word like GOOGLE then the result is appear > if i search two or more word search then the result is not appear. <?php ...
1
by: navsa | last post by:
I have multiple records in mysql database,but i need to fetch all the records from db and store it on seperate dynamic created form.please help me guyz its urgent.. id name 1 aaa 2 bbb 3 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.