I'm working on a project in order to be able to make a bespoke customizer for PC hardware in order to ensure everything is compatible. I am able to make the dropdowns and have validated that they can both be submitted successfully, although I need to pull 2 values and store them in 2 variables so I can use them to A (Store the ID of the CPU) and B (Store the appropriate socket).
<select id="pcat" name="pcat" onchange="autoSubmit();">
<option value="">-- Select Parent Category --</option>
<?php
//select parent categories. parent categories are with parent_id=0
$sql = "SELECT * FROM `cpus` ORDER BY `cpus`.`CPUName` ASC";
$result = dbQuery($sql);
while ($row = dbFetchAssoc($result)) {
echo ("<option value=\"{$row['CPUID']}\" " . ($pmenu == $row['CPUID'] ? " selected" : "") . ">{$row['CPUName']} ({$row['Cores']} Cores | {$row['Threads']} Threads | {$row['Frequency']}GHz)</option>");
}
?>
</select>
I would like to also add in the ability to store a variable called $sockets which would relate to the row called 'CPUSoc' in my database implementation.
Figured this out myself in a kind of... Strangely easy but stupid workaround. All I needed to do in order to pull out an additional variable was to have another query running with the CPUID like so:
$sql = "SELECT * FROM cpus WHERE CPUID = $cpumenu";
$result = dbQuery($sql);
while ($row = dbFetchAssoc ($result)){
($socket = $row['CPUSoc']);
}
And then using the stored $socket variable in the following query to use the stored result in the next query:
$sql = "SELECT * FROM `motherboards` WHERE Socket = '$socket' ORDER BY `motherboards`.`MoboName` ASC";
Related
I am a novice when it comes to PHP but I don't understand if my syntax is wrong in this statement, or how would I grab an int from my MySQL server.
I know that my server credentials are working fine. How would I fix this statement to give me a returned integer of the number of reviews in the userinfo table?
$numberofpreviousreviews = mysql_query("SELECT `number_of_reviews` FROM `userinfo`") or die(mysql_error()); //Check to see how many reviews user has previously created
$amountofreviews = $numberofpreviousreviews + 1;
$query2 = mysql_query("ALTER TABLE userinfo ADD `amountofreviews` VARCHAR(10000)") or die(mysql_error()); //Make another column in database for the new review
You need to fetch your results after you run your query. There are several ways to do this but using mysql_fetch_assoc() will work for you.
$numberofpreviousreviews = mysql_query("SELECT `number_of_reviews` FROM `userinfo`") or die(mysql_error()); //Check to see how many reviews user has previously created
$row = mysql_fetch_assoc($numberofpreviousreviews);
$amountofreviews = $row['number_of_reviews'] + 1;
FYI, you shouldn't be using mysql_* functions anymore. They are deprecated and going away. You should use mysqli or PDO.
Assume you have a table userinfo which has the following structure and data :
Scenario #1 :
If you want to retrieve the all number_of_reviews, then do like this,
$query = "SELECT `number_of_reviews` FROM `userinfo`";
$result = mysqli_query($db,$query);
while ($row = mysqli_fetch_assoc($result)) {
echo "Number of reviews : " . $row['number_of_reviews'] . "<br/>";
}
It will give you,
Number of reviews : 20
Number of reviews : 40
Since, the result has many rows, it will display like above.
Scenario #2:
If you want to retrieve only the specific number_of_reviews for some user id (which is unique). I take id as 1 as a example here. Then do like,
$query2 = "SELECT `number_of_reviews` FROM `userinfo` WHERE `id` = 1";
$result2 = mysqli_query($db,$query2);
while ($row2 = mysqli_fetch_assoc($result2)) {
echo $row2['number_of_reviews'] . "<br/>";
}
This will print,
20.
Because, number_of_reviews is 20 for id 1.
I have a form with a select field:
<select class="indexSearchLocationList" name="locationList">
<option value="allLocations">Anywhere in London</option>
<option value="barking_and_dagenham">Barking and Dagenham </option>
<option value="barnet">Barnet</option>
What im trying to do is if the user chooses the option allocations (Anywhere in London) then I need to use that to select all when querying the database below is the php I currently have but how do I specifiy if that particular option is choosen:
$choosenLocation = $_POST['locationList'];
Just try this, if it is not meet your requirement, then add your requirement briefly as comment.
$choosenLocation = $_POST['locationList'];
if($choosenLocation == 'allLocations')
{
$query = "select * from TABLE ";
}
else
{
$query = "select * from TABLE WHERE condition="any_condition";
}
I think that may be
if(isset($_POST['locationList']) & $_POST['locationList']=='allLocations')
{
$query = "select * from TABLE ";
//based on the mysqli_ or PDO you execute
//fetch the row and display accordingly
}
Do you expect something similar,
$choosenLocation = $_POST['locationList'];
if(isset($choosenLocation) && $choosenLocation == 'allLocations'){
$query = "select * from TABLE ";
//based on the mysqli_ or PDO you execute
//fetch the row and display accordingly
}else{
$query = "select * from TABLE WHERE condition='your condition here' " ;
}
I am trying to learn php and came across a task which i have need help with.
Background information - I have a postgresql database which has
multiple tables.
What I need - When I enter anything in the form on my HTML page, i
need to extract all information from all the tables that contain
information related to what I have entered.
Example - Suppose I enter food poisoning in the form. I need to access
all the tables and extract the different information related to food
poisoning.
My code: (the connection part is not being posted as it works fine)
<?php
$result = pg_prepare($dbh, "Query1", 'SELECT * FROM Project.bacteria WHERE disease = $1');
// if (pg_numrows($result) == 0) {
// $result = pg_prepare($dbh, "Query1", 'SELECT * FROM Project.virus WHERE disease = $1');
// }
//$sql = "SELECT * FROM Project.bacteria WHERE disease=";
//$result = pg_query($dbh, $sql);
$result = pg_execute($dbh, "Query1", array($disease));
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
//$rows = pg_fetch_all($result)
/*// iterate over result set
// print each row*/
while ($row = pg_fetch_array($result)) {
echo $row[0]." ".$row[1]. "<br />";
}
?>
For the above code, when I enter food poisoning it searches just one table i.e bacteria and returns the related information ( here as a test i have taken just information at row position 1 and row position 2.)
Since there are multiple tables, like a table drugs that stores information of drugs used to cure food poisoning, i would want to extract that information from the respective table.
Any help would be appreciated.
try this one
SELECT * FROM bacteria WHERE disease = ''
UNION ALL
SELECT * FROM drugs where desease = ''
but i think the best way is to normalize you tables. :)
I'm working on a search engine on my website. Users can add on criteria which is submitted with a GET in the url.
When users select for example 1 criteria, it looks like this:
localhost/search.php?course=1&price=&name=
They have 3 criteria they can select, so as you see he only selected COURSE.
Now I have to select from the database according to the criteria so my code looks like this:
if ($_GET['price'] > 0 && $_GET['name'])
{
$search_price = $_GET['price'];
$search_name = $_GET['name'];
$result2 = mysql_query("SELECT id, name, price, views, userid, type, anonymous FROM files WHERE course='$course_id' AND price < $search_price AND name LIKE '%$search_name%'");
}
elseif ($_GET['price'] > 0)
{
$search_price = $_GET['price'];
$result2 = mysql_query("SELECT id, name, price, views, userid, type, anonymous FROM files WHERE course='$course_id' AND price < $search_price");
}
elseif ($_GET['name'])
{
$search_name = $_GET['name'];
$result2 = mysql_query("SELECT id, name, price, views, userid, type, anonymous FROM files WHERE course='$course_id' AND name LIKE '%$search_name%'");
}
else
{
$result2 = mysql_query("SELECT id, name, price, views, userid, type, anonymous FROM files WHERE course='$course_id'");
}
while ($row2 = mysql_fetch_assoc($result2))
{
.....
But this can not be the correct way, because if eventually users can select 10 criteria this is going to be a very long code
How do I fix this?
What I would do is dynamically create the sql query,and then execute it at the end. So something like this
$query_string = "SELECT blahblah, blahblah, blah blah from blahx where 1=1 ";
$where = "";
if(isset($_GET['somecriteria']))
{
$where .= " AND blahblah = $_GET['somecriteia'] ";
}
if(isset($_GET['someOTHERcriteria']))
{
$where .= " AND blahblah=$_GET['someOTHERcritera'] ";
}
mysql_query($query_string . $where);
etc..
Take note this is just to show you how to achieve your objective. This is obviously prone to SQL Injection attacks and you'd have to clean the stuff up.
Use $_post to send larger amounts of information to the php script. When using get you should create the url to include get calls only if they are populated. As such if no price is selected the url should not include "price=". This will cause problems with your receiving script.
Your database script can be done with one call including only the selected criteria.
Myqsl has been depreciated, you need to look into Myqsli or PDO
I have a drop downmenu on a page, after users add a content to the db,
i do not want the specific value that was added
from the dorpdown menu to show in the list again.
I do not want to delete that specific value from the dropdown table.
Your help will do.
Here is my code below:
<?php
$query = "SELECT * FROM vreg_no order by vreg desc";
$rs = mysql_query($query);
while($row = mysql_fetch_assoc($rs))
{{
$_SESSION['svregx'] = $row['vreg'];
}}
?>
<select name="svreg" class="bodytxt" id="svreg">
<option>Select Vehicle #</option>
<?php
$query = "SELECT * FROM vreg_no order by vreg desc";
$rs = mysql_query($query);
while($row = mysql_fetch_assoc($rs))
{{
$vreg = $row['vreg'];
if($_SESSION['svregx'] == $vreg){
//do nothing
}
elseif($_SESSION['svregx'] != $vreg){
echo"<option value='$vreg'>$vreg</option>";
}else{}
}}
?>
</select>
You are executing the same query twice.
The first one should be something like:
$query = "SELECT * FROM vreg_no WHERE user_id = YOUR_USER_ID";
or probably a join depending on your database structure.
Than you can add all values to an array and use something like in_array to check if this value exists for a certain user.
And you should dump the deprecated mysql_* functions and switch to prepared statements with bound variables in PDO or mysqli.
If the issue is not producing duplicate user generated content, wouldn't it just be an issue of issuing a DISTINCT query?
$query = "SELECT DISTINCT vreg FROM vreg_no order by vreg desc";