i just want to build comment and status system similar to facebook. when i give a status its show and comment regarding this status its show. but when i give a new status it dose not show.
<?php
// show status.
mysql_connect("localhost","root","");
mysql_select_db("saif");
$sql="select id, st from status ";
$result=mysql_query($sql);
while($row= mysql_fetch_array($result))
{
echo $row['id']. " " .$row['st'];
echo "<br>";
//show comment;
mysql_connect("localhost","root","");
mysql_select_db("saif");
$sql="select com from comment ";
$result=mysql_query($sql);
while($row= mysql_fetch_array($result))
{
echo $row['com'];
echo "<hr>";
}
// end of show comment
//new comment area.
include('textarea.php');
echo "<hr>";
}
?>
Ok, this is my solution:
First I create two tables in my db and I populated them, then I create a connection to my db using MySQLi (MySQL is deprecated).
I SELECTED all the status, and for each status I selected the related comments. To do so, In the comments table I need something to make a relation between status and comments.
Just to understand to which status the comment is related.
First let's create and populate the tables:
SQL:
CREATE TABLE `status` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`st` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `status` (`id`, `st`)
VALUES
(1, 'This is the first status in your DB, let\'s say \"Hello World\"'),
(2, 'This is the second status');
CREATE TABLE `comment` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_status` int(11) DEFAULT NULL,
`com` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `comment` (`id`, `id_status`, `com`)
VALUES
(1, 1, 'This is just a comment to the first status'),
(2, 1, 'This is another comment to the first status'),
(3, 2, 'This is a comment to the second status'),
(4, 1, 'This is the third comment to the first status');
Ok, the php file:
<?php
$con = mysqli_connect("localhost","YourUsername","YourPassword","YourDB");
// Check connection
if (mysqli_connect_errno()) {
echo "Connection error: ".mysqli_connect_error();
exit();
}
$sql = "SELECT * FROM status";
$result = $con->query($sql);
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
echo $row['id']." ".$row['st']."<br>";
//show comment
echo "<ol>";
$id_status = $row['id'];
$query = "SELECT com FROM comment WHERE id_status = $id_status";
$comments = $con->query($query);
while($comment = $comments->fetch_array(MYSQLI_ASSOC))
{
echo "<li>".$comment['com']."</li>";
}
echo "</ol>";
// end of show comment
//new comment area.
include('textarea.php');
echo "<hr>";
}
?>
And this is the result (no text area in my output, cause I didn't have your textarea.php)
I hope this helps.
Related
i have a login script that use discord outh but in the database each player can have many characters so im using discord column as identifier and im trying to print all CID that related to same discord id
for example player1 have 3 characters so once player1 logged in he must see a clickable text like
cid1 cid2 cid3
if he click on cid2 for example than show all info about his cid 2 which is
(4, 'YTS46301', '555555555555555555', 2, 'player1')
or if he click on cid3 print the this
(2, 'ZAZ11111', '555555555555555555', 3, 'player1')
so here is database
CREATE TABLE `players` (
`id` int(11) NOT NULL,
`citizenid` varchar(50) NOT NULL,
`discord` varchar(50) DEFAULT NULL,
`cid` int(11) DEFAULT NULL,
`name` varchar(255) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
ALTER TABLE `players`
ADD PRIMARY KEY (`citizenid`),
ADD KEY `id` (`id`),
ADD KEY `discord` (`discord`);
INSERT INTO `players` (`id`, `citizenid`, `discord`, `cid`, `name`) VALUES
(1, 'TPT54548', '555555555555555555', 1, 'player1'),
(2, 'ZAZ11111', '555555555555555555', 3, 'player1'),
(3, 'UGH53311', '333333333333333333', 1, 'player2'),
(4, 'YTS46301', '555555555555555555', 2, 'player1');
this is profile.php i know this is to print one data of player
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require __DIR__ . "/includes/functions.php";
require __DIR__ . "/includes/discord.php";
require __DIR__ . "/config.php";
if (isset($_SESSION['user'])) {
} else {
header("Location:index.php");
}
//PDO QUERY//
$discord = $_SESSION['user_id'];
$character = $dbh->query("SELECT * FROM players WHERE discord = '$discord'");
foreach($character as $row){
$charid = $row["id"];
$citizenid = $row["citizenid"];
$cid = $row["cid"];
$owner = $row["name"];
}
?>
<p><b>Character ID: </b><?php echo $cid; ?> </p>
<p><b>Owned By:</b> <?php echo $owner; ?></p>
<p><b>discord ID:</b> <?php echo $discord; ?></p>
<p><b>CitizenID:</b> <?php echo $citizenid ; ?></p>
I have made a user_login table, having pk = userid.
A credit_request table is created which references to userid as fk.
when the user login, he should see only his entries in the dashboard.
But, here i am not able to insert data, once i link a foreign key to it.
and even the data inserted through phpmyadmin is visible to all users.
Please help me out.
how to insert and retrieve data for logged in users.
<>
//Database setup for Credit request
//Insert data into Credit request
if(isset($_POST['taskid']))
{
$taskid =$_POST['taskid'];
$orderid = $_POST['orderid'];
$status = $_POST['status'];
$query1 = "INSERT INTO credit_request(taskid, orderid, status)
VALUES ('$taskid', '$orderid', '$status')";
if ($connect->query($query1) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $query1 . "<br>" . $connect->error;
}
}
//Display data for credit request
$query2 = "SELECT taskid, orderid, status FROM credit_request WHERE agentid = '$userid'";
$res = $connect->query($query2);
if ($res->num_rows > 0) {
// output data of each row
while($row = $res->fetch_assoc()) {
echo "<br>taskid: " . $row["taskid"]. " -orderid: " . $row["orderid"]. " --:" . $row["status"]."";
}
} else {
echo "0 results";
}
I think you have problem on update, on delete when creating foreign key..
Here's an example of how you'd build your schema:
CREATE TABLE `country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `state_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`state_name` varchar(45) DEFAULT NULL,
`country_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `country_fk` FOREIGN KEY (`country_id`)
REFERENCES `country` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='';
INSERT INTO country (country_name) VALUES ('US of A');
INSERT INTO state_table (state_name,country_id) VALUES
('Minnesota', 2),
('Arizona', 2);
See this fiddle for more.
My 3 tables namely product, band, product Info
Product
CREATE TABLE IF NOT EXISTS `product` (
`p_id` int(11) NOT NULL AUTO_INCREMENT,
`product` varchar(50) NOT NULL,
PRIMARY KEY (`p_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `product` (`p_id`, `product`) VALUES
(1, 'Atta'),
(2, 'OIl');
Brand
CREATE TABLE IF NOT EXISTS `brand` (
`b_id` int(11) NOT NULL AUTO_INCREMENT,
`p_id` int(11) NOT NULL,
`brand` varchar(50) NOT NULL,
`image` varchar(255) NOT NULL,
PRIMARY KEY (`b_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
Insert
INSERT INTO `brand` (`b_id`, `p_id`, `brand`, `image`) VALUES
(1, 1, 'Ashirvad', 'FreeVector-Blue-Squares-Vector.jpg'),
(2, 1, 'Phillsberry', 'ILBAGNOALESSI_One_02.jpg'),
(3, 2, 'Sunflower', '001-bi-fold-corporate-brochure-template-vol-1-2.jpg');
Product Info
CREATE TABLE IF NOT EXISTS `product_info` (
`pi_id` int(11) NOT NULL AUTO_INCREMENT,
`pro` int(11) NOT NULL,
`b_id` int(11) NOT NULL,
`quantity` varchar(20) NOT NULL,
`measurement` varchar(20) NOT NULL,
`mrp` varchar(20) NOT NULL,
`our_price` varchar(20) NOT NULL,
PRIMARY KEY (`pi_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
Insert
INSERT INTO `product_info` (`pi_id`, `pro`, `b_id`, `quantity`, `measurement`, `mrp`, `our_price`) VALUES
(1, 1, 1, '1', 'kg', '50', '48'),
(2, 1, 2, '1', 'kg', '60', '59'),
(3, 2, 3, '1', 'ltr', '90', '86');
When i use the below query to display data according to the p_id, it displays data correctly
<?php
// Make a MySQL Connection
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not Connect to DB :'. mysql_error());
}
mysql_select_db("mr_bazaar",$con);
$result = mysql_query("select * FROM product");
echo '<ul class="list-1 p2">';
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo '<li>';
echo "<a href='page1.php?$row[p_id]'>";
echo '<b>';
echo $row['product'];
echo '</b>';
echo "</a>";
echo '</li>';
}
echo '</ul>';
mysql_close($con);
?>
and I want to display data according to the p_id in another page page1.php using the table product, brand, product_info(product from product table),(brand from brand table), (quantity, mrp from product_info table)
Send the product id to the next page as a URL parameter. So change,
echo "<a href='page1.php?$row[p_id]'>";
to
echo "<a href='page1.php?pid=$row[p_id]'>";
and accept the pid in page1.php using
$pid=$_GET['pid']; // getting the URL parameter
Now you can query to get the details corresponding to the pid.
mysql_query("SELECT p.product,b.brand,pi.quantity,pi.mrp FROM product as p INNER JOIN brand as b ON p.p_id=b.p_id
INNER JOIN product_info as pi ON pi.b_id=b.b_id WHERE p.p_id=$pid");
1- For sending the product id to page1.php, pass the variable correctly, change
echo "<a href='page1.php?$row[p_id]'>";
to
echo "<a href='page1.php?pid=$row[p_id]'>";
2- Get the variable on page1.php using:
$pid = $_GET['pid'];
3- Query the database for record of that product
$result = mysql_query("select * FROM product WHERE p_id='{$pid}'");
*Note:
1- I would recommend you not to use mysql_* function use mysqli or pdo library instead, and also escape the string while getting it from GET parameter.
2- You can use joins to fetch data from other tables corresponding to current product
I'm creating a dashboard feature for my website. I'm having trouble getting it sorted correctly. The dashboard will contain all of the recent status updates of the people who they are following. I'm using MySQL and PHP for this.
Status Table:
id: key value, auto-increments
user: the username of the poster of the status
status: the actual status that was posted
date: an int value, has the time that the status was posted
Users table: (Unneeded rows are excluded)
username: The user's name
following: All of the users that he is following. This is a text field, and is delimited by semicolons.
It needs to be sorted by the date posted. The reason I'm having trouble is because I have to get the people who the user is following. Any help?
Here i will have a go. I had to change sql tables slightly
Heres SQL:
CREATE TABLE IF NOT EXISTS `status` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`user_id` int(3) NOT NULL,
`user_status` text NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `status` (`id`, `user_id`, `user_status`, `date`) VALUES
(1, 1, 'Hi, Im Dave, User One.', '2012-05-03'),
(2, 2, 'Hi, Im Amy, User Two.', '2012-05-01'),
(3, 3, 'Hi, Im Lisa user 3', '2012-05-01'),
(4, 4, 'Hi, Im Joe user 4', '2012-05-02');
CREATE TABLE IF NOT EXISTS `users` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`following` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `users` (`id`, `username`, `following`) VALUES
(1, 'Dave', '2:3'),
(2, 'Amy', '1:4'),
(3, 'Lisa', '1:4'),
(4, 'Joe', '1:2:3');
and heres is the php (this presumes you have already connected to the database):
// Get user 2 (id 2) details from user table
$res = mysql_query("SELECT * FROM users WHERE id='2'");
while ($row = mysql_fetch_assoc($res)) {
$username = $row['username'];
$following = $row['following'];
}
if(!empty($following)) {
$data = explode(':',$following);
foreach($data as $user){
// Now get user 2 (id 2) followers statuses
$res = mysql_query("SELECT * FROM status WHERE user_id='$user'");
while ($row = mysql_fetch_assoc($res)) {
echo $user_status = $row['user_status'].'<br>';
echo $date = $row['date'].'<br>';
}
}
}
I tested and it seems to work just fine
hope it is what you need :)
Im making a simple forum for my site but im having some problems
it wont print the posts. and i get no error very weird. It prints the thread title.
$id = isset($_GET['t']) ? intval($_GET['t']) : 0;
$query = "SELECT * FROM threads t INNER JOIN posts p ON p.tid = t.id WHERE t.id = $id";
$result = mysql_query($query);
// see if thread exists
if (!mysql_num_rows($result)) {
echo "The requested thread does not exist.";
return false;
}
// Fetch the rows
$row = mysql_fetch_assoc($result);
// Print title
echo '<h1>'.htmlspecialchars($row['title']).'</h1>';
// Print posts
while ($row2 = mysql_fetch_assoc($result)) {
echo $row2['message'];
}
The Tables:
CREATE TABLE IF NOT EXISTS `threads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `threads` (`id`, `title`) VALUES
(1, 'My first thread!');
CREATE TABLE IF NOT EXISTS `posts` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tid` int(10) unsigned NOT NULL DEFAULT '0',
`message` text NOT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `posts` (`id`, `tid`, `message`) VALUES
(1, 1, 'This is my first post in my first thread :)');
Check if your queries and db connections succeed. You're not doing that, so right now if the query fails, it'll do so silently and you're stuck in the dark:
$result = mysql_query($query) or die(mysql_error());
Something like that during development will save you a ton of hair and time.
the first message is in $row try this
// Fetch the rows
$row = mysql_fetch_assoc($result);
// Print title
echo '<h1>'.htmlspecialchars($row['title']).'</h1>';
echo $row['message'];
// Print posts
while ($row2 = mysql_fetch_assoc($result)) {
if($row['title'] == $row2['title'])
echo $row2['message'];
else
//start a new title
You probably want something like this:
$query = "SELECT * FROM threads t INNER JOIN posts p ON p.tid = t.id WHERE t.id = $id ORDER BY `tid` ASC";
$result = mysql_query($query);
if (!mysql_num_rows($result)) {
echo "The requested thread does not exist.";
return false;
}
$lastTID = null;
while ($row = mysql_fetch_assoc($result)) {
if ($lastTID != $row['tid']) {
echo '<h1>'.htmlspecialchars($row['title']).'</h1>';
$lastTID = $row['tid'];
}
echo $row['message'];
}
Note the ORDER BY clause and the $lastTID bit.