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";
Related
Users can add their workouts into a database. There will have more than one workout per user. How can i display the workout names for the logged in user, underneath each other? I have no problem adding the workouts. When I echo the names of the added workouts, it display right next to each other like "workout1workout2". I want to be able to display the workouts underneath each other. I have no idea how to do it. It would be best if I could display each workout as a button.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo $data['workout'];
}
Firstly, you shouldn't put that username in query like that. Look into SQL Injection and how to avoid that in PHP.
As for displaying buttons/workouts. You can embed HTML code inside the echo statement like below. You can add any html like that and it will render HTML.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo "<button>$data['workout']</button></br>";
}
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' Group By username";
I'm designing a website and I wrote some webpage that display the list of users.
I used to do a
$query = SELECT * FROM `table_users` WHERE `id`='.$id.'
and then increment the ID with a "while" so I can grab all the users. But it's too slow now, and it glitches when there is a gap between IDs.
So I tried something like
$query = SELECT `name` FROM `tbl_user`ORDER BY `id`
and displaying the userlist with a
while ($i < sizeof(mysql_fetch_array(mysql_query($query)))){
<code to display an user>
$i++
}
But the mysql_fetch_array only returnes one user, the first one (the one with the littliest ID). I want it to return all users in an array. How do I do ?
Try This
$query = "SELECT `name` FROM `tbl_user` ORDER BY `id`";
$user_query = mysql_query($query);
$i=1;
while ($row = mysql_fetch_array($user_query)){
echo $i." : ".$row['name']."<br>";
$i++;
}
Try it like this:
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
//$row is the row which was just gathered
}
And please, use PDO or MySQLi instead of deprecated mysql.
I started programming in php and I'm having a small doubt.
I'm trying to do a search the database using a value from a dropdown.
The problem is that the query always uses the last value of the dropdown.
Does anyone can help me find the error?
Why is research in where clause is always the last value of the dropdown?
Code
<tr><td>Technical:</td><td>
<select>
<?php
$query = "SELECT idTechnical, name FROM technicals";
$result2 = mysql_query($query);
$options="";
while($row=mysql_fetch_array($result2)){
$id=$row["idTechnical"];
$thing=$row["name"];
echo "<OPTION VALUE=$id>$thing</option>";
}
?>
</select>
<?php
if (isset($_POST['Next'])) {
if($_REQUEST['Next']=='Search') {
{
$sql="select idTask, descTask, deadline, idTechnical from tasks where idTechnical = '$id' order by deadline desc";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
}
}
}
?>
I select any value from dropdown, but only uses the last value in clause where :S
Here is what I would do for the form (assuming you have a proper form tag with an action attribute that points to the correct PHP script):
<tr>
<td>Technical:</td>
<td>
<select name="technical">
<?php
$query = "SELECT idTechnical, name FROM technicals";
$result2 = mysql_query($query);
$options="";
while($row=mysql_fetch_array($result2)){
echo '<option value='.$row["idTechnical"].'>
'.$row["name"].'
</option>';
}
?>
</select>
</td>
Then in the PHP script:
$sql='SELECT
idTask,
descTask,
deadline,
idTechnical
FROM tasks
WHERE idTechnical = '.$_REQUEST['technical'].'
ORDER BY deadline DESC';
$result=mysql_query($sql);
$count=mysql_num_rows($result);
This should do it for you.
But please note: The script above is a security risk because it leaves the door wide open for SQL injection
A better way to do this would be to use a PDO Prepared statement, like this:
$db = new PDO('mysql:host=CHANGE_THIS_TO_YOUR_HOST_NAME;
dbname=CHANGE_THIS_TO_YOUR_DATABASE',
'CHANGE_THIS_TO_YOUR_USERNAME',
'CHANGE_THIS_TO_YOUR_PASSWORD');
$sql='SELECT
idTask,
descTask,
deadline,
idTechnical
FROM tasks
WHERE idTechnical = :id
ORDER BY deadline DESC';
$query = $db->prepare($sql);
$query->bindValue(':id', $_REQUEST['technical']);
$query->execute();
$count = $query->rowCount();
If you're just starting in PHP, I would highly recommend that you spend some time to become familiar with PDO Database querying. Good luck and happy coding!
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'm trying to display a field from my MySQL database. It's in the table tblproducts in the row with the id is set to 1. And under the column qty.
This is the code I'm using:
<?php
mysql_connect("localhost","username","password");
mysql_select_db("database_name");
$available = "SELECT qty FROM tblproducts WHERE id = 1";
$result = mysql_query($available);
echo $result;
?>
However, I keep getting this message: Resource id #2
I've done a bit of research and seen where other people are having similar problems but most of them are trying to display their data in an HTML table whereas I just need the data from 'qty' to display. And of course I'm definitely not a MySQL guru.
Can anyone help me out with this please?
Try changing this:
$result = mysql_query($available);
To this:
$result = mysql_result(mysql_query($available), 0);
Let's start from the start. (I'll assume you have the connection set)
Form the query
$query = "SELECT `qty`
FROM `tblproducts`
WHERE `id` = 1";
Execute the query
$run = mysql_query($query);
Now, put the result in an assoc array
$r = mysql_fetch_array($run);
See the contents of the array
echo $r['qty'];
It's also advised that you move up from mysql to either mysqli, or PDO. PDO is preferred as you're not bound to the MySQL database model.
Try this:
Here you need to generate associative array and then get the resulting row.
$query = "SELECT `qty` FROM `tblproducts` WHERE `id` = 1";
$run = mysql_query($query);
$r = mysql_fetch_array($run);
echo $r['qty'];
-
Thanks