Inserting different form elements in mysql trough jquery - php

I can't seem to figure this one out, nor how to start my query.
I have a form with different input field ID's
(customer_relationid, customer_brand, customer_goal, customer_floors, ....)
I have a mysql table with 3 columns (auto increment id, Key, Value)
The goal is to post all the info from the form row by row in mysql so that it results in this :
column id
this is just a auto increment, not important in this case
column KEY
relationid
brand
goal
floors
....
column VALUE
15
sony
music
3
.....
This is part of my form (the rest of the fields is the same):
<form id="calculator_form">
<fieldset id="generalInfo">
<legend>General Info</legend>
<h2>General Info</h2>
<label for="calculator_relationID">relationID:</label>
<input type="text" name="calculator_relationID" style="width:250px;" id="calculator_relationID" class="calculator_relationID">
<label for="calculator_brand">Brand:</label>
<input type="text" name="calculator_brand" style="width:250px;" id="calculator_brand" class="calculator_brand">
<label for="calculator_goal">Goal:</label>
<input type="text" name="calculator_goal" style="width:250px;" id="calculator_goal" class="calculator_goal">
<label for="calculator_floors">Floors:</label>
<input type="text" name="calculator_floors" style="width:250px;" id="calculator_floors" class="calculator_floors">
...
</fieldset>
How can I create the query as such so it knows it needs to insert it row by row for each form element?
Tnx in advance!

Related

How to display the last field which is user selected from form builder with the field name and field type?

I am making the form builder and I have an input field which is called as Firstname, Lastname, Email, Iagree. Whatever user choose the field from the form builder page, all the elements will store in the database after clicking on a button.
Like I choose elements Firstname, Lastname, Email and iagree from my form builder page and saved it. In the database, it will store like below example
Now I update the form builder and I change the form elements. It's displaying like below image.
Using below query I am getting the last record updated
SELECT form_id,form_elements,form_builder_type, update_date FROM `form_builder` WHERE form_id=1 AND form_builder_type='example' ORDER BY update_date DESC LIMIT 1
the output of above query
Now I have the second table with field name and field type
I have to join both the table to get the field name and type of the elements.
Now I have to display the last elements of the form builder with field name and field type. I mean it will display the last name and email with field type.
I am using CodeIgniter, my controller
$inst_id=1;
$result['data']=$this->formbuilder_Model->check_example_form_builder_fields($inst_id);
$this->load->view('user-form',$result);//passing elements to the view page
View
foreach ($data as $key) {
$exp_fields_name=$key->fields_name;
$exp_fields_type=$key->fields_type;
$exp_form_elements=$key->form_elements;
$abc=explode(',',$exp_form_elements);
foreach ($abc as $value) {
if ($exp_fields_name == $value) {?>
<div class="form-group row label-capitals">
<label class="col-sm-5 col-form-label"><?php echo $exp_fields_name;?></label>
<div class="col-sm-7">
<input type="<?php echo $exp_fields_type;?>" name="<?php echo $exp_fields_name;?>" placeholder="<?php echo $value;?>" class="form-control" />
<?php echo form_error($exp_fields_name); ?>
</div>
</div>
<?php
}}}?>
I need a output like
<label>Lastname</label>
<input type="text" name="lastname">
<label>Email</label>
<input type="email" name="email">
Would you help me out in this?
Rather than storing form_elements in fom_builder table, store their respective id and then use query something like this
SELECT form_elements_id FROM form_builder WHERE form_id=1 AND form_builder_type='example' ORDER BY update_date DESC LIMIT 1
Once you get all form elements Id implode it and convert it into array and use whereIn clause to get data from second table.

How to get add, edit, next & previous buttons on HTML form

I have an entry form to post data into a MySQL database (with a submit button) and it works fine. Now I want to have edit, next & previous buttons on it, to get next and previous record and also to edit them if needed.
I have searched on the internet but could not find a solution according to my requirement.
First of all, you should ensure that you have a column for the record ID that's an INTEGER, a PRIMARY KEY, and set to AUTOINCREMENT, let's call this `recordID`.
Let's take an example person table schema:
CREATE TABLE people (
recordID INTEGER PRIMARY KEY AUTOINCREMENT,
firstName VARCHAR(140) NOT NULL,
middleNames VARCHAR(250),
lastName VARCHAR(140) NOT NULL,
dateOfBirth DATE NOT NULL
);
To query the first record, we can do:
SELECT * FROM people WHERE recordID = 1;
Now to edit the record, we can do:
UPDATE people SET firstName="NewName" WHERE recordID = 1;
Next we build an HTML form to display/edit this data in.
<form action="#" method="post">
<input type="text" readonly="readonly" name="recordID" id="recordID" />
<input type="text" name="firstName" id="firstName" />
<input type="text" name="middleNames" id="middleNames" />
<input type="text" name="lastName" id="lastName" />
<input type="date" name="dateOfBirth" id="dateOfBirth" />
<input type="submit" />
</form>
Last of all you create some next and previous buttons to traverse through the records and populate the input fields, and then an edit button that sends the data to the server for it to update the database.
If you're feeling extravagant, you could use SQL's INSERT INTO ... ON DUPLICATE KEY UPDATE. E.g.:
INSERT INTO people (firstName, lastName, middleNames, dateOfBirth) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE firstName="?", lastName="?", middleNames="?", dateOfBirth="?";
In order to do an "in-place edit", you could add a variable to the querystring.
For example, if you want to edit recordID = 3, you could have the URL as: http://yourserver.com/person/?id=3&edit.
On the serverside you can check for edit by using isset($_GET['edit']). If that returns true, than run your edit code and populate the fields/enable the edit functionality.

I am creating database for checkbox with different column. How to insert the values to db?

I am creating database for checkbox values with different column. How to insert the db?because I have stored the check box values in different column name,my column name is allsubject,science,maths.
my questions are:
1.if user checks allsubject,the value is inserted, the other two column is going with null values.
2.is this way of storing value is correct because among three check box user select any values, I want to clarify this?
3.More over I want to store the values in this manner only
dbstructure:
**allsubject science maths**
allsubject science maths
allsubject science
maths
allsubject
Myform:
<form name="f1" action="" method="post">
Student Name:<input type="text" name="sname" value=""/>
All Subject:<input type="checkbox" name="allsubject" value="allsubject"/>
Science<input type="checkbox" name="science" value="science"/>
Maths<input type="checkbox" name="maths" value="maths"/>
<input type="submit" name="submit" value="submit"/>
</form>
**myphp value is:**
<?php
mysql_connect('localhost','root','');
mysql_select_db('checkbox');
if(isset($_POST['submit'])=='submit')
{
$allsubject=$_POST['allsubject'];
$science=$_POST['science'];
$maths=$_POST['maths'];
$sql=mysql_query("insert into studentinfo (allsubject,science,maths) values ('".$allsubject."','".$science."','".$maths."')");
}
?>
if(isset($_POST['allsubject']){$allsubject=1;} (OR VALUE YOU WANT)

How do I search my table for a form variable and add to the table if not found but increment the value by the user specified quantity if it is found?

I have the following form in my main page:
<left><h2><font color="ghostwhite">Enter Part</font></h2></left>
<form action="addpart.php" method="post">
<font color="ghostwhite">Date:&nbsp&nbsp&nbsp& &nbsp&nbsp</font> <input type="text" name="date" /><br>
<font color="ghostwhite">Part Number:&nbsp&nbsp&nbsp&nbsp&nbsp</font> <input type="text" name="partnum" /><br>
<font color="ghostwhite">Location:&nbsp&nbsp&nbsp&nbsp&nbsp</font> <input type="text" name="location" /><br>
<font color="ghostwhite">Quantity:&nbsp&nbsp&nbsp&nbsp&nbsp</font> <input type="text" name="quantity" /><br>
<input type="submit" />
</form>
I have the following php that I'm wanting to do the following:
Insert the $date entered by the user into the date column in the project database.
Insert the $partnumber entered by the user into the part_name column in the project database.
Insert the $location entered by the user into the location column in the project database.
Insert the $quantity entered by the user into the quantity column in the project database.
Here is the problem I'm having. I can't figure out how to search the part_name column for what the user entered and if it's not there enter it in with the quantity the user specified, but increment by the user specified amount if it is found.
I'm thinking it would be something like:
$dup = mysql_query("SELECT * FROM parts(part_name) WHERE VALUE($partnumber)
if ($dup)
{
mysql_query("UPDATE parts SET part_name = part_name + $quantity");
}
else
{
mysql_query("INSERT INTO parts (time, part_name, location, quantity) VALUES($date, $partnumber, $location, $quantity)");
}
Any help would be appreciated, thanks.
Have a look at:
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
Should do what your asking
No comments on the html.

display same form multiple times in PHP

I have a table in database in which there is a column named roll_no, there are other four columns in the table. I am having a form to insert data into this table. I want to display this form x number of times, where x is number of distinct values of roll_no. That means if roll_no have 50 distinct values then the form will be displayed for 50 times on the same page. And in the form the roll_no field should be filled automatically with the value fetched from the database.
The other 4 fields are start_date, end_date, total_lectures, attended_lectures.
suppose start_date = 7th and end_date = 20 so there are total 13 lectures and out of which roll_no 1 attended 10 lectures and roll_no 2 attended 7 lectures and so on. The teachers fills the data like this in the form and then submit the form and data gets updated in the table. the combination of roll_no, start_date and end_date together serves as primary key.
If you have used phpmyadmin then you know that when we click on insert it shows two different forms to insert data.
I also want to accomplish something like this but the number of forms will be about 60 maximum.
I can't for the life of me figure out what you're trying to do, but if I understand you correctly, you want to print something as often as there are distinct values in the database.
(Pardon me, but I still use the old-school MySQL functions so if you're using the newer ones you might need to change this somewhat.)
$distinct_query = mysql_query("
SELECT DISTINCT row_no
FROM table_name
");
for ($i = 0; $i < mysql_num_rows($distinct_query); $i++) {
?>
All the stuff you want to print, where <?php print $i; ?> is the iteration of your form.<br />
<?php
}
Please note that this code is untested.
You could simply add a unique number to each form element as you ouput each sub-form
1. <input type="text" name="roll_no_1" ... />
<input type="text" name="start_date_1" ... />
etc...
2. <input type="text" name="roll_no_2" ... />
<input type="text" name="start_date_2" ... />
etc...
...
50. <input type="text" name="roll_no_50" ... />
<input type="text" name="start_date_50" ... />
etc...
or, if you trust the users to not mangle the form, you can use PHP's array syntax for form fields:
1. <input type="text" name="roll_no[]" ... />
<input type="text" name="start_date[]" ... />
2. <input type="text" name="roll_no[]" ... />
<input type="text" name="start_date_[]" ... />
...
50. <input type="text" name="roll_no[]" ... />
<input type="text" name="start_date[]" ... />
Either way works. The first has the benefits of guaranteeing that all the related fields have the same "subscript" identification within the name. The other one makes it simpler to simply do foreach ($_POST['roll_no'] as $idx => $val) { ... }

Categories