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."'";
Related
I have a very strange problem today. I have a section of code which queries a table based on a GET variable passed from a user input (pretty standard).
However for this to work I have to include a redundant mysql_num_rows variable....
This Works;
<?php
1. $cat = strval($_GET['c']);
2. $query = "SELECT * FROM stock WHERE Category = '$cat'";
3. $num_rows = mysql_num_rows(mysql_query($query));
4. $values = mysql_query($query);
?>
For some reason without line 3 it doesn't work.
By the way this is ultimately used to create an array passed to a google chart which is all fine.
What am I doing wrong?
All code for reference;
<?php
$cat = strval($_GET['c']);
$query = "SELECT * FROM stock WHERE Category = '$cat'";
LINE UNDER QUESTION --> $num_rows = mysql_num_rows(mysql_query($query));
$values = mysql_query($query);
$material = array();
$quantity = array();
$colour = array();
while ($row = mysql_fetch_array($values)) {
array_push($material, $row['Material']);
array_push($quantity, $row['Quantity']);
array_push($colour, $row['Colour']);
}
...Then all the google chart stuff....
?>
Specify columns in the select and use an index.
And make sure the $_GET value is validated.
You probably already have done this, but try the query in e.g. phpMyAdmin.
Maybe all this will help.
SELECT
material,
quantity,
colour
FROM
stock
WHERE
Category = ...
ORDER BY ..
;
I have one drop down and lists are aMan, bMan, cMan.I am selecting any one of them from drop down. So whatever I am selecting from drop down I want to update that records according to list. Below update query is updating all my records because i added '$action_points' for each.
For example. If I selected bMan from the drop down then in update table will update only bMan records according to user_id.If I select aMan then update table it will update only aMan with 10.It will not effect on other.
I am getting the issue on update query.Would you help me with update query?
$result = $conn->query($sql_user);
if (isset($result->num_rows) > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$action_type=$row['action_type'];// Value will be aMan,bMan, cMan
$action_points=$row['action_points']; //10, 20, 30
}
}
$sql = "UPDATE man SET aMan='$action_points',bMan='$action_points', cMan='$action_points' where user_id='$user_id'";
$result = $conn->query($sql);
Update table
You are selecting from select drop down it means it will pass the value, that you have either aMan, bMan or cMan.
so you can do it like this,
$action_type = $_GET['action_type'];
$sql = "update man set `$action_type` = '$action_value' where id = $user_id";
Above is an example.
Firstly, you should replace if (isset($result -> num_rows) >0 ) with if(isset($result)) && ($result->num_rows>0)) .The first condition returns the number of rows (which at the least is 0) and then checks if it is set. Thus, isset will always return true, even when $result is not set. The second condition solves this problem
You have the type of list to update, why don't you use it?
For eg:
$result = $conn->query($sql_user);
if(isset($result)) && ($result->num_rows>0)) {
// output data of each row
while($row = $result->fetch_assoc()) {
$action_type=$row['action_type'];// Value will be aMan,bMan, cMan
$action_points=$row['action_points']; //10, 20, 30
}
}
$sql = "UPDATE man SET $action_type = $action_points WHERE user_id='$user_id'";
$result = $conn->query($sql);
This shall automatically update the required column
U should use AND in your query
UPDATE man SET aMan='$action_points' AND bMan='$action_points' AND cMan='$action_points' where user_id='$user_id'"
Or use several update query it means once update aMan row then a query for bMan row and so on.
The issue is because you are updating the three column at a time, you have to make it conditional like:
$action_type=$row['action_type'];// Value will be aMan,bMan, cMan
$action_points=$row['action_points']; //10, 20, 30
$column = '';
if($action_type == 'aMan'){
$column = 'aMan';
}
else if($action_type == 'bMan'){
$column = 'bMan';
}
else if($action_type == 'cMan'){
$column = 'cMan';
}
$sql = "UPDATE man SET ".$column." = '".$action_points."' where user_id='$user_id'";
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>";
}
I'm trying to work out the following,
I am querying a Salesforce db with PHP, an seems that they don't really allow relationship queries, which is my whole problem, so need to run 2 queries, a query first to populate a variable that is used into a second query right after.
That variable will hold a string with usernames, just plain text usernames, that I need to match with an Id that will match a OwnerId after,
something like this,
<?php
$query = "SELECT FirstName FROM User WHERE Id in (SELECT OwnerId from Case WHERE Region__c = 'WORLD' and CaseOwnerManager__c = 'some_guy' AND Status = 'Open')";
$response = $mySforceConnection->query($query);
$queryResult = new QueryResult($response);
for ($queryResult->rewind(); $queryResult->pointer < $queryResult->size; $queryResult->next()) {
$record = $queryResult->current();
$firstName = $record->fields->FirstName;
echo $firstName."<br>\n";
}
$query = "SELECT CaseNumber, Status FROM Case where Status = 'Open' and OwnerId in (SELECT Id FROM User where FirstName = '{$firstName}')";
$response = $mySforceConnection->query($query);
$queryResult = new QueryResult($response);
for($queryResult->rewind(); $queryResult->pointer < $queryResult->size; $queryResult->next()) {
$record = $queryResult->current();
$caseNumber = $record->fields->CaseNumber;
$status = $record->fields->Status;
echo $caseNumber."<br>\n";
echo $status."<br>\n";
}?>
Of course, in the above, the second query, the one with the $firstName variable, it picks up the last value of the string and then the loop gives the CaseNumber and Status of that single user.
So what i need to do is get that second query in some sort of loop and while it is looping, get the $firstName variable to loop as well.
So at the end, $firstName should populate the th of a table and $caseNumber and $status its tds
Any ideas on how to do it, or if it even can be done?
Cheers.
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.