Insert to a table column based on php variable - php

I have a comboBox that has options: transpo, meal and medical. I want to insert the values into the column that corresponds to option that the user chooses.
Is it possible?
This is my latest query.
$conn->query("INSERT INTO expenses
SET employee_id = :emp_id,
client_id = :client_id,
CASE
WHEN :cat == 'transpo' THEN transpo_exp = :worth
WHEN :cat == 'meal' THEN meal_exp = :worth
WHEN :cat == 'medical' THEN medical_exp = :worth");

please use array [] symbol in select name.
<select name="expenses[]" multiple="multiple">
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>
and get array variable.
$expenses= $_POST['expenses'];
for($i = 0; $i < count($expenses); $i++){
echo $expenses[$i];
$Qry = mysql_query("INSERT INTO expenses VALUES('$expenses[$i]]')");
}

Related

Collecting information from menu items printed from database (including dynamic creation of Quantity drop down and add to cart)

I am trying to link the dynamically created quantity drop down list to it's respective add to cart button.
Any help would be appreciated.
<?php
$sql = "SELECT Course_name as name, Calories, Remarks, PictureID, Price, Ingredients, Description, Days FROM I_MAIN_COURSE";
$result = mysqli_query($con,$sql);
if ($result->num_rows > 0) {
// output data of each row
print "<ul>";
while($row=mysqli_fetch_assoc($result)){
$n=$row['name'];
$c=$row['Calories'];
$i=$row['Ingredients'];
$p=$row['Price'];
$r=$row['Remarks'];
$d=$row['Days'];
$de=$row['Description'];
$_SESSION["name"]=$n;
print "<li>";
print "<p>Name:".$n."</p>";
print "<p>Calories: ".$c." cal</p>";
print "<p>Price: ".$p."</p>";
print "<p>Available Day: ".$d."</p>";
print "<p>Ingredients: ".$i."</p>";
print "<p>Description: ".$de."</p>";
print "<p>Remarks: ".$r."</p>";
?>
Quantity:
<select class="lol" name = "quantity">
<option value = "0">0</option>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
<option value = "4">4</option>
<option value = "5">5</option>
<option value = "6">6</option>
<option value = "7">7</option>
<option value = "8">8</option>
<option value = "9">9</option>
<option value = "10">10</option>
</select>
<button class="food" value="<?php echo $n ?>" > Add to Cart</button>
</li>
<?php
$count++;
}
print "</ul>";
}
?>
I presume that you mean you are not sure how to differentiate the quantity dropdowns from one cart item to the next. To do that you need to get the id (or whatever your unique auto incrementing column name is) of the item from your database and array the select input so your SQL should be something like:
//add ---------v
$sql = "SELECT id, Course_name as name, Calories, Remarks, PictureID, Price, Ingredients, Description, Days FROM I_MAIN_COURSE";
Then add that id to the select:
<select class="lol" name = "quantity[<?php echo $row['id']; ?>]">
You would get this when you submit:
Array
(
[quantity] => Array
(
[123] => 3
)
)

Dropdown menu to filter results in php get all results from sql

I have this drop down:
<select class="w-select filterdropdown" id="Tipo-de-cocina" name="tipofiltro" data-name="Tipo de cocina">
<?php
if (isset($_GET['tipofiltro'])) {
echo '<option value="' .$filtrocuisine. '"> ' .$filtrocuisine. "</option>";
} else {
echo '<option value="Todos los tipos">Todos los tipos</option>';
}
?>
<option value="Japonesa">Japonesa</option>
<option value="Mexicana">Mexicana</option>
<option value="India">India</option>
<option value="Mediterranea">Mediterranea</option>
<option value="Italiana">Italiana</option>
<option value="Americana">Americana</option>
<option value="Asiatica">Asiatica</option>
<option value="Thai">Thai</option>
<option value="China">China</option>
<option value="Francesa">Francesa</option>
<option value="Turca">Turca</option>
<option value="Latina">Latina</option>
<option value="Africana">Africana</option>
<option value="Griega">Griega</option>
<option value="Arabe">Arabe</option>
</select>
How can i make that when the user selects the field "Todos los tipos" my sql query returns all types? This is the sql behind:
if (isset($_GET['preciofiltro']) OR isset($_GET['preciofiltro'])) {
$filtroprecio = $_GET['preciofiltro'];
$filtrocuisine = $_GET['tipofiltro'];
$sql = "SELECT Meals.Meal_ID, Meals.Name, Price, Cooks.Cook_ID
FROM Meals
INNER JOIN Cooks ON Cooks.Cook_ID = Meals.Cook_ID
WHERE Cooks.Area = '$area'
AND Meals.Capacity > 0
AND Meals.Price < '$filtroprecio'
AND Meals.Type = '$filtrocuisine'";
$result = mysqli_query($conn, $sql);
Basically I would need something such as "AND Meals.Type = any"
Cheers!
Well there are simpler methods but without rewriting everything the simple answer is that if the user selects Todos los tipos from the dropdown what you actually want to do is remove this selection criteria AND Meals.Type = '$filtrocuisine' from the query completely i.e. you no longer limit the query with that criteria.
So change your script like this :-
I am of course assuming that you have taken data from the $_GET array, validated it, and cleansed it before we get to this code.
if (isset($_GET['preciofiltro']) OR isset($_GET['preciofiltro'])) {
$filtroprecio = $_GET['preciofiltro'];
$filtrocuisine = $_GET['tipofiltro'];
$sql = "SELECT Meals.Meal_ID, Meals.Name, Price, Cooks.Cook_ID
FROM Meals
INNER JOIN Cooks ON Cooks.Cook_ID = Meals.Cook_ID
WHERE Cooks.Area = '$area'
AND Meals.Capacity > 0
AND Meals.Price < '$filtroprecio'";
if ( isset($filtrocuisine) && $filtrocuisine == 'Todos los tipos' ) {
$sql .= " AND Meals.Type = '$filtrocuisine'";
}
$result = mysqli_query($conn, $sql);

PHP MySQL set selected selectpicker multiple

i need help for my php script with bootstrap selectpicker integration
i have php script for show selectpicker multiple value
<select id="skill" name="skill" class="selectpicker" multiple data-size="5" data-selected-text-format="count">
<?php
$skills = mysql_query("SELECT skill_id, skill_name FROM skill",$conn);
while($r_skills = mysql_fetch_assoc($skills)){
echo '<option value="'.$r_skills['skill_id'].'">'.$r_skills['skill_name'].'</option>
';
}
?>
</select>
and on my other table i have user and skill_id, for skill_id content : 1, 2, 3
i had explode it into each value like this :
$q = ("SELECT nik, skill_id FROM dosen WHERE nik = '".$nik."'",$conn);
while($row_dosen = mysql_fetch_assoc($q)){
$pieces = explode(",", $row_dosen['skill_id']);
for($i = 0; $i < count($pieces) ; $i++){
$pp = mysql_query("SELECT skill_id, skill_name FROM skill WHERE skill_id = '".$pieces[$i]."'",$conn);
while($p = mysql_fetch_assoc($pp)){
$var_each = $p['skill_id'];
}
}
}
The question is : how i can set default selected value for my multiple selectpicker option to make them selected as my user data on table ?
Thank you.
The help will be appreciated :)
How about something like:
<select id="skill" name="skill" class="selectpicker" multiple data-size="5" data-selected-text-format="count">
<?php
$skills = mysql_query("SELECT skill_id, skill_name FROM skill",$conn);
while($r_skills = mysql_fetch_assoc($skills)){
echo '<option value="'.$r_skills['skill_id'].'"';
if ($r_skills['skill_id'] == '1337 codingz')
echo ' selected';
echo '>'.$r_skills['skill_name'].'</option>';
}
?>
</select>

How to update mysql data when using explode

I have this code to load the month of the birthday that corresponds to the id number:
<?php
$query = "SELECT DISTINCT BIRTHDAY FROM student WHERE IDNO='".$_GET['id']."'";
if($result = mysql_query($query))
{
if($success = mysql_num_rows($result) > 0)
{
?>
<select title="- Select Month -" name="mm" id="mm" class="" >
<?php
while ($row = mysql_fetch_array($result))
list($year,$month,$day)=explode("-", $row['BIRTHDAY']);
?>
<option value="<?php echo $month;?>"><?php echo $month; ?></option>\n";
And this is the form action:
$birthday = mysql_real_escape_string($_POST['mm']);
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
What should I do, when I click on the update button, it executes, but I see the undefined offset error is stored in the mysql database and not the month.
I'm just a beginner, can you give me some tips on how can I achieve updating the data
In cases like this... you will have to use 3 selects and then join them to update the database... so, in the form you have something like this:
<select name='month'>
<option value='1'>January</option>
<option value='xx'>etc</option>
</select>
<select name='day'>
<option value='1'>1</option>
<option value='xx'>etc</option>
</select>
<select name='year'>
<option value='1980'>1980</option>
<option value='xx'>etc</option>
</select>
Then... the PHP that receives that data should do something like:
$year = mysql_real_escape_string($_REQUEST['year']);
$month = mysql_real_escape_string($_REQUEST['month']);
$day = mysql_real_escape_string($_REQUEST['day']);
$birthday = $year.'-'.$month.'-'.$day;
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
Of course... you have to verify first whether all variables are set or not. You can do so by using the isset method.
Check your update query, It may be wrong in that.
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
See this year and firstname, in this year is assigned to null character for you.
Just assign like this,
$birthday = mysql_real_escape_string($_POST['mm']);

PHP help with building query

I have several drop lists where if no option is selected then the value is = ""...
I cant figure out how to build the query for mysql in PHP.
query = SELECT * FROM db
I assume you have a select like this:
<select name="data[]" multiple="multiple">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
Your php can be something like
<?php
$data = array();
$data = $_POST['data'];
$query = "select * from table";
if (count($data > 0)) {
for ($i = 0; $i < count($data); $i++) {
$data[$i] = "'{$data[$i]}'";
}
$query .= " where field in (".implode(",", $data).")";
}
Too less information, but here's what I would do
$rows = $db->query(
'select *
from
table
where
checkbox_value = ?',
$_POST['checkbox']
);
In $rows you will have all the data you need.
You can run a SELECT on a table not on a DB! A Database consists of many tables. See http://www.php.net/manual/en/function.mysql-select-db.php
Check out w3Schools sql tutorials.
Or more specifically the select tutorial
Also the PHP/mysql tutorial will give you all that you need for this stuff.

Categories