select a single value from table into a field from while loop - php

I have a select option to get data from a table by using while loop
$select_material_code = "SELECT material FROM tbl_material";
$get_material_code = mysqli_query ($con, $select_material_code);
$options_material_Code = "--Select material Code--";
while ($result_material_code = mysqli_fetch_array($get_material_code))
{
$options_material_Code = $options_material_Code."<option>$result_material_code[0]</option>";
}
All data is coming into the list as expected, so, i want to continue selection data from selection option and fetch a value into next textbox, but i cannot get it.
please help....

$select_material_code = "SELECT material FROM tbl_material";
$get_material_code = mysqli_query ($con, $select_material_code);
$options_material_Code = "--Select material Code--";
while ($result_material_code = mysqli_fetch_array($get_material_code))
{
$options_material_Code =
$result_material_Code."<option>$result_material_code[0]</option>";
}

Related

How to select all rows, and update field in row within the same while loop?

I'm trying to query all data, manipulate 2 of the fields to create a new value, and then update an existing field (woocat) within the same row with this new value. It runs without error, but no updating happens. What am I missing?
# Get all data
$query = "SELECT * FROM cities";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
$cityid = $row['id'];
$city = $row['city'];
$state_abr = $row['state_abr'];
$state = $row['state'];
$gstate = ucwords($state); // HELLO WORLD!
$gstate = ucwords(strtolower($gstate)); // Hello World!
$gcity = ucwords($city); // HELLO WORLD!
$gcity = ucwords(strtolower($gcity)); // Hello World!
$woocat = ("Towns>$gstate>$gcity");
// FIX THIS to make it work
$query = "UPDATE cities SET woo_cat = '$woocat' WHERE id = '$cityid'";
mysqli_query($con, $query);
}
mysqli_close($con);
You are modifying the loop variable inside the loop. When you do mysqli_query($con, $query); you are modifying the values of $row = mysqli_fetch_assoc($result) to the values of the new query inside the same loop, so it won't work,you can do three things:
Use a different connection for the first query ('select') and the second query ('update')
Use two loops, one to fetch the results of the select and another one to update the rows
Use mysqli_fetch_all($result,MYSQLI_ASSOC) and then loop over the results to make the update

Don't query SQL string if value is empty

I have two drop down menu's and when you select a value the value is saved in a session and passed trough AJAX to update a div. I have this working.
The reason I save the value in a session is because I need the database query to be dynamically filled (constructed). At this moment I have the following code:
<?php
$q = $_GET['q'];
$_SESSION['theme'] = $q;
$i = $_SESSION['category'];
$query = "SELECT COUNT(".$q.") c FROM MirrorWebProductsExpanded WHERE Subcategorie = '".$i."'";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_assoc($result);
echo "
<a href='http://example.nl/search/'>" . $row['c'] . "</a>";
?>
This counts all items from $q which is the value of the first drop down menu.
And WHERE Subcategorie = '".$i."', $i is the value from the second drop down menu.
But, now the problem. If the second value is empty (I haven't selected an option from that drop down menu) the query still add's this part to the query, like:
$query = "SELECT COUNT(".$q.") c FROM MirrorWebProductsExpanded WHERE Subcategorie = ''";.
This makes the count from the first drop down menu always show 0. Is there away to only add the WHERE Subcategorie = '".$i."' when the second drop down menu has a value?
I'm still kind of new to MySQL so please be nice...
Use a query without WHERE clause. Then check if $i is not null, empty or whitespace. If so, add WHERE clause to your query.
$query = "SELECT COUNT(".$q.") c FROM MirrorWebProductsExpanded";
if($i != "")
$query = $query." WHERE Subcategorie = '".$i."'";

Prevent Form from inserting data multiple times into database

Trying to insert form data into mysql database, my issues is as the user submits the form the data gets inserted twice into database. I know that I am doing something wrong with my 2nd query $query_file = "INSERT INTO upperbit_files... statment as when I remove the whole if loop if(mysqli_query($dbc, $query_info)){...} the form gets submitted once as expected.
Basically I need to insert data into 2 tables. One is for general product info and the other one is to store photos relating to that product both the table are connected via a global variable $advert_id. I am using 2 separate queries
Table1: advert_sell_category1, is for general product info
Table2: upperbit_files, is to store details of the images uploaded
But for some reason the 1st query relating to general product info is getting inserted twice into database and the irony is both the time the $advert_id is the same. Below is my code and a screenshot of the database for your understanding,
if(isset($_POST['postad'])){
$adtype = $_POST['offering_type'];
$manufacturer = mysqli_real_escape_string($dbc, $_POST['manufaturer']);
$mediafile = mysqli_real_escape_string($dbc,$_POST['mediafile']);
$GLOBALS['advrt_post_id'] = crypto_rand_secure(10, 100000);
$query_info = "INSERT INTO advert_sell_category1(advert_id,manufacturer,image_file)
VALUES('$advrt_post_id','$manufacturer','$mediafile')";
$result = mysqli_query($dbc, $query_info) or die(mysqli_error($dbc));
if(mysqli_query($dbc, $query_info)){
$last_id = mysqli_insert_id($dbc);
$query_link_id = "SELECT advert_id FROM advert_sell_category1 WHERE id = '$last_id' ";
$result_id = mysqli_query($dbc, $query_link_id);
while ($row = mysqli_fetch_assoc($result_id)) {
$link_id = $row['advert_id'];
if(!empty($mediafile)){
$media_file = explode(",", mysqli_real_escape_string($dbc,$_POST['mediafile']));
$media_file = array_filter($media_file);
$media_file_size = explode(",", mysqli_real_escape_string($dbc,$_POST['mediafilesize']));
$media_file_size = array_filter($media_file_size);
$media_file_type = explode(",", mysqli_real_escape_string($dbc,$_POST['mediafiletype']));
$media_file_type = array_filter($media_file_type);
for ($var = 0; $var < sizeof($media_file); $var++){
$query_file = "INSERT INTO upperbit_files(file,size,type,link_id) VALUES ('$media_file[$var]','$media_file_size[$var]','$media_file_type[$var]','$link_id')";
$result_file = mysqli_query($dbc, $query_file) or die(mysqli_error($dbc));
}
}
}
}
/********** Your Code ************/
$result = mysqli_query($dbc, $query_info) or die(mysqli_error($dbc));
if(mysqli_query($dbc, $query_info)){
/**********************/
See here in if statement you are calling mysqli_query() second time so same data is inserted twice. Use following code to solve your problem
/********** Suggested Code ************/
$result = mysqli_query($dbc, $query_info) or die(mysqli_error($dbc));
if(mysqli_affected_rows()>0){
/**********************/

Want to give values in sql command dynamically by the dropdown box?

I have a Attendance table containing attendance of all the employee.
Currently i am working on report_generation page.
So, i want to pass emp_mail id through drop-down box to fetch records of that particular employee.
here is the database table containing records
Here is the code from the report generation page:
$dbh = getDBH();
$output = "";
$sql = "SELECT ATT.AttDate FROM attandance ATT";
$query = $dbh->prepare($sql);
$query->execute();
$results = $query->fetchAll(\PDO::FETCH_OBJ);
$arrDates = array();
foreach($results as $intKey=>$objAttDate)
{
$arrDates[] = $objAttDate->AttDate;
}
var_dump($arrDates);

Accessing two different tables for one loop

I have a small issue that I can't figure out.
I have to pull data from two different tables, in one loop. I've never done that before, so I have no idea how. I tried two different queries. That looked like this:
$query = "SELECT * FROM colors ";
$color_select = mysqli_query($connection, $query);
$second_query = "SELECT * FROM votes";
$vote_select = mysqli_query($connection, $second_query);
And then put them into a loop:
while($row = mysqli_fetch_assoc($color_select) && $second_row = mysqli_fetch_assoc($vote_select))
{
$color = $row['Colors'];
$votes = $second_row['Votes'];
echo "<tr><td>$color</td><td>$votes</td></tr>";
}
But that didn't work. I didn't expect it to, just wanted to try. :) Maybe someone experienced can help me out. Thanks.
At the end of the day I need a table displayed, that has two columns, one of them contains the color name from one DB table and the other one contains a number of votes.
As requested: table structures.
Table: colors has only one field Colors.
Table: votes has four fields city_id, City, Colors and Votes
*************************EDIT**************************************
So fixed up the query as suggested, but is still shows nothing.
Here is the edited code:
$query = "SELECT * FROM colors,votes WHERE colors.Colors=votes.Colors";
$color_votes_select = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($color_votes_select))
{ $color = $row['Colors'];
$votes = $row['Votes']; }
if table having relation.
try this in single query .
SELECT
`colors`.*,votes.*
FROM
`colors`
INNER JOIN
`votes` ON
`votes`.colorId = `colors`.Id
Most imp *****You should have some relationship between tables
Otherwise workaround
Run query on color, Save it in ArrayA
Run query on vote, Save it in ArrayB
Create New Array ArrayC
$arrayC = array();
Loop array A or C if they both contact same row count
array_push($ArrayC, key and value of color, key and value of votes);
Final loop ArrayC to print tr and td
First Relate These two tables, write color_id in votes table.
$query = "SELECT * FROM colors,votes where colors.id=votes.color_id";
$color_select = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($color_select))
{
$color = $row['Colors'];
$votes = $row['Votes'];
}
Try this:
$query = "SELECT colors FROM colors";
$color_select = mysqli_query($connection, $query) or die (mysqli_error());
$second_query = "SELECT votes FROM votes"; //you only need column votes right?
$vote_select = mysqli_query($connection, $second_query) or die (mysqli_error());;
while( $row = mysqli_fetch_assoc($color_select) && $second_row = mysqli_fetch_assoc($vote_select)){
$color[] = $row['colors'];
$votes[] = $second_row['votes'];
echo "<tr><td>$color</td><td>$votes</td></tr>";
}
Short explanation:
It will fetch the select and store into an array (because what you were doing is selecting multiple rows into one single variable) and then just display with the echo.

Categories