Selecting Item From Category id - php

I'm trying to select items for my different category table but its shows only the i use with If() and I'm trying to use elseif
if($get_idy='Cars'){
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='Cars' ");
}elseif($get_idy='Trucks'){
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='Trucks' ");
}
while($row= mysqli_fetch_array($sel))
{
Can someone help?

You have put wrong if condition in your code.Single "=" assign the value in variable and double "==" check variable value is same or not. It should be like below.
if($get_idy=='Cars'){
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='Cars' ");
} elseif($get_idy=='Trucks'){
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='Trucks' ");
} else{
echo "The category is empty";
}
while($row= mysqli_fetch_array($sel))
{
}

You could have done like this instead checking condition for every category,
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='".$get_idy."'");
while($row= mysqli_fetch_array($sel)) {
//Your code here.
}
Please start using PDO as your sql is vulnerable to SQL Injections
Update: posted this update based on your comment
if(isset($get_idy)) {
$sel = mysqli_query($con,"SELECT * FROM item WHERE category='".$get_idy."'");
while($row= mysqli_fetch_array($sel)) {
//Your code here.
}
} else {
echo "The category is empty";
}

Related

PHP If variable equals

I am trying to show some text depending upon what a status is set to in a db table.
See my code below:
$result=mysql_query("SELECT * FROM hr_recruitment_stages where vacancy_ref='$vacancyref' order by added_on DESC limit 0,1")or die('ERROR 315' );
$row = mysql_fetch_array($result);
$stage_name = $row ['stage_name'];
if($stage_name['stage_name'] == 'Shortlisting') { echo"Shortlisting"; } else { echo"Not Shortlisting"; } ?>
However this doesnt seem to be working properly as it is showing as Not Shortlisting even when stage_name equals Shortlisting.
Any ideas why?
Its variable type mistake. Check your assigned variable, you assigned the Array Element not the entire array. so try like below.
<?php
$result = mysql_query("SELECT * FROM hr_recruitment_stages where vacancy_ref='$vacancyref' order by added_on DESC limit 0,1") or die('ERROR 315' );
$row = mysql_fetch_array($result);
$stage_name = $row['stage_name'];
if($stage_name == 'Shortlisting') {
echo"Shortlisting";
} else {
echo"Not Shortlisting";
}
?>
Refer this Article for PHP Array understanding.
http://php.net/manual/en/language.types.array.php

While loop only displaying one row

I have an HTML form that allows the user to select from a list of employees. The variable is '$empfullname.' However, I also want them to be able to select an 'All' option and if selected it should take the first employee and run the PHP script, then take the next, and so on and so forth. However, the code I have right now if 'All' is selected just displays the first employee in the table and stops. Maybe I need a foreach in their as well? Any help is greatly appreciated. Thanks.
if ($empfullname == 'All') {
$query = "select * from ".$db_prefix."employees order by empfullname asc";
$result = mysql_query($query);
} else {
print timecard_html($empfullname, $local_timestamp_in_week);
}
while ($row=mysql_fetch_array($result)) {
print timecard_html(stripslashes("".$row['empfullname'].""), $local_timestamp_in_week);
}
Your while loop isn't within the if statement, try:
if ($empfullname == 'All') {
$query = "select * from ".$db_prefix."employees order by empfullname asc";
$result = mysql_query($query);
while ($row=mysql_fetch_array($result)) {
print timecard_html(stripslashes("".$row['empfullname'].""), $local_timestamp_in_week);
}
}
else {
print timecard_html($empfullname, $local_timestamp_in_week);
}
Also, I wouldn't use the same variable as the Employee's Full Name to check if 'All' is selected ;).

PHP is actioning all IF Functions not the exact match

I am currently writing a search form in PHP for an SQL query. The user can enter information in any field (aslong as one has information in). Once the form is submitted the mySQL table is searched based on the criteria that they submitted.
The problem I am having is that each IF statement is running not just the one that matches exactly, e.g.
if I complete all 3 criteria I get a search for the first criteria, then the second and first and then again for the first second and third.
Any help is appreciated. (This is only for internal use so I know about the SQL security etc but it not important at this point).
<?php
require 'connectdb.php';
// if there is ONLY a value in the Floor Field
if (!empty($_POST['Floor']))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// if there is ONLY a value in the Floor Field
if (!empty($_POST['Floor']))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// if there is ONLY a value in the flat number Field
if (!empty($_POST['flatno']))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE flatnumber='$_POST[flatno]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// if there is a value in the floor and flatno field then do this:
if (!empty($_POST['Floor']) && (!empty($_POST['flatno'])))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]' AND flatnumber='$_POST[flatno]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// second if - if there is something in the floor, flatno and status field then do this:
if (!empty($_POST['Floor']) && (!empty($_POST['flatno']) && (!empty($_POST['status']))))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]' AND flatnumber='$_POST[flatno]' AND status='$_POST[status]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// if there is a value in the floor and status field then do this:
if (!empty($_POST['Floor']) && (!empty($_POST['status'])))
{
$result = mysqli_query($con, "SELECT * FROM issuelog WHERE floor='$_POST[Floor]' AND status='$_POST[status]'");
}
while($row = mysqli_fetch_array($result))
{
include("searchoutputform.php");
}
// CLOSE CONNECTION TO DATABASE
mysqli_close($con);
?>
What I think you mean by this is that if you enter a value for 'Floor' and 'flatno' and 'status' then you are getting back results for all queries. This is because the other two if statements (than the one you want) specify only that the two fields in question need filling, but not that the third must be empty. So all if conditions are met. You need to specify in each that for that condition to run, the others should be empty.
For example, make your first condition this:
if ((!empty($_POST['Floor']))&&(empty($_POST['flatno']))&&(empty($_POST['status']))){
And repeat this for all conditions. If I haven't explained this well enough let me know and I'll clarify why
Try this It will help you and why writing the same while condition these many times just add once in the last:
<?php
require 'connectdb.php';
// second if - if there is something in the floor, flatno and status field then do this:
if (!empty($_POST['Floor']) && (!empty($_POST['flatno']) && (!empty($_POST['status']))))
{
$result = mysqli_query($con,"SELECT * FROM issuelog WHERE floor='$_POST[Floor]' AND flatnumber='$_POST[flatno]' AND status='$_POST[status]'");
}
// if there is a value in the floor and flatno field then do this:
if (!empty($_POST['Floor']) && (!empty($_POST['flatno'])))
{
$result = mysqli_query($con,"SELECT * FROM issuelog WHERE floor='$_POST[Floor]' AND flatnumber='$_POST[flatno]'");
}
// if there is a value in the floor and status field then do this:
if (!empty($_POST['Floor']) && (!empty($_POST['status'])))
{
$result = mysqli_query($con,"SELECT * FROM issuelog WHERE floor='$_POST[Floor]' AND status='$_POST[status]'");
}
// if there is ONLY a value in the Floor Field
if (!empty($_POST['Floor']))
{
$result = mysqli_query($con,"SELECT * FROM issuelog WHERE floor='$_POST[Floor]'");
}
// if there is ONLY a value in the flat number Field
if (!empty($_POST['flatno']))
{
$result = mysqli_query($con,"SELECT * FROM issuelog WHERE flatnumber='$_POST[flatno]'");
}
// if there is ONLY a value in the flat number Field
if (!empty($_POST['status']))
{
$result = mysqli_query($con,"SELECT * FROM issuelog WHERE flatnumber='$_POST[status]'");
}
while($row = mysqli_fetch_array($result))
{
include( "searchoutputform.php" );
}
// CLOSE CONNECTION TO DATABASE
mysqli_close($con);
?>

While loop not printing all answers

This might be really simple, but i cannot figure out the problem with this code:
$sql = mysql_query("select * from Temporary_Stock_Transfer where Emp_ID = '$emp_id' and Company_ID = '$company_id'");
if(mysql_num_rows($sql) == 0) {
echo "<tr><td colspan='3'><i>You currenty have no items</i></td></tr>";
}else {
while($row = mysql_fetch_array($sql)) {
echo mysql_num_rows($sql);
echo 'reached';
$book_id = $row[1];
$sql = mysql_fetch_row(mysql_query("select title from Book where Book_ID = '$book_id'"));
echo "<tr><td>".$sql[0]."</td><td>".$row[2]."</td><td><span class='label label-important'>Remove</span></td></tr>";
}
}
Now based on my database the query is returning 2 results, the echo mysql_num_rows($sql) also gives out 2. However the reached is echoed only once. Does anyone see a potential problem with the code?
P.S: My bad, $sql is being repeated, that was a silly mistake
It will only echo once because of this line :
$sql = mysql_fetch_row(mysql_query("select title from Book where Book_ID = '$book_id'"));
your overwriting the $sql variable.
Why not just run a single query and join the data you require ?
try ($sql2 instead of $sql and $sql[0] to $sql2[0]):
$sql = mysql_query("select * from Temporary_Stock_Transfer where Emp_ID = '$emp_id' and Company_ID = '$company_id'");
if(mysql_num_rows($sql) == 0) {
echo "<tr><td colspan='3'><i>You currenty have no items</i></td></tr>";
}else {
while($row = mysql_fetch_array($sql)) {
echo mysql_num_rows($sql);
echo 'reached';
$book_id = $row[1];
$sql2 = mysql_fetch_row(mysql_query("select title from Book where Book_ID = '$book_id'"));
echo "<tr><td>".$sql2[0]."</td><td>".$row[2]."</td><td><span class='label label-important'>Remove</span></td></tr>";
}
}
I think it'll be because you are ressetting the sql variable
$sql = mysql_fetch_row(mysql_query("select title from Book where Book_ID = '$book_id'"));
when you are still using it in the while loop. :P
It's probably this line:
$sql = mysql_fetch_row(mysql_query("select title from Book where Book_ID = '$book_id'"));
Try using a different variable name.
You are changing the $sql variable inside the loop, therefore the next time you run $row = mysql_fetch_array($sql), the value will be different (probably won't be any based on your code).
Inside the while loop you are re-using the same variable: $sql. This is used to control the terminating condition of the loop.
Using a different variable name, e.g. $sql2 should leave the original variable intact and the loop should proceed as expected.

Loop through array, query each value until certain condition is met

I'm a bit of a newb to PHP and MySQL. I seem to be having an issue with something. How do I loop through an array, querying each value in the array until the query meets a certain condition.. In this case it would be that the number of rows returned from the query is less than five. Here is what I have:
$query1="SELECT UserID FROM Users where RefID='$userid'";
$result1=mysql_query($query1);
while ($row = mysql_fetch_array($result1, MYSQL_NUM) && $sql2querynum < '5')
{
echo ($row[0]);
echo "
";
$sql2 = "SELECT * FROM Users WHERE RefID=$row[0]";
$sql2result = mysql_query($sql2);
$sql2querynum = mysql_numrows($sql2result);
}
Problem is, for every value it echoes out, I get the following warning:
mysql_numrows(): supplied argument is not a valid MySQL result resource
Like I said, I'm a newb, so maybe I'm not even going about doing this the right way.
try this
$query1="SELECT UserID FROM Users where RefID='$userid'";
$result1=mysql_query($query1);
if(mysql_num_rows($result1)<5)
{
while ($row = mysql_fetch_array($result1))
{
echo ($row[0]);
echo "
";
$sql2 = "SELECT * FROM Users WHERE RefID=$row[0]";
$sql2result = mysql_query($sql2);
$sql2querynum = mysql_numrows($sql2result);
}
}
$query1="SELECT UserID FROM Users where RefID='$userid'";
$result1=mysql_query($query1);
while ($row = mysql_fetch_array($result1, MYSQL_NUM) && $sql2querynum < '5')
{
echo ($row[0]);
echo "
";
$sql2 = "SELECT * FROM Users WHERE RefID={$row[0]}";
$sql2result = mysql_query($sql2);
$sql2querynum = mysql_numrows($sql2result);
}
Use { } for variables in " " ... and why you are not using joins ?

Categories