By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,830 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,830 IT Pros & Developers. It's quick & easy.

insert data on mysql thru php

ddtpmyra
100+
P: 333
I donít know if I posted my question on the right forum if not my bad, but Iím having trouble how to create a php script that will insert data on mysql

Scenario:
I have 3 checkbox on my form and users select all
Checkbox1
Checkbox2
Checbox3

Table structure:
Date, checknames


Goal:
Every time I hit the update button with query behind I want to insert the current date and checkboxnames (see below table)
------------------------------
|Created | checkboxname|
---------------------------------
|10-07-08 |checkbox1 |
---------------------------------
|10-07-08 |checkbox2 |
--------------------------------
|10-07-08 |checkbox3 |
---------------------------------

Problem:
Help to construct my insert query which i know is wrong :(
INSERT INTO dmtable (Created, checkboxname)
VALUES (NOW(),'{checkbox1}', '{checkbox2}','{checkbox3}');
Oct 7 '08 #1
Share this Question
Share on Google+
5 Replies


chelvan
P: 90
hi
i don't know why you put this { bracket, on your inserting value. remove that.

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO dmtable (Created, checkboxname)
  2. VALUES (NOW(),'{checkbox1}', '{checkbox2}','{checkbox3}');
  3.  
change your query like this
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO dmtable (Created, checkboxname)
  2. VALUES (NOW(),'checkbox1', 'checkbox2','checkbox3');
  3.  

regards
chel-1
Oct 8 '08 #2

Markus
Expert 5K+
P: 6,050
hi
i don't know why you put this { bracket, on your inserting value. remove that.

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO dmtable (Created, checkboxname)
  2. VALUES (NOW(),'{checkbox1}', '{checkbox2}','{checkbox3}');
  3.  
That wouldn't affect anything..

OP, you pass more values to your query than columns. You give 2 columns (created, checkboxname) and 4 values. This will not work.
Oct 8 '08 #3

ddtpmyra
100+
P: 333
Markus that's the thing how can I insert all the 3 information in one column in one query
Oct 8 '08 #4

Markus
Expert 5K+
P: 6,050
Markus that's the thing how can I insert all the 3 information in one column in one query
Why can't you have a column for each checkbox? That makes sense.
Oct 8 '08 #5

Atli
Expert 5K+
P: 5,058
Why don't you just add a row for each checked box?

Arrays of checkboxes can be sent using the same <input> name, each one containing a unique value.
You can simply send such an an array, loop through it once you get it and construct a query based on that.

For example:
Expand|Select|Wrap|Line Numbers
  1. <form action="?" method="post">
  2.   <input type="checkbox" name="boxes[]" value="box1" /><br />
  3.   <input type="checkbox" name="boxes[]" value="box2" /><br />
  4.   <input type="checkbox" name="boxes[]" value="box3" /><br />
  5.   <input type="submit" />
  6. </form>
  7. <?php
  8. if(isset($_POST['boxes']))
  9. {
  10.   $rows = array();
  11.   foreach($_POST['boxes'] as $_box)
  12.   {
  13.     $rows[] = "(NOW(), '{$_box}')";
  14.   }
  15.  
  16.   if(count($rows) > 0)
  17.   {
  18.     $sql = "INSERT INTO `tbl`(`Date`, `Box`) VALUES";
  19.     $sql .= implode(", ", $rows);
  20.  
  21.     echo "<pre>", $sql, "</pre>";
  22.   }
  23. }
  24. ?>
  25.  
You would want to sanitize the box value before using it tho. I skipped that part to keep things simple.
Oct 8 '08 #6

Post your reply

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