PHP & MySQL question - php

How can I check if $user_id and $logged_user are in both the MySQL table fields userID and friendID
MySQL code.
$user_id = 2;
$logged_user = 1;
$dbc = mysqli_query($mysqli,"SELECT *
FROM users_friends
WHERE userID = '" . $logged_user . "'
AND friendID = '" . $user_id . "'");

if you want to check both combinations
SELECT *
FROM users_friends
WHERE (userID = '" . $logged_user . "'
AND friendID = '" . $user_id . "')
OR (friendID = '" . $logged_user . "'
AND userID = '" . $user_id . "')"

$dbc->num_rows
would be non-zero if the query succeeds and there's any matching rows in the table.
comment followup:
$dbh = new mysqli(...);
$sql = <<<EOL
SELECT *
FROM users_friends
WHERE (userID = $logged_user) AND (friendID = $user_id)
EOL;
$stmt = $dbh->query($sql)
if ($stmt->num_rows > 0) {
echo "That record exists";
}
You'd want to put in some error condition handling (connection failed, query failed, etc...) but that's the basics.

Related

Multiple if else in mysql query

I have re-written this question because of all the down votes and it would seem that either no one understood the question or was unwilling to help a newbie. So I'll as it this way: Can someone tell/show me why this code does not work? (500 error)
OR... at the very least point me in the right direction? I AM WILLING TO LEARN, I just don't know where to begin or who/where to learn from as I am not sure what to even ask other than how do you run multiple "else" statements... that however left me more confused than I already am though!
Any "HELP" would be greatly appreciated!
<?php
$servername = "localhost";
$username = "****";
$password = "***";
$dbname = "***";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$bname = $_POST['bname'];
$baddress = $_POST['baddress'];
$bcity = $_POST['bcity'];
$bstate = $_POST['bstate'];
$zipcode = $_POST['zipcode'];
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Login FROM `USERS` WHERE Business_Name = '" . $_POST["bname"] . "' AND Business_Address = '" . $_POST["baddress"] . "' AND Business_State = '" . $_POST["bstate"] . "' AND Business_Zip = '" . $_POST["zipcode"] . "' LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while ($row = $result->fetch_assoc())
{
echo "https://www.***.com/realsite.php?Login=" . $row["Login"] . "";
}
}
else
{
$sql = "SELECT Login FROM `DATA` WHERE Business_Name = '" . $_POST["bname"] . "' AND Business_Address = '" . $_POST["baddress"] . "' AND Business_State = '" . $_POST["bstate"] . "' AND Business_Zip = '" . $_POST["zipcode"] . "' LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while ($row = $result->fetch_assoc())
{
echo "https://www.***.com/demo.php?Login=" . $row["Login"] . "";
}
}
else
{
$sql = "INSERT INTO `DATA` (Business_Name, Business_Address, Business_City, Business_State, Business_Zip)
SELECT '$bname', '$baddress', '$bcity', '$bstate', '$zipcode' FROM (SELECT 1) t
WHERE NOT EXISTS (SELECT Login, Business_Name,Business_Address FROM `DATA` WHERE Business_Name='$bname' AND Business_Address='$baddress')";
if (mysqli_query($conn, $sql))
{
echo "<a href='https://www.servedwell.com/realsite.php?Login=" . $row["Login"] . "'>LINK</a>";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
}
?>
Credit goes to #Ancaron for properly formatting code and correcting a syntax error. Final posted code works fine.

Could not find what's wrong in sql set values

I have the following SQL.
$sql_shells = "UPDATE `shells` SET `situation` = 'sold' WHERE `id` = '" . $id . "'";
$conn->query($sql_shells);
I updated the rows with column named buytime and another one called buyer so it would be like this:
$sql_shells = "UPDATE `shells` SET `buyer`= " . $buyer . ", `buytime`= " . $trn_date . " , `situation` = 'sold' WHERE `id` = '" . $id . "'";
$conn->query($sql_shells);
It's not updating.
`$trn_date = $trn_date = date("Y-m-d H:i:s");
and
$buyer = $_SESSION['username'];
Error log is empty.
Another SQL update doesn't have constant values and works fine
$sql_users = "UPDATE `users` SET `amount`= " . $newAmount . ", `gain`= " . $newgain . " WHERE `username` = '" . $buyer . "'";
I tried echoing the SQL query that's not working, this is what I get when I run it.
try to remove ` caracter and i think buytime and buyer should add ' caracter and remove it from id if is int type:
$sql_shells = "UPDATE shells SET buyer= '" . $buyer . "', buytime= '" . $trn_date . "' , situation = 'sold' WHERE id = " . $id ;
$conn->query($sql_shells);

list all users after filling in a form

I have a event form for my members to fill in. The form is working and going to database.
But, how can I show which member has posted the event, so other people know? Because I tried having the user_id in sql string and was only showing the last post in the database not all posts from all users.
here is the form php section
<?php
include("config/db_connect.php");
include("config/ckh_session.php");
// Inserting user Details code
if (isset($_POST['btnSave'])) {
$event_title = mysqli_real_escape_string($conn, $_POST['event_title']);
$myDate = mysqli_real_escape_string($conn, $_POST['myDate']);
$cboStartTime = mysqli_real_escape_string($conn, $_POST['cboStartTime']);
$dteEndDate = mysqli_real_escape_string($conn, $_POST['dteEndDate']);
$dteEndTime = mysqli_real_escape_string($conn, $_POST['dteEndTime']);
$event_type = mysqli_real_escape_string($conn, $_POST['event_type']);
$country = mysqli_real_escape_string($conn, $_POST['country']);
$event_region = mysqli_real_escape_string($conn, $_POST['event_region']);
$union_territory = mysqli_real_escape_string($conn, $_POST['union_territory']);
$event_town = mysqli_real_escape_string($conn, $_POST['event_town']);
$event_postalcode = mysqli_real_escape_string($conn, $_POST['event_postalcode']);
$event_title = mysqli_real_escape_string($conn, $_POST['event_title']);
$event_description = mysqli_real_escape_string($conn, $_POST['event_description']);
$event_ltm = mysqli_real_escape_string($conn, $_POST['event_ltm']);
$insert = mysqli_query($conn, "update events set event_title = '" . $event_title . "' ,
myDate = '" . $myDate . "' ,
cboStartTime = '" . $cboStartTime . "' ,
dteEndDate = '" . $dteEndDate . "' ,
dteEndTime = '" . $dteEndTime . "' ,
event_type = '" . $event_type . "' ,
country = '" . $country . "' ,
event_region = '" . $event_region . "' ,
union_territory = '" . $union_territory . "' ,
event_town = '" . $event_town . "' ,
event_postalcode = '" . $event_postalcode . "' ,
event_title = '" . $event_title . "' ,
event_description = '" . $event_description . "' ,
event_ltm = '" . $event_ltm . "' ") or die(mysqli_error($conn));
if ($_POST['event_ltm'] == 'MF Cpl') {
header("location: CoupleffEvent.php");
} else if ($_POST['event_ltm'] == 'MM Cpl') {
header("location: CouplemmEvent.php");
} else if ($_POST['event_ltm'] == 'FF Cpl') {
header("location: CoupleffEvent.php");
}
}
// Fetch user details
$query = mysqli_query($conn, "select * from user where user_id = '" . $_SESSION['last_id'] . "' ");
$fetch_user = mysqli_fetch_array($query);
$user_country = $fetch_user['user_country'];
$user_gender = $fetch_user['user_gender'];
?>
any help would be much appreciated so i can go to next step
I'm not sure to understand what you're looking for.
If you're getting an user_id, you will have to select * FROM events and then join user table, like INNER JOIN user ON events.user_id = user.id
This type of request will display all event and join informations about the user.id
Resources : https://www.w3resource.com/mysql/advance-query-in-mysql/inner-join-with-multiple-tables.php
Hope it will help,

resource 150 of mysql result

What is resource(150) of type (mysql result)? I am getting that after var_dumping an select query,
My code is:
$userlevelcheck = $this->query_silent("SELECT user_level FROM " . DB_PREFIX . "users WHERE user_id='" . $user_id . "'");
var_dump ($userlevelcheck);
if ($userlevelcheck != "1")
{
code
}
Try:
$userlevelquery = $this->query_silent("SELECT user_level FROM " . DB_PREFIX . "users WHERE user_id='" . $user_id . "'");
$row = mysql_fetch_assoc($query);
if ($row['user_level'] != "1")
{
code
}
You need to get the row with the data in ($row) then check the relevant field $row['user_level']

PHP MySQL counter/loop query

I'm doing a MySQL query to search for items in a database. I've pulled variables out of the search form but I'm having some problems with my WHERE clause. As I don't want to search on fields that haven't been input in the form. The code I have at the minute is:
$query = " SELECT RequestID, clients.ClientName, clients.Username, RequestAssignee, requests.StatusID, requests.PriorityID, StatusName, PriorityName
FROM requests
INNER JOIN clients ON requests.ClientID = clients.ClientID
INNER JOIN statuses ON requests.StatusID = statuses.StatusID
INNER JOIN priorities ON requests.PriorityID = priorities.PriorityID
WHERE ";
if(!empty($RequestID))
{
$query2 .= "RequestID = '" . $RequestID . "' OR ";
}
if(!empty($ClientName))
{
$query2 .= "clients.ClientName = '" . $ClientName ."' OR ";
}
if(!empty($Username))
{
$query2 .= "clients.Username = '" . $Username . "' OR ";
}
if(!empty($RequestAssignee))
{
$query2 .= "RequestAssignee = '" . $RequestAssignee . "' OR ";
}
if(!empty($Status))
{
$query2 .= "statuses.StatusName = '" . $Status ."' OR ";
}
if(!empty($Priority))
{
$query2 .= "priorities.PriorityName = '" . $Priority ."'";
}
However you can see an issue whereby if someone only searches one field, the query adds an 'OR' to the end, resulting in an error:
SELECT RequestID, clients.ClientName, clients.Username, RequestAssignee, requests.StatusID, requests.PriorityID, StatusName, PriorityName FROM requests INNER JOIN clients ON requests.ClientID = clients.ClientID INNER JOIN statuses ON requests.StatusID = statuses.StatusID INNER JOIN priorities ON requests.PriorityID = priorities.PriorityID WHERE RequestID = '3' OR
Im guessing I'm going to have to put some sort of loop or counter in but unsure how to approach it. Any ideas?
Thanks, Matt.
$parts = array();
if(!empty($RequestID))
{
$parts[] = "RequestID = '" . $RequestID . "' ";
}
if(!empty($ClientName))
{
$parts[] = "clients.ClientName = '" . $ClientName ."' ";
}
if(!empty($Username))
{
$parts[] = "clients.Username = '" . $Username . "' ";
}
if(!empty($RequestAssignee))
{
$parts[] = "RequestAssignee = '" . $RequestAssignee . "' ";
}
if(!empty($Status))
{
$parts[] = "statuses.StatusName = '" . $Status ."' ";
}
if(!empty($Priority))
{
$parts[] = "priorities.PriorityName = '" . $Priority ."' ";
}
$query2 .= implode(' OR ', $parts);
Ok so the way i would approach this is to build the variables that will form your where clause separatley:
This could be done in an array:
FIELD1=>'Value1';
FIELD2=>'Value2';
I would then loop over this array, for the first element, i=1 i would build in a WHERE, for i+n -> i+(n-1) i would pre-pend an OR, for the last array value i wouldn't do anything.
I can then use the string i build in this loop - to stick into my query string.
Have a go at something like this and give us a shout if you need more help. Doing it this way is slightly more maintainable also.

Categories