Dynamic Dropdown mysql apostrophe issue - php

Alright then I'm having an issue when trying to fill a dropdown with MySQL information. The problem occurs when on the second dropdown I try to get information from things with apostrophes... such as women's clothing or men's clothing. Any help with be greatly appreciated.
Here is the error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Clothing'' at line 1
Here is the code.
<?php
//**************************************
// Page load dropdown results //
//**************************************
function getTierOne()
{
$catresult = mysql_query("SELECT DISTINCT category FROM categories")
or die(mysql_error());
while($tier = mysql_fetch_array( $catresult ))
{
echo '<option value="'.$tier['category'].'">'.$tier['category'].'</option>';
}
}
//**************************************
// First selection results //
//**************************************
if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
drop_1($_GET['drop_var']);
}
function drop_1($drop_var)
{
include_once('db.php');
$result = mysql_query("SELECT DISTINCT level1 FROM categories WHERE category='$drop_var'")
or die(mysql_error());
echo '<select name="drop_2" id="drop_2">
<option value=" " disabled="disabled" selected="selected">Choose one</option>';
while($drop_2 = mysql_fetch_array( $result ))
{
echo '<option value="'.$drop_2['level1'].'">'.$drop_2['level1'].'</option>';
}
echo '</select>';
echo "<script type=\"text/javascript\">
$('#wait_2').hide();
$('#drop_2').change(function(){
$('#wait_2').show();
$('#result_2').hide();
$.get(\"pla2.php\", {
func: \"drop_2\",
drop_var: $('#drop_2').val()
}, function(response){
$('#result_2').fadeOut();
setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
});
return false;
});
</script>";
}
//**************************************
// Second selection results //
//**************************************
if($_GET['func'] == "drop_2" && isset($_GET['func'])) {
drop_2($_GET['drop_var']);
}
function drop_2($drop_var)
{
include_once('db.php');
$bresult = mysql_query("SELECT DISTINCT level2 FROM categories WHERE level1='$drop_var'")
or die(mysql_error());
echo '<select name="drop_3" id="drop_3">
<option value=" " disabled="disabled" selected="selected">Choose one</option>';
while($drop_3 = mysql_fetch_array( $bresult ))
{
echo '<option value="'.$drop_3['level2'].'">'.$drop_3['level2'].'</option>';
}
echo '</select> ';
echo '<input type="submit" name="submit" value="Submit" />';
}
?>

Use htmlentities?
.htmlenteties($drop_2['level2']).

I figured it out. I had to change the
"SELECT DISTINCT level2 FROM categories WHERE level1='$drop_var'"
to
"SELECT DISTINCT level2 FROM categories WHERE level1='".mysql_real_escape_string$drop_var"'"

You can replace it with html special chars, by replacing with the single quote char.
See the list here:

Related

How to populate Dropdown values from database in php [duplicate]

This question already has answers here:
Using PHP to populate a <select></select> dropdown? [duplicate]
(7 answers)
Closed 7 months ago.
Hello Friends how to get data from database in Drop-down list?
This is my code but i can't get data in Drop-down list,
This is my php code.
public function System_parameter_dropdown_value() {
$sql= " SELECT * FROM SYSTEM_PARAMETERS WHERE PARAMETER_KEY='ROLE_TYPE'";
$rs= mysql_query($sql) or die(mysql_error());
$option_list="<option value=0>Select Role Type</option>";
while($data= mysql_fetch_assoc($rs)) {
$option_list.="<option value='$data[PARAMETER_KEY]'>
$data[PARAMETER_VALUE]</option>";
}
return $option_list;
}
This is my html option list, When i run the code it's show nothing in Dropdown list
<label class="control-label" for="roleid" style="padding-right: 80px;">ROLE TYPE</label>
<select id="" name="roleid">
<?php echo $option_list['PARAMETER_KEY']['PARAMETER_VALUE'];?>
</select>
I am Stuck on this code , please Guide me How to Resolve it.
Try some thing like this:
$arr = array(1 => 'MP', 2 => 'UP');
echo '<select>';
foreach($arr as $key => $val)
{
echo '<option value="'.$key.'">'.$val.'</option>';
}
echo '</select>';
It will generate html like:
<select>
<option value="1">MP</option>
<option value="2">UP</option>
</select>
In your case it is like:
<select id="" name="roleid">
<?php echo System_parameter_dropdown_value();?>
</select>
that's because $option_list in not array but string:
<?php $option_list = $your_object->System_parameter_dropdown_value(); ?>
<label class="control-label" for="roleid" style="padding-right: 80px;">ROLE TYPE</label>
<select id="" name="roleid">
<?php echo $option_list;?>
</select>
Method should be changed too
public function System_parameter_dropdown_value()
{
$sql= " SELECT * FROM SYSTEM_PARAMETERS WHERE PARAMETER_KEY='ROLE_TYPE'";
$rs= mysql_query($sql) or die(mysql_error());
$option_list="<option value=0>Select Role Type</option>";
while($data= mysql_fetch_assoc($rs))
{
$option_list.=sprintf("<option value='%s'>%s</option>", $data['PARAMETER_KEY'], $data['PARAMETER_VALUE']);
}
return $option_list;
}
If you are writing the function System_parameter_dropdown_value inside a class, you have to create an object of the class and then call the function.
Like this
$classObj = new className();
echo $classObj->System_parameter_dropdown_value();
class className {
public function System_parameter_dropdown_value() {
$sql= " SELECT * FROM SYSTEM_PARAMETERS WHERE PARAMETER_KEY='ROLE_TYPE'";
$rs= mysql_query($sql) or die(mysql_error());
$option_list="<option value=0>Select Role Type</option>";
while($data= mysql_fetch_assoc($rs)) {
$option_list.="<option value='$data[PARAMETER_KEY]'>
$data[PARAMETER_VALUE]</option>";
}
return $option_list;
}
}
I think this might help you.
I hope this one helps, it's complete example with the db connection.
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT record FROM table";
$result = mysql_query($sql);
echo "<select name='tagName'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['tagName'] ."'>" . $row['tagName'] .</option>";
}
echo "</select>";

Output of autocomplete populated from mysql tables

My textboxes get autocomplete populated from mysql tables.
I want to display the output list from the textbox into a selectable option instead of an list item.
echo '<li onClick="fill(\''.$result->naam_klant.'\');">'.$result->naam_klant.'</li>';
My code so far:
'<select onClick="fill(\''.$result->naam_klant.'\');"><option value=$result->naam_klant></option></select>';
Can you guys help me with this ?
UPDATE
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
// Is the string length greater than 0?
if(strlen($queryString) >0) {
$query = $db->query("SELECT naam_klant FROM overboekingen WHERE naam_klant LIKE '$queryString%' LIMIT 10");
if($query) {
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\''.$result->naam_klant.'\');">'.$result->naam_klant.'</li>';
'<select onClick="fill(\''.$result->naam_klant.'\');"><option value=$result->naam_klant></option></select>';
}
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
} // There is a queryString.
} else {
echo 'There should be no direct access to this naam_klant script!';
}
}
?>
You don't give enough information, how you is your results structured?
<select onchange="fill(this.value);" onfocus="this.selectedIndex = -1;">
<?
for ($i=0; $i < count(results); $i++) {
$result = results[i];
echo "<option value='{$result->naam_klant}'>option {$result->naam_klant}</option>\r\n";
}
?>
</select>
Update
Since you want a selectable option instead of an list item
if ($query) {
echo '<select onchange="fill(this.value);" onfocus="this.selectedIndex = -1;">\r\n';
while ($result = $query->fetch_object()) {
echo '<option value={$result->naam_klant}>{$result->naam_klant}</option>\r\n';
}
echo '</select>\r\n';
}

Chained dropdown option not pulling previous values through in to query

I have found a example of chained dropdown boxes, however i am struggling to ensure that the 3rd box uses both the previous boxes in showing the required results.
The boxes are intended to show the following,
DROP DOWN BOX 1 = Select a sector
DROP DOWN BOX 2 = Select a Level
DROP DOWN BOX 3 = Select a Qualification
On the last SQL query i have shown a $HELP, i think this is where im having my problems, i think it is loosing the previously stored value in dropdown box 1 when dropdown box 2 is selected.
<?php
//**************************************
// Page load dropdown results //
//**************************************
function getTierOne()
{
$result = mysql_query("SELECT DISTINCT SSA1Text FROM qualifications ORDER BY SSA1Text ASC")
or die(mysql_error());
while($tier = mysql_fetch_array( $result ))
{
echo '<option value="'.$tier['SSA1Text'].'">'.$tier['SSA1Text'].'</option>';
}
}
//**************************************
// First selection results //
//**************************************
if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
drop_1($_GET['drop_var']);
}
function drop_1($drop_var)
{
include_once('db.php');
$result = mysql_query("SELECT DISTINCT Level FROM qualifications WHERE SSA1Text='$drop_var' ORDER BY Level ASC")
or die(mysql_error());
echo '<select name="drop_2" id="drop_2">
<option value=" " disabled="disabled" selected="selected">Choose one</option>';
while($drop_2 = mysql_fetch_array( $result ))
{
echo '<option value="'.$drop_2['Level'].'">'.$drop_2['Level'].'</option>';
}
echo '</select>';
echo "<script type=\"text/javascript\">
$('#wait_2').hide();
$('#drop_2').change(function(){
$('#wait_2').show();
$('#result_2').hide();
$.get(\"func.php\", {
func: \"drop_2\",
drop_var: $('#drop_2').val()
}, function(response){
$('#result_2').fadeOut();
setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
});
return false;
});
</script>";
}
{//**************************************
// Second selection results //
//**************************************
if($_GET['func'] == "drop_2" && isset($_GET['func'])) {
drop_2($_GET['drop_var']);
}
function drop_2($drop_var)
{
include_once('db.php');
$result = mysql_query("SELECT * FROM qualifications WHERE Level='$drop_var' AND SSA1Text='$HELP'")
or die(mysql_error());
echo '<select name="drop_3" id="drop_3">
<option value=" " disabled="disabled" selected="selected">Choose one</option>';
while($drop_3 = mysql_fetch_array( $result ))
{
echo '<option value="'.$drop_3['Title'].'">'.$drop_3['Title'].'</option>';
}
echo '</select> ';
echo '<input type="submit" name="submit" value="Submit" />';
}
?>
Any help would be much appreciated.
You could pass both select variables to drop_2 function?
function drop_2($drop_var1, $drop_var2 == null) {
if ($drop_var2 !== null) {
$sqlSnipet = " AND SSA1Text='$drop_var2'"
}
$result =
mysql_query(
"SELECT *
FROM qualifications
WHERE Level='$drop_var' " .
$sqlSnipet
) or die(mysql_error());
}

Edit drop down but not showing selected value from mysql data base in php

I am new to php, i created drop down which calling data from mysql data base, user selects option and its save to data base.
Problem Arises in edit form in which its do not showing selected value.
Drop Down code is below:
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
echo "<option value='$heading'>$heading\n";
}
echo "</select>"
Please advise solution for the edit form.
Thanks in Advance
you must close <option> tag:
echo "<option value='$heading'>$heading</option>";
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
?>
<option <?php if($heading=="SOMETHING") { echo "selected='selected'"; } ?> value="SOMETHING">SOMETHING</option>
<option <?php if($heading=="SOMETHING2") { echo "selected='selected'"; } ?> value="SOMETHING2">SOMETHING2</option>
<option <?php if($heading=="SOMETHING3") { echo "selected='selected'"; } ?> value="SOMETHING3">SOMETHING3</option>
<?php
}
echo "</select>"
I'd do it this way.
$numrows = mysql_num_rows($result);
if ($numrows != 0){
echo "<select name='owner'>\name";
while ($x = mysql_fetch_assoc($result)){
echo "<option value='".$x['heading']."'>".$x['heading']."</option>";
}
echo "</select>";
}
$x['heading'] is using the value of the row 'heading' in the database
It's much more efficient and simply looks more sophisticated.

Ajax(ify) Combox populated by PHP array

I'm still learning how Ajax works, and as such I'm having a lot of trouble taking my current PHP combo box, and making it Ajaxified. The combo box choices are populated by a PHP array, and represent the number of images I wish to display on a page. Right now all the code is in one PHP file, although I'm pretty sure when it's in Ajax, it will have to be in two pages.
Oh, and if you could use jQuery, it would be much appreciated.
<?php
$curPage = 0;
if(isset($_GET['page'])){
$curPage = (int) $_GET['page'];
}
// values of combobox in an array
$imgNum_values = array('12','16','20');
if(isset($_GET['imgs']) && in_array($_GET['imgs'], $imgNum_values))
{
$selected_imgNum = $_GET['imgs'];
}else{
// input default value, if empty the first variable will be shown
$selected_imgNum = '';
}
$option_num = count($imgNum_values);
echo '
<form name=imgNum method="get" action="new_arrivals_img.php">
<label>number of images per page:</label>
<select name="imgs" onChange="imgNum.submit();">';
for($x = 0; $x < $option_num; $x++)
{
// print the options
echo '
<option value="'.$imgNum_values[$x].'"'.($imgNum_values[$x] == $selected_imgNum ?
' selected="selected"' : '').'>'.$imgNum_values[$x].'</option>';
}
echo '
</select>
<input type="hidden" name="page" value="<?php echo $curPage; ?>" />
</form>';
?>
Below is the PHP query etc for the images displayed. I don't need the below section changed into Ajax at the moment, unless it's essential to the above code being changed into Ajax.
<?php
if((int) $_GET['imgs'] > 0){
$limit = (int) $_GET['imgs'];
} else {
$limit = 12;
}
$mysql_link = mysql_connect("localhost", "root", "root");
mysql_select_db("new_arrivals_imgs") or die("Could not select database");
$query = mysql_query("SELECT `imgURL`,`imgTitle` FROM `images` ".
"ORDER BY `imgDate` DESC LIMIT " . $limit * $curPage . ", $limit") or die(mysql_error());
if(!$query) {
echo "Cannot retrieve information from database.";
} else {
while($row = mysql_fetch_assoc($query)) {
echo "<li><a href='new_arrivals_img/".$row['imgURL']."' class='gallery' title='".$row['imgTitle']."'><img src='new_arrivals_img/thumbnails/".$row['imgURL']."'></a></li>";
}
}
?>
Thanks so much for any help.
I'll give you an example on how you could do this with jQuery:
The HTML:
<form>
<label>Images Number:</label>
<select id="imgNum" name="imgNum">
<option value="12">12</option>
<option value="16">16</option>
<option value="20">20</option>
</select>
</form>
<div id="imgTray"></div>
The JavaScript(jQuery):
//Bind the onChange event to Fetch images on combox selection
$("#imgNum").change(function(){
//The combo box
var sel = $(this);
//Selected value
var value = sel.value();
//Feth the images
$.get("get_images.php",{imgs: value}, function(data){
//Add images(or what ever the script output is) to the document
$("#imgTray").html(data);
});
})
//You should store the current selected option in a cookie
//For the sake of the example i'll set the default permanently to 12
var imgNum_selected = 12;
//set the initial selected option and trigger the event
$("#imgNum [value='"+imgNum_selected+"']")
.prop("selected","selected")
.change();
I'll assumed you know where to put the jQuery code
The PHP(get_images.php):
<?php
if((int) $_GET['imgs'] > 0){
$limit = (int) $_GET['imgs'];
} else {
$limit = 12;
}
$mysql_link = mysql_connect("localhost", "root", "root");
mysql_select_db("new_arrivals_imgs") or die("Could not select database");
$query = mysql_query("SELECT `imgURL`,`imgTitle` FROM `images` ".
"ORDER BY `imgDate` DESC LIMIT " . $limit * $curPage . ", $limit") or die(mysql_error());
if(!$query) {
echo "Cannot retrieve information from database.";
} else {
while($row = mysql_fetch_assoc($query)) {
echo "<li><a href='new_arrivals_img/".$row['imgURL']."' class='gallery' title='".$row['imgTitle']."'><img src='new_arrivals_img/thumbnails/".$row['imgURL']."'></a></li>";
}
}
?>
And by the way i did not test this, i hope you find something useful

Categories