how can i delete rows id from the database - php

can somebody help me?
here is my php script...
$req = mysql_query('SELECT id, email, start_date, end_date, time_event, time_submitted, payment_method, status FROM booking_members WHERE status="Canceled"');
while($dnn = mysql_fetch_array($req))
$req1 = mysql_query('DELETE FROM booking_members WHERE id='$id');
while($dnn1 = mysql_fetch_array($req1))
{
<td class="left"><center>
<img src="images/cross.png"></img>
</td></center>
how can i get the id from $dnn['id'] in order to delete rows?

Why not just run the delete directly?
DELETE FROM booking_members WHERE status='Cancelled'
There is absolutely no reason to first select all the id's and then loop through them all deleting rows one at a time.

Mike Brant is exactly right; You should delete them without doing a lookup first. But if you wanted to access the id, you'd use:
// ... PREVIOUS
while ($dnn = mysql_fetch_array($req)) {
$delete_id = $dnn['id'];
$req1 = mysql_query("DELETE FROM booking_members WHERE id='".$delete_id."'");
// ... CONTINUE

while($dnn = mysql_fetch_array($req))
Where is the { ?
And here:
$req1 = mysql_query('DELETE FROM booking_members WHERE id='$id');
If you want the id, of last query, you must do: $dnn['id'] and not $id ( or you set $id before)...
And, for include a HTML script, in PHP script, you must use echo or print or must close the tags php ( ?> )

If you want to do a look up, and im assuming you are using dot net nuke, download the free reports module. After installing it edit it and type in the following select statement:
Select u.id,
u.username,
u.email,
b.start_date,
b.end_date,
b.time_event,
b.time_submitted,
b.payment_method,
b.status
FROM users as u
Join
booking_members as b on u.userid = b.userid
Where status="Canceled"'
this will show you the username, email, start_date, End_date, Time_event, Time_submitted, Payment_method and status of users where there status is canceled...

Related

Showing two different values depending on SESSION value in INNER JOIN

I have two different tables, one named users, and another named transactions. Transactions contains wallet1, wallet2, amount. Users contains user details such as firstname, lastname, and wallet. I am trying to display the corresponding first name and last name, depending on whether or not the SESSION_wallet is equal to wallet1 or wallet2 within transactions. I tried searching for a while, and came up with a solution for showing the correct display name for the first and last name making the transfer, however, I am trying to make it display the correct value for "Transfer to:"
Here is some of my code to get a better understanding of what I mean:
MySQLi Query:
$result2 = mysqli_query($link, "SELECT * FROM transactions INNER JOIN users ON transactions.wallet1 = users.wallet WHERE transactions.wallet1 = '" . $_SESSION["wallet"] . "' OR transactions.wallet2 = '" . $_SESSION["wallet"] . "' Order by transactions.id DESC LIMIT 5 ");
PHP Code:
<?php
if(mysqli_num_rows($result2) > 0)
{
while($row = mysqli_fetch_array($result2))
{
?>
The table that needs to display the transfer from, and transfer to:
<?php
if ($_SESSION["wallet"] == $row["wallet1"]) {
echo "<td>Transfer to ".$row["firstname"]." ".$row["lastname"]."</td>";
}
else if ($_SESSION["wallet"] == $row["wallet2"]) {
echo "<td>Transfer from ".$row["firstname"]." ".$row["lastname"]."</td>";
}
?>
Right now my tables are only showing the first and last name of the user that made the Transfer, however, I need it to display the first and last name of the user that the transaction is made to as well. The else if code is working correct, but the first part is not showing the corresponding value.
You will need to JOIN your transactions table to your users table twice, once to get each users name. Then to avoid duplicate column names overwriting the results in the output array, you will need to use column aliases. Something like this should work:
$result2 = mysqli_query($link, "SELECT t.*,
u1.firstname AS w1_firstname,
u1.lastname AS w1_lastname,
u2.firstname AS w2_firstname,
u2.lastname AS w2_lastname
FROM transactions t
INNER JOIN users u1 ON t.wallet1 = u1.wallet
INNER JOIN users u2 ON t.wallet2 = u2.wallet
WHERE t.wallet1 = '{$_SESSION["wallet"]}'
OR t.wallet2 = '{$_SESSION["wallet"]}'
ORDER BY t.id DESC
LIMIT 5 ");
Then you can access each user's names as $row['w1_firstname'] etc.:
if ($_SESSION["wallet"] == $row["wallet1"]) {
echo "<td>Transfer to ".$row["w2_firstname"]." ".$row["w2_lastname"]."</td>";
}
else if ($_SESSION["wallet"] == $row["wallet2"]) {
echo "<td>Transfer from ".$row["w1_firstname"]." ".$row["w1_lastname"]."</td>";
}
Note that ideally you should use a prepared query for this, for example:
$stmt = $link->prepare("SELECT t.*,
u1.firstname AS w1_firstname,
u1.lastname AS w1_lastname,
u2.firstname AS w2_firstname,
u2.lastname AS w2_lastname
FROM transactions t
INNER JOIN users u1 ON t.wallet1 = u1.wallet
INNER JOIN users u2 ON t.wallet2 = u2.wallet
WHERE t.wallet1 = ?
OR t.wallet2 = ?
ORDER BY t.id DESC
LIMIT 5");
$stmt->bind_param('ss', $_SESSION["wallet"], $_SESSION["wallet"]);
$stmt->execute();
$result2 = $stmt->get_result();

Get friends profile picture(s) from user table

I have 2 table's:
Users (id, username, email, avatar, etc...);
Friends (id, user1, user2, status);
Now I want to build on my profile page an list of my friends with there avatar(s). I'm trying for like 4 hours by myself but i don't get it... :(
BTW: this is an error i got!
Notice: Array to string conversion in /home/reduaqi158/domains/reduankurtaj.eu/public_html/snapfriends/vrienden.php on line 26
This is what i have right now:
<?php
error_reporting(E_ALL);
session_start();
$username = $_SESSION['username'];
$status = 2;
include "includes/conn.php";
$vrienden=mysqli_query($server,"SELECT * FROM vrienden WHERE status='$status' && vriend1='$username' || vriend2='$username' ");
$vriend_list = array();
while($row = mysqli_fetch_array($vrienden))
{
if ($row['vriend1'] == $username) {
$vriend_list[] = $row['vriend2'];
}
else {
$vriend_list[] = $row['vriend1'];
}
}
echo json_encode($vriend_list);
$foto=mysqli_query($server,"SELECT prof_pic FROM users WHERE username='$vriend_list['vriend1''vriend2']' ");
while($row2 = mysqli_fetch_array($foto)) {
echo "<img class='img-rounded' src=assets/profiel/".$row2['prof_pic']." alt='Card image cap'>";
}
?>
json_encode output:
["ja","amando"]
Someone who can help me pls :)
Your initial approach is very confusing.
Almost everything in your code can be substituted by single SQL query.
You can use JOIN to get all your friends with their avatars in one go:
SELECT u.username as username, u.avatar as avatar,.... <== all columns which you need
FROM `friends_table` f <== your friends table
JOIN `users_table` u <== your users table
ON (f.user1 = u.id) <== notice that i join on user1 column
WHERE u.username = '$username' && f.status = '$status'
UNION
SELECT u.username as username, u.avatar as avatar,.... <== same columns
FROM `friends_table` f <== your friends table
JOIN `users_table` u <== your users table
ON (f.user2 = u.id) <== notice that i join on user2 column
WHERE u.username = '$username' && f.status = '$status'
By this query you select all users who are in a friendship with your $username. You need union because you don't know in which field (user1 or user2) your $username is located.
NOTE: I strongly suggest using prepared statements instead of just putting '$var' inside SQL query to prevent SQL Injection.
After executing this query you can parse results and display avatars in such a way:
while($row = mysqli_fetch_array($vrienden, MYSQLI_ASSOC))
{
echo "<img class='img-rounded' src=assets/profiel/".$row['avatar']." alt='Card image cap'>";
}
I hope you got the idea.
in your while statement you have to declare a value for the array. like array[0] = value. so that you know that array position 0 has a certain value. Like what I did here below. Don't know if it's in PHP like this but certain in .net you have to declare the location of a value in an array.
while($row = mysqli_fetch_array($vrienden))
{
if ($row['vriend1'] == $username) {
$vriend_list[0] = $row['vriend2'];
}
else {
$vriend_list[1] = $row['vriend1'];
}
}
and the following
$foto=mysqli_query($server,"SELECT prof_pic FROM users WHERE username='$vriend_list['vriend1''vriend2']' ");
shouldn't it be $vriend_list['vriend1'] . $vriend_list['vriend2']'
you have to use a connect character (the . in PHP)

wall updates can be seen only by friends

i'm creating a wall system where you can post updates on the index page and i wanted the posts to be seen only by friends of the user, i almost made my code work but i still have a problem ,but before i mention my problem if there is a better way to do it please share it with me , i will show you my code
first i have two tables for the code
'my_friends' : 'id' ,'username','friend'
'statues' : 'p_id','post','f_name','userip','date_created'
this code is in post.php then it will be included in the index page
//Check for user's friends
$check = mysql_query("select * from `my_friends` where `username` = '".$username."' order by `id` desc ");
//Get users friends
while($get_users_friends = mysql_fetch_array($check))
{
$show_more_button = 1;
$result = mysql_query("SELECT *,
UNIX_TIMESTAMP() - date_created AS TimeSpent FROM `statues` WHERE `f_name` = '".mysql_real_escape_string(strip_tags($get_users_friends["friend"]))."' ");
}
while($row = mysql_fetch_array($result))
{
$comments = mysql_query("SELECT *,
UNIX_TIMESTAMP() - date_created AS CommentTimeSpent FROM comments where post_id = ".$row['p_id']." order by c_id asc"); ?>
<div class="friends_area" id="record-<?php echo $row['p_id']?>">
<label style="float:left" class="name">
<b><?php echo $row['f_name']; ?></b><br>
<span>
</span><br>
<em><?php echo $row['post'];?></em>
<br clear="all" />
the code doesn't work right, it show the post feed to only one friend, the first added friend of the user ,in other words , the sql $result = mysql_query("SELECT *,
UNIX_TIMESTAMP() - date_created AS TimeSpent FROMstatuesWHEREf_name= '".mysql_real_escape_string(strip_tags($get_users_friends["friend"]))."' "); select all the statues of only one friend ,when i write echo mysql_real_escape_string(strip_tags($get_users_friends["friend"])),'<br>'; before the sql it shows all the friends but when i use it in the sql it doesn't, please help
Here are some thoughts that maybe can help you
Dont use mysql_* functions they are deprecated use PDO class instead
Dont query for * if you dont need whole the information from the table
Table name is statues
After completing first while your variable $result will hold only the last friend data query cause it gets overritten by the next step of iteration(use $result[] = )
Aggregate info from $get_users_friends to rewrite your query into SELECT p_id, post, f_name, UNIX_TIMESTAMP() - date_created AS TimeSpent FROM statues WHERE f_name IN '.implode(',', $firends_name).
Dont see if you use $comments variable at all
$show_more_button = 1; dont see if it is used at all. and ofc it gets overritten by next iteration step;
your code should look something like:
$check = mysql_query("select friend from `my_friends` where `username` = '".$username."' order by `id` desc ");
while($get_users_friends = mysql_fetch_array($check, MYSQL_ASSOC))
{
$result[] = $get_users_friends['friend'];
}
$friends_data_query = mysql_query("SELECT p_id, f_name, post,UNIX_TIMESTAMP() - date_created AS TimeSpent FROM `statues` WHERE `f_name` IN'".implode(',',$result)."'");
while($friends_data = mysql_fetch_array($friends_data_query,MYSQL_ASSOC)){
But make sure that it is rewritten into PDO. Cause now it is very vulnurable to injections.

php mysql query combination

I first search all questions info. from "question" table including title, content, user etc.
the Code:
$sql = "select * FROM question where id>0 ORDER BY id ASC";
$result1 = mysql_query($sql);
$res=Array();
And then I want to search the user's point from "user" table. So I must search point for each user in each row from the result1
The Code:
while($rows=mysql_fetch_assoc($result1))
{
$res[]=$rows;
$user = $rows['user'];
$sql2 = "select point from user where name='$user'";
$result2 = mysql_query($sql2);
}
My problem is how to combine all the users' point(result2) with the questions info.(result1) together so that I can return a json for each row.
Use left join, as my understanding this work for you
$sql = "SELECT q.*, u.point AS point FROM question AS q LEFT JOIN user AS u ON q.user = u.name WHERE q.id > 0 ORDER BY q.id ASC";
$result = mysql_query($sql);
It's better go with the joins here i am giving you the query.i hope it may helps you
select * from question q,user u where q.id>0 ORDER BY id ASC
try something like this:using left join
select question.*,user.point FROM question left join user on user.name= question.name where id>0 ORDER BY id ASC

Getting information from 3 tables SQL & PHP

Im making a blog like system using HTML, CSS, PHP and MySQl.
The site is made up of three tables.
user (id, username, password, email)
posts (postid, title, post)
comments (postid, id, comment, commentid) postid coming from posts and id from user.
I am trying to display all of the comments and the users username who left them for a certain post.
When i use this query in phpmyadmin:
SELECT user.username, comments.comment FROM user INNER JOIN comments on user.id=comments.id where postid=1
It shows what i need.
When i add it into php i get a blank page.
<?php
//echo "1";
session_start();
$connect = mysql_connect('localhost', 'root', 'root') or die("Couldn't connect");
mysql_select_db("com541blog") or die("Couldn't connect to database");
//echo "2";
//$postid = $_GET['type'];
$_SESSION['postid'] = $postid;
//echo "3";
$query_comments = mysql_query("SELECT user.username as username, comments.comment as comment FROM user INNER JOIN comments on user.id=comments.id WHERE postid='1'");
$info = mysql_fetch_array($query_comments);
$username = $info['username'];
$comment = $info['comment'];
echo $username;
echo $comment;
?>
Thanks in advance for the help :)
You're not executing any query.
$rs = mysql_query($query_comments);
$info = mysql_fetch_array($rs);
Your first line has an error I suspect, ie missing 'c' near the end of 'connect'.
include("db_connet.php"); should be include("db_connect.php");
Also, missing a semi-colon ;. This:
$query_comments = ("SELECT user.username, comments.comment
FROM user INNER JOIN comments on user.id=comments.id
where postid=1")
Should read:
$query_comments = ("SELECT user.username, comments.comment
FROM user INNER JOIN comments on user.id=comments.id
where postid=1");
Also, not bad practice to qualify each of your column names with a table name eg user.username as you're doing. But you might prefer eg the following more concise syntax using table aliases:
$query_comments = ("SELECT u.username, c.comment
FROM user u INNER JOIN comments c on u.id = c.id
where c.postid = 1");
(Note the table aliases don't need to be a single letter, so can be handy reducing a table name such as "ManufacturerSuppliedPartsListData_Feb01", to eg "mpl", without losing their meaning. Or eg if you've got "Customers" and "Credit" instead of just "c" you might use eg "cust" and "cred")
You need to specify mysql_query in PHP ... Else your query will not be executed
Like :
$query_comments = mysql_query("SELECT user.username, comments.comment FROM user INNER JOIN comments on user.id=comments.id where where postid=1");

Categories