473,326 Members | 2,173 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,326 software developers and data experts.

insert data on mysql thru php

ddtpmyra
333 100+
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
5 1512
chelvan
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
6,050 Expert 4TB
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
333 100+
Markus that's the thing how can I insert all the 3 information in one column in one query
Oct 8 '08 #4
Markus
6,050 Expert 4TB
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
5,058 Expert 4TB
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

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

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
2
by: Atz | last post by:
Hi to all ! Im using MySql front and PHP 5 for some web shop. I didn't try it so far but i guess that the online data insertation ( accept costs and time ) should be the same process like for...
10
by: Python_it | last post by:
Python 2.4 MySQL-python.exe-1.2.0.win32-py2.4.zip How can I insert a NULL value in a table (MySQL-database). I can't set a var to NULL? Or is there a other possibility? My var must be variable...
1
by: Joe | last post by:
Hello All, I am trying to insert a record in the MS Access DB and for some reason I cannot get rid of error message, System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. ...
2
by: bballr | last post by:
I have a problem that I'm not sure if it can be done. I'm trying to use the MySQL C API to be able to use a normal sql insert statement that will send the data or file to a directory and NOT the...
6
by: VitaminB | last post by:
Hi Guys, I try to insert data from a form into a mysql database, but it did not work... There is no error, but the data did not pass thru the table. This is the short script:
1
by: sheenaa | last post by:
Hello Members, I m creating my application forms in ASP.Net 2005 C# using the backend SQL Server 2005. What i have used on forms :: ? On my first form i have used some...
2
josie23
by: josie23 | last post by:
Egad, I'm not a coder/programmer by nature or occupation but understand things like html and css and a small amount of perl. So, basically, I'm a perl/mysql imbecile. But, I've been trying to...
9
by: jith87 | last post by:
My proj involves JSP @ front end n MySql @ back end and connectivity is established thru JDBC. The thing is when i store values from the front end into the db i need to change the data types n vice...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.