I am creating simple private message conversation script. my script showing incoming users conversation. but messages which i sent to another user not showing.
any one can help me i have lot of tried.
my database
id from_id from_name to_id to_name msg
1 2 master 3 john hi how are you?
2 3 john 2 master fine
3 2 master 3 john hi
Here is my code conversation
<?php
if (isset($_GET['to_id'])) {
$from_id = $_GET['to_id'];
}
if (isset($_GET['to_name'])) {
$from_name = $_GET['to_name'];
}
if (isset($_SESSION['userid'])) {
$to_id = $_SESSION['userid'];
}
require_once"config.php";
if ($stmt = $con->prepare("SELECT * from inbox where from_id=? and from_name=? and to_id=? ")){
$stmt->bind_param('sss', $from_id,$from_name,$to_id);
$stmt->execute();
}
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<div class="msg">
<?php echo $row['from_name'];?>
<?php echo $row['msg'];?>
<?php
}
}
?>
</div>
output image here
check this code
<?php
if (isset($_GET['to_id'])) {
$from_id1 = $_GET['to_id'];
}
if (isset($_GET['to_name'])) {
$from_name1 = $_GET['to_name'];
}
if (isset($_SESSION['userid'])) {
$to_id1 = $_SESSION['userid'];
}
require_once"config.php";
if ($stmt = $con->prepare("SELECT * from inbox where (from_id='2' and to_id='3' and from_name ='jhon') || (from_id='3' and to_id='2')")){
$stmt->bind_param('sss', $from_id,$from_name,$to_id);
$stmt->execute();
}
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<div class="msg">
<?php echo $row['from_name'];?>
<?php echo $row['msg'];?>
<?php
}
}
?>
</div>
Related
I want to display a page, if user doesn't pay for content (via Stripe) and therefore have to check in DB if he paid or not. If he paid, I store string "ok" into status and if he doesn't it's just blank.
Now I'm not sure why the following code doesn't work:
<?php
if(!isset($_SESSION["username"])) {
?>
Login to watch Satellite data.
<?php
$query = 'SELECT status
FROM users
WHERE username="'.$_SESSION["username"].'"';
$stmt = $conn->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$status = $row["status"];
if ($status !== "ok") {
$status_notpaid = true;
}
}
} elseif(isset($_SESSION["username"]) && isset($status_notpaid)) {
include("notpaid.php");
} else {
?>
<?php
$query = 'SELECT id
FROM users
WHERE username="'.$_SESSION["username"].'"';
$stmt = $conn->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
?>
Hello <strong><?php echo $_SESSION["username"];?></strong> |
<?php
while ($row = $result->fetch_assoc()) {
echo $row["id"]; }
?>
I'm not sure why elseif(isset($_SESSION["username"]) && isset($status_notpaid)) { include("notpaid.php"); } doesn't work.
I am assuming the login script sets $_SESSION["username"] if login is successful.
It would make more sense to put the id of the users table, as I assume that is the primary key. You can keep username in session as well if you like and that would save you running some of this code at all.
<?php
if(!isset($_SESSION["userid"])) {
# user not logged in direct to login, and nothing else
echo 'Login to watch Satellite data.';
}
if (isset($_SESSION["userid"])) {
# then we are logged in
# So now we check if they paid
$query = 'SELECT status
FROM users
WHERE id=?';
$stmt = $conn->prepare($query);
$stmt->bind_param('i', $_SESSION["userid"])
$stmt->execute();
$result = $stmt->get_result();
# we had better only be getting one row as a resut of that query
# so a loop is totally unnecessary
$row = $result->fetch_assoc();
$status = $row["status"];
if ($status !== "ok") {
include("notpaid.php");
}
}
?>
Hello <strong><?php echo $_SESSION["username"];?></strong> | $_SESSION['userid']
I'm trying to get this output
Province
1.city 1 33poits
2.city 2 33poits
3.city 3 33poits
and what my current output is
Province
1.city 1 33points
Province
2.city 2 33points
Province
3.city 3 33points
please help just a newbie here, below is my php code
<?php
$sql = "SELECT * FROM ccty WHERE ID=".$ID;
$results = $mysqli->query($sql);
if ($results->num_rows > 0) {
while ($row = $results->fetch_object()) {
?>
<table>
<tr><?= $row->PID; ?>
<td><?= $row->SPID; ?><?= $row->points; ?></td>
</tr>
<?php
}
} else {
echo "No record available!";
}
?>
For one, your table-structure is invalid.
To only print the PID once per group, store the previous PID in a variable and check against it - only print it if its different from before.
You should also be using a prepared statement instead of a direct query.
<?php
$sql = "SELECT PID, SPID, points FROM ccty WHERE ID=? ORDER BY PID";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $ID);
$stmt->execute();
$stmt->bind_result($PID, $SPID, $points);
if ($stmt->num_rows) {
echo '<table>';
$previousPID = null;
while ($stmt->fetch()) {
if ($PID != $previousPID) {
echo '<tr><td>'.$PID.'</td></tr>';
$previousPID = $PID;
}
?>
<tr>
<td><?= $SPID." ".$points; ?></td>
</tr>
<?php
}
echo '</table>';
} else {
echo "No records available!";
}
I have 3 whiles in the code below. Before this I had just 1 while but after expanding the CMS it doesn't want to display the content anymore, I'm not sure what is causing the bug.
<?php
$stmt = $dbConnection->prepare('SELECT * FROM paginas WHERE public="1"');
$stmt->execute();
$result = $stmt->get_result();
$url = $dbConnection->prepare('SELECT * FROM websettings WHERE setting="url"');
$url->execute();
$urlresult = $url->get_result();
$startpagina = $dbConnection->prepare('SELECT * FROM websettings WHERE setting="startpagina"');
$startpagina->execute();
$startresult = $startpagina->get_result();
if(mysqli_num_rows($result) > 0) {
while ($row = $result->fetch_assoc()) {
while ($urlrow = $urlresult->fetch_assoc()) {
while ($startrow = $startresult->fetch_assoc()) {
if(!empty($_GET[$urlrow['value']])) {
if(isset($_GET[$urlrow['value']]) && $_GET[$urlrow['value']] == $row['name']) {
?>
<h1><?php echo $row["heading"]; ?></h1>
<?php echo ubbreplace($row["content"]); ?>
<?php
}
} else {
header("Location: ?" . $urlrow['value'] . "=" . $startrow["value"]);
}
}
}
}
} else {
echo "Er zijn nog geen pagina's aangemaakt.";
}
?>
Can someone please help me out? I'm really trying to fix it, nothing really works.
SOLUTION
<?php
$stmt = $dbConnection->prepare('SELECT * FROM paginas WHERE public="1"');
$stmt->execute();
$result = $stmt->get_result();
if(mysqli_num_rows($result) > 0) {
while ($row = $result->fetch_assoc()) {
$url = $dbConnection->prepare('SELECT * FROM websettings WHERE setting="url"');
$url->execute();
$urlresult = $url->get_result();
while ($urlrow = $urlresult->fetch_assoc()) {
$startpagina = $dbConnection->prepare('SELECT * FROM websettings WHERE setting="startpagina"');
$startpagina->execute();
$startresult = $startpagina->get_result();
while ($startrow = $startresult->fetch_assoc()) {
if(!empty($_GET[$urlrow['value']])) {
if(isset($_GET[$urlrow['value']]) && $_GET[$urlrow['value']] == $row['name']) {
?>
<h1><?php echo $row["heading"]; ?></h1>
<?php echo ubbreplace($row["content"]); ?>
<?php
}
} else {
header("Location: ?" . $urlrow['value'] . "=" . $startrow["value"]);
}
}
}
}
} else {
echo "Er zijn nog geen pagina's aangemaakt.";
}
?>
Get Rid of the "dynamic" parametername - hide it behind a parameter you can access in a static way:
?page=index
becomes
`?p_mode=page&p_selection=index`
or whatever.(Do you REALLY need page to be dynamic? - I guess not.)
Then, you can easily use a single query (Imagine the first 2 parameters are not hardcoded, but used from the GET-Parameters p_mode and p_selection):
set #setting_name = "page";
set #setting_value = "index";
SELECT
p.*
FROM
paginas p
right JOIN
websettings ws
ON
ws.`value` = p.`name`
WHERE
p.`public` = 1 AND
ws.`setting` = #setting_name AND
ws.`value` = #setting_value;
http://sqlfiddle.com/#!9/ce97a/2
However, note that your "setting name" is useless in this tiny example, as it only joins on the value.
(See this example, leading to a wrong result: http://sqlfiddle.com/#!9/d4abb0/1)
So - if you want different keys, add them to the paginas table, make them (and the value) a unique pair:
public | heading | content | key | value
1 Test Test mode index
1 Test 2 Test 2 anotherKey index
and query them in a single run.
Add the End: Please read this post: https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem
Try to ask about your actual problem rather than the solution you think might work in the future.
i have the following information displayed
<?php
$my_query="SELECT * FROM games";
$result= mysqli_query($connection, $my_query);
if (mysqli_num_rows($result) > 0)
while ($myrow = mysqli_fetch_array($result))
{
$description = $myrow["game_description"];
$image = $myrow["gamepic"];
$game_id = $myrow["game_id"];
$gamename = $myrow["game_name"];
echo "<div class='cover'>
</div>";
}
?>
as you can see i have created a game_details page which will display that specific Game_id when the image is clicked
im having trouble understanding how to pull the data out from that game_id in sql on the other page.
here is my attempt on the game_details page
<?php
if (!isset($_GET['$game_id']) || empty($_GET['game_id']))
{
echo "Invalid category ID.";
exit();
}
$game_id = mysqli_real_escape_string($connection, $_GET['game_id']);
$sql1 = "SELECT * games WHERE game_id={$game_id}'";
$res4 = mysqli_query($connection, $sql1);
if(!$res4 || mysqli_num_rows($res4) <= 0)
{
while ($row = mysqli_fetch_assoc($res4))
{
$gameid = $row['$game_id'];
$title = $row['game_name'];
$descrip = $row['game_description'];
$genre = $row['genretype'];
echo "<p> {$title} </p>";
}
}
?>
This attempt is giving me the "invalid category ID" error
Would appreciate help
There are a few issues with your code.
Let's start from the top.
['$game_id'] you need to remove the dollar sign from it in $_GET['$game_id']
Then, $row['$game_id'] same thing; remove the dollar sign.
Then, game_id={$game_id}' will throw a syntax error.
In your first body of code; you should also use proper bracing for all your conditional statements.
This one has none if (mysqli_num_rows($result) > 0) and will cause potential havoc.
Rewrites:
<?php
$my_query="SELECT * FROM games";
$result= mysqli_query($connection, $my_query);
if (mysqli_num_rows($result) > 0){
while ($myrow = mysqli_fetch_array($result))
{
$description = $myrow["game_description"];
$image = $myrow["gamepic"];
$game_id = $myrow["game_id"];
$gamename = $myrow["game_name"];
echo "<div class='cover'>
</div>";
}
}
?>
Sidenote for WHERE game_id='{$game_id}' in below. If that doesn't work, remove the quotes from it.
WHERE game_id={$game_id}
2nd body:
<?php
if (!isset($_GET['game_id']) || empty($_GET['game_id']))
{
echo "Invalid category ID.";
exit();
}
$game_id = mysqli_real_escape_string($connection, $_GET['game_id']);
$sql1 = "SELECT * games WHERE game_id='{$game_id}'";
$res4 = mysqli_query($connection, $sql1);
if(!$res4 || mysqli_num_rows($res4) <= 0)
{
while ($row = mysqli_fetch_assoc($res4))
{
$gameid = $row['game_id'];
$title = $row['game_name'];
$descrip = $row['game_description'];
$genre = $row['genretype'];
echo "<p> {$title} </p>";
}
}
?>
Use error checking tools at your disposal during testing:
http://php.net/manual/en/mysqli.error.php
http://php.net/manual/en/function.error-reporting.php
You want to be using $_GET['gameid'] as that's the parameter you passed.
You are calling for game_id when the link to go to game_details.php has the variable gameid. Either change the parameter in the link to game_id or call for gameid in your $_GET['$game_id'].
Also, as Fred -ii- said, take out the dollar sign in $_GET['$game_id']
Code:
$Username = $_SESSION['VALID_USER_ID'];
$q = mysql_query("SELECT * FROM `article_table`
WHERE `Username` = '$Username'
ORDER BY `id` DESC");
while($db = mysql_fetch_array($q)) { ?>
<?php if(!isset($db['article'] && $db['subject'])) {
echo "Your articles";
} else {
echo "You have no articles added!";
} ?>
<?php } ?>
So I want the rows for example(db['article'] and $db['subject']) from a specific username (see: $Username = $_SESSION['VALID_USER_ID'];) to echo the information if is not empty else if is empty to echo for example "You have no articles added!"
If is some information in the rows the code works, echo the information BUT if the rows is empty don't echo nothing, the code should echo "You have no articles added!" but this line don't appear, where is the mistake?
I tried for if !isset, !empty, !is_null but don't work.
I think what you're trying to achieve is:
$Username = $_SESSION['VALID_USER_ID'];
$q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY `id` DESC");
if(mysql_num_rows($q) > 0)
{
echo "Your articles:\n";
while($db = mysql_fetch_array($q)) {
echo $db['subject']." ".$db['article']."\n";
}
}
else
{
echo "You have no articles added!";
}
?>
I don't understand. Do you have article rows with username, but without article, i.e.:
| id | user | article |
-------------------------------------
| 1 | X | NULL |
If so, you can test with:
if($db['article'] == NULL) { .... } else { .... }
Otherwise, if you don't have a row with user=x, when there are no record, mysql will return an empty result.
So, basicly, if no rows are found on selection: SELECT * FROM article_table WHERE Username = 'X';, you can test
if(mysql_num_rows($q) > 0) { .... } else { .... }
However, mysql_ functions are not recommended anymore. Look at prepared statements.
You have a logic error in your if statement -- what you want is to check if both the article and subject are set.
With your current code, you compare $db['article'] with $db['subject'], and check if the result is set. You need to change it a bit :
Instead of :
if(!isset($db['article'] && $db['subject'])) {
Try:
if(isset($db['article']) && isset($db['subject'])) ...
I would do something like this:
$articles='';
$Username = $_SESSION['VALID_USER_ID'];
$q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY `id` DESC");
while($db = mysql_fetch_array($q)) {
if(isset($db['article']) && isset($db['subject'])) {
$articles .= $db['article']."<br/>";
}
}
if($articles != ''){
echo $articles;
}
else{
echo "No articles";
}
?>
fastest way to achieve what you want is by adding a variable that will verify if the query returned any rows:
<?php $Username = $_SESSION['VALID_USER_ID'];
$i = 0;
$q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY `id` DESC");
while($db = mysql_fetch_array($q)) {
$i = 1;
if(!isset($db['article'] && $db['subject'])) { echo "Your articles"; } ?>
<?php }
if ($i == 0) echo "You have no articles";
?>
You tried to echo "no articles" in the while loop, you get there only if the query returns information, that is why if it returns 1 or more rows, $i will become 1 else it will remain 0.
In your case:
$numArticles = mysql_num_rows($q);
if($numArticles > 0)
echo 'Your articles';
else
echo 'No articles :((((';
I recommend tough moving on to PDO to communicate with DB.