How to set same id for multiple rows in a table - php

I have a table named wp_shortcode. When a form is data from the form will be inserted to multiple rows into the table. Below given is my form:
<form method="POST" action="" class="form1">
<input type="submit" name="submit" value="Generate"><br>
Post Type
<select class="taxonomy">
<option value="">Select</option>
</select>
<br>
Category
<select class="cat">
<option value="">Select</option>
</select>
<h3>Questions</h3>
<button class="new_btn">Add New</button><br>
<div class="add_ques">
<span>Question1</span>
<textarea name="ques1" id="ques1"></textarea>
<br>
Option1
<select class="option" name="opt1" id="opt1">
<option value="1">1</option>
<option value="2">2</option>
</select>
<br>
</div>
</form>
The add_ques div section will be multiplied on add new button click.I need to insert the data from the form to wp_shortcode table.Currently the table have s_id,post_types,category,question,option columns.
How can i add a common id to the rows generated during form submission.

Here is the solution:
I add an addition column group_id to the table. And when we submit the form we will check group_id values:
$post_id = $wpdb->get_results("SELECT group_id FROM $table_name");
$siz = sizeof($post_id);
$index = $siz-1;
$gid = $post_id[$index];
$group = $gid->group_id;
$group_id = (int)$group;
And last group_id value is checked:
if($group_id == 0){
$id = 1;
}
else{
$id = $group_id+1;
}
This id is inserted along with other values into table. By this set of data inserted during of form submission will get same id.

The field name should be an array, then you will get the values in post array in PHP ($_POST['ques'] and $_POST['opt'])Try this:
<div class="add_ques">
<span>Question1</span>
<textarea name="ques[]" id="ques1"></textarea><br>
Option1<select class="option" name="opt[]" id="opt1">
<option value="1">1</option>
<option value="2">2</option>
</select><br>
</div>
Also, the id of the fields should be unique.

Related

How to combine several dropdown list values in mysql query?

<form method="post" action="search.php">
<select name="Num[]">
<option value="">Select One</option>
<option value="numofpeople">Number of people</option>
</select>
<select name="op[]">
<option value="">Select One</option>
<option value=">4">>4</option>
<option value=">2">>2</option>
</select>
<br />
<select name="num2[]">
<option value="">Select One</option>
<option value="price">Price</option>
</select>
<select name="op2[]">
<option value="">Select One</option>
<option value="<20000"><20000</option>
<option value="<40000"><40000</option>
</select>
<br />
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</form>
Here is my code to provide a dropdown list for the user. It will act as a filter to generate more specific result through mysql and don't get the logic to combine these dropdown values.
I would like to have something like:
Select * from hotel where price < 40000;
and I will provide all keywords after "Where".
Select boxes name are defined as array in html and your requirement is a user can select a single option in a select box. So you need to update the name of select boxes. You no need to add < in select box values, These conditions used at server end. I have corrected the HTML.
<form method="post" action="">
<select name="Num">
<option value="">Select One</option>
<option value="numofpeople">Number of people</option>
</select>
<select name="op">
<option value="">Select One</option>
<option value="4">>4</option>
<option value="2">>2</option>
</select>
<br />
<select name="num2">
<option value="">Select One</option>
<option value="price">Price</option>
</select>
<select name="op2">
<option value="">Select One</option>
<option value="20000"><20000</option>
<option value="40000"><40000</option>
</select>
<br />
<div class="input-group-btn">
<button class="btn btn-default" type="submit" name="search_btn"><i class="glyphicon glyphicon-search"></i> Search</button>
</div>
</form>
Server Side code for handle selected values and add to query.
if(isset($_POST['search_btn'])){
$conditons = array();
if(isset($_POST['Num'])){
$conditons[] = '`people` = "'.$_POST['Num'].'"';
}
if(isset($_POST['op'])){
$conditons[] = '`option` < '.$_POST['op'];
}
if(isset($_POST['price'])){
$conditons[] = '`price` = '.$_POST['price'];
}
if(isset($_POST['op2'])){
$conditons[] = '`op2` < '.$_POST['op2'];
}
$conditons = implode(' & ', $conditons);
echo $query = 'select * from hotel where '.$conditons;
}
You an update your real field name in conditions. After added conditions, result query will be as -
select * from hotel where `people` = "numofpeople" & `option` < 4 & `op2` < 20000
Here is a very generic example that I just put together. This code is not tested, but I have written like this before.
<?php
// Checks to see if post variables are available
if(isset($_POST)) {
// Creates original command
$query = "SELECT * FROM hotel WHERE "
// gets each value from the post
foreach($_POST as $key => $value) {
$query .= $key . "=" . $value . "AND ";
}
// Removes the trailing "AND "
$query = substr($query, 0, -4);
echo $query;
}
If you need to switch things up depending on key, you can just do a switch inside of the foreach statement that will check the key value. The key is the "name" attr that is assigned in the HTML that you are posting from.
This method is also not sanitized because I did not get a response to which connection type you are using, but it shouldn't be that hard. If you need me to write it, then I would be more than happy to do so.

passing values to sql through check boxes and dropdowns

How can i pass Drop down values to sql database and also the check box for example if a user selects English and maths than the value inserted in to the database would be 1 or else the value would be 0
<form>
<p id="p1">Select Your Year</p>
<select id="year_sel">
<option value="blank"></option>
<option id="primary" value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
<input type="checkbox" name="Math" value="Math">Math<br>
<input type="checkbox" name="English" value="English">English<br>
<input type="checkbox" name="HealthScience" value="HealthScience">Health Science<br>
<input class="sub_reg" type="submit" value="Register Subjects" />
</form>
this is how my database looks like
First, your <select id="year_sel"> needs a name attribute to post ->
<select id="year_sel" name="year_sel" >
Since you are using <form> the default is get, so you would get the selected value in $_GET
$year_sel = $_GET['year_sel'];
If you changed it to
<form method="post">
then you would get it in $_POST
$year_sel = $_POST['year_sel']
Second, checkboxes are only posted if checked, so you can use isset() to set a value using a ternary -
$math = isset($_GET['Math']) ? 1 : 0;
$english = isset($_GET['English']) ? 1 : 0;
...[rest of your checkboxes]...
swap $_GET/$_POST like the select
$math = isset($_POST['Math']) ? 1 : 0;
$english = isset($_POST['English']) ? 1 : 0;
<select id="year_sel" name="year_sel">
<option value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
on your form action something.php file use this to get the select value
$language = $_POST["year_sel"]; // chose whetever your form method
establish the database connection please do refer some tutorials (if you don't know )
write the mysqli insert command like
$SQL = "INSERT INTO yourtable (language) VALUES ('$language')";
mysqli_real_escape_string($sql);
$result = mysqli_query($sql) or die (mysqli_error());
now you can insert this value in your mysql or any database table
this is not tested
<form>
<p id="p1">Select Your Year</p>
<select id="year_sel" name="year_sel">
<option value="blank"></option>
<option id="primary" value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
<?php
$variable = $_POST["year_sel"];
?>
Should mention the HTML ELEMENT name to get the value.

Submitting multiple values in php to a search query

I'm sorry I'm asking something very basic but I'm beyond stuck. I have a search form with 3 fields: a blank field for search term data, a dropdown box, and a checkbox list. My page is functioning as intended for the search term box.
However, I don't know what I need to do to be able to send the select value (one select choice) and checkbox values to the processing form.
This is my current query:
SELECT nameid, productypetid, typeid, termsid, endsid, sourceid FROM listings WHERE nameid LIKE %s ORDER BY nameid ASC
This is the html form:
<form action="results2.php" method="get" name="form" class="Ccenter">
<p>Product Name:
<input name="namesearch" type="text" id="namesearch" title="namesearch" />
</p>
<p>
<label for="category">Category:</label>
<select name="category" size="1" id="category">
<option value="Any" selected="selected">Any</option>
<option value="Baby and Kids">Baby and Kids</option>
<option value="Beauty and Hair">Beauty and Hair</option>
<option value="Beverages">Beverages</option>
<option value="Cleaning">Cleaning</option>
<option value="Condiments">Condiments</option>
<option value="Cooking and Baking">Cooking and Baking</option>
<option value="Dairy">Dairy</option>
<option value="Frozen">Frozen</option>
<option value="Meat">Meat</option>
<option value="Medicine">Medicine</option>
<option value="Other">Other</option>
<option value="Pantry Foods">Pantry Foods</option>
<option value="Paper Products">Paper Products</option>
<option value="Pets">Pets</option>
<option value="Restaurants">Restaurants</option>
<option value="Snacks and Candy">Snacks and Candy</option>
<option value="Toiletries">Toiletries</option>
</select>
</p>
<p>
<label>
Type:
<input name="Type" type="checkbox" id="manufacturer" value="manufacturer" checked="checked">
Manufacturer</label>
<label>
<input name="Type" type="checkbox" id="store" value="store" checked="checked">
Store</label>
If you're accessing the namesearch text field like so:
$name = $_GET['namesearch'];
Then your category select field will be available via:
$category = $_GET['category'];
As for your checkboxes (manufacturer and store), they may or may not exist in the $_GET array. It depends on whether the user has checked them or not.
$manufacturer = isset($_GET['manufacturer']) ? $manufacturer : null;
$store = isset($_GET['store']) ? $store: null;

Why does my HTML5 form not submit the value for my option list with PHP?

In my code everything is submitted fine except for the "category" field in the option drop down list. The PHP file that used $category = $_POST['category']; can't get the string of text associated with each option value, but assigns the other variables with no problem.
This information from the form is then put into my database, and the user can make a search - all works fine apart from the category field.
<form method="post" action="add_item_action.php">
<table>
<tr><td>Item Name:</td> <td><input type="text" name="name"></td></tr>
<tr><td>Vendor Name:</td> <td><textarea name="vendor"></textarea></td></tr><tr><td>Item Details:</td> <td><textarea name="description"></textarea></td></tr>
<tr><td>Item Start Price ($):</td> <td><textarea name="price"></textarea></td></tr>
<tr><td>Item </td>
<tr><td><label>Category:</label></td>
<td><select id = "category">
<option value= "1">Select an option</option>
<option value = "Electronics">Electronics</option>
<option value = "Cars">Cars</option>
<option value = "Fashion">Fashion</option>
<option value = "Pets">Pets</option>
<option value = "Miscellaneous">Miscellaneous</option>
<option value = "Books">Books</option>
<option value = "Sports">Sports</option>
</select>
</td></tr>
<tr><td colspan=2><input type="submit" value="Add item">
</table>
</form>
Jquery uses the "ID" or the "class" to identify your elements, however, for a webbrowser to send the form data back to the webserver upon submit, you need the "name" attribute:
<select id="category" name="category">
Just remember: forms need names!
Your select
<select id = "category">
must have a name to be posted
<select id = "category" name="category">
user input values withou a name will not be posted.
All your other have one :) why not your category ..?
add name attribute here
<select id = "category" name="category">
and access in add_item_action.php like this
$_POST['category']
In your select box you have just given
<select id = "category">
give it as
<select id = "category" name="category">
Should be name=category
<select id = "category" name="category">
<option value= "1">Select an option</option>
<option value = "Electronics">Electronics</option>
<option value = "Cars">Cars</option>
<option value = "Fashion">Fashion</option>
<option value = "Pets">Pets</option>
<option value = "Miscellaneous">Miscellaneous</option>
<option value = "Books">Books</option>
<option value = "Sports">Sports</option>
</select>

multiple options registering incorrectly when inserted into mysql table

I have 2 drop-down menu forms 'category' and 'sub-category'. in 'category' I have options music and film and in the subcategories I have options 'pop' and 'rock'(f1) and in film 'comedy'and 'drama'(f2). the problem is, with the code I have the subcategories for film should register as follows(cat,subcat): film-drama=2,5 and film-comedy=2,6 however they both register as 2,0 when entered in the mysql table. here is the code:
<form action='submitsite.php' method='POST'>
<table>
<tr>
<td>category(optional)</td>
<td><select name='cat' id = "opts" onchange = "showForm()">
<option value = "0">Select</option>
<option value = "1">music </option>
<option value = "2">film </option>
</select>
<div id = "f1" style="display:none">
<form name= "subcat">
<select name='subcat' id = "opts" onchange = "showForm()">
<option value = "0">Select</option>
<option value = "3">pop</option>
<option value = "4">rock </option>
</select>
</form>
</div>
<div id = "f2" style="display:none">
<form name= "subcat">
<select name='subcat' id = "opts" onchange = "showForm()">
<option value = "0">Select</option>
<option value = "5">comedy</option>
<option value = "6">drama</option>
</select>
</form>
</div>
<script type = "text/javascript">
function showForm()
{
var selopt = document.getElementById("opts").value;
if (selopt == 1)
{
document.getElementById("f1").style.display="block";
document.getElementById("f2").style.display="none";
}
if (selopt == 2)
{
document.getElementById("f2").style.display="block";
document.getElementById("f1").style.display="none";
}
}
and here is the variable declarations and mysql query:
$cat=$_POST['cat'];
$subcat=$_POST['subcat'];
$sql="INSERT INTO favorites VALUES('$cat','$subcat')";
it's not a mysql issue I just added that part to give a better overall picture.
You can't have nested forms in HTML. The markup is not valid and I guess you get unexpected behavior when submitting. The solution to this is using a single form. There's more than one way to do this, but here's one example:
HTML code:
<form action='submitsite.php' method='POST'>
<table>
<tr>
<td>category(optional)</td>
<td><select name='cat' id = "opts" onchange = "showForm()">
<option value = "0">Select</option>
<option value = "1">music </option>
<option value = "2">film </option>
</select>
<div id = "f1" style="display:none">
<select name='subcat1' id = "opts" onchange = "showForm()">
<option value = "0">Select</option>
<option value = "3">pop</option>
<option value = "4">rock </option>
</select>
</div>
<div id = "f2" style="display:none">
<select name='subcat2' id = "opts" onchange = "showForm()">
<option value = "0">Select</option>
<option value = "5">comedy</option>
<option value = "6">drama</option>
</select>
</div>
</form>
....
PHP code:
$cat = $_POST['cat'];
$subcat = $_POST['subcat' . $cat];
Keep in mind that all user-submitted values must be validated before being used further. In this case, you would check for is_numeric(), check to see if it's in your accepted range of values and maybe cast to an int.

Categories