"echo" not shown in php - php

I am trying to display some variables and forms like a inbox type messages with "echo".
The problem is that the echo is not displayed and don't know how to repair it.
When i click on subject field on the subject title should show the "echo" but it doesn't.
Bellow is the code:
<?php
if(isset($_GET['msg_id'])){
$get_id = $_GET['msg_id'];
$sel_message = "select * from messages where msg_id='$get_id'";
$run_message = mysqli_query($con, $sel_message);
$row_message = mysqli_fetch_array($run_message);
$msg_subject = $row_message['msg_sub'];
$msg_topic = $row_message['msg_topic'];
$reply_content = $row_message['reply'];
//updating the unread message to read
$update_unread = "update messages set status='read' where msg_id='$get_id'";
$run_unread = mysqli_query($con, $update_unread);
echo "
<center><br />
<hr>
<h2>$msg_subject</h2><br/>
<p><b>Message:</b>$msg_topic</p><br />
<p><b>My reply:</b>$reply_content</p><br/>
<form action='' method='post'>
<textarea cols='60' rows='10' name='reply'></textarea><br /><br />
<input type='submit' name='msg_reply' value='Reply to this' />
</form>
</center>
";
}
if(isset($_POST['msg_reply'])){
$user_reply = $_POST['reply'];
if($reply_content!='no_reply'){
echo "<h2 align='center'>This message was already replied!</h2>";
exit();
} else {
$update_msg = "update messages set reply='$user_reply' where msg_id='$get_id'";
$run_update = mysqli_query($con, $update_msg);
echo "<h2 align='center'>Message was replied!</h2>";
}
}
}
?>
Bellow is the all code from my_messages.php. I decided to put it here all the code to avoid any questions regarding missing code information. Maybe will help to get the error.
The problem is at the end of the code when i try to click on my inbox and the messages are not displayed when i click on the sender subject. The form is not shown with the echo function.
<?php
session_start();
include("includes/connection.php");
include("functions/functions.php");
if(!isset($_SESSION['user_email'])){
header("location: index.php");
}
else{
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Welcome User!</title>
<link rel="stylesheet" type="text/css" href="styles/home_style.css" media="all">
</head>
<body>
<!-- Container starts -->
<div class="container">
<!-- Header Wrapper Starts -->
<div id="head_wrap">
<!-- Header Starts -->
<div id="header">
<ul id="menu">
<li>Home</li>
<li>Memebers</li>
<strong>Topics:</strong>
<?php
$get_topics = "select * from topics";
$run_topics = mysqli_query($con, $get_topics);
while($row= mysqli_fetch_array($run_topics)){
$topic_id = $row['topic_id'];
$topic_title = $row['topic_title'];
echo "<li><a href='topic.php?topic=$topic_id'>$topic_title</a></li>";
}
?>
</ul>
<form method="get" action="results.php" id="form1">
<input type="text" name="user_query" placeholder="Search a topic"/>
<input type="submit" name="search" value="Search"/>
</form>
</div>
<!-- Header Ends -->
</div>
<!-- Header Wrapper Ends -->
<!-- Content area starts -->
<div class="content">
<!-- User timeline starts -->
<div id="user_timeline">
<div id="user_details">
<?php
$user = $_SESSION['user_email'];
$get_user = "select * from users where user_email='$user'";
$run_user = mysqli_query($con, $get_user);
$row = mysqli_fetch_array($run_user);
$user_id = $row['user_id'];
$user_name = $row['user_name'];
$user_country = $row['user_country'];
$user_image = $row['user_image'];
$register_date = $row['register_date'];
$last_login = $row['last_login'];
$user_posts = "select * from posts where user_id='$user_id'";
$run_posts = mysqli_query($con, $user_posts);
$posts = mysqli_num_rows($run_posts);
//getting the number of unread messages
$sel_msg = "select * from messages where receiver='$user_id' AND status='unread' order by 1 DESC";
$run_msg = mysqli_query($con, $sel_msg);
$count_msg = mysqli_num_rows($run_msg);
echo "
<center><img src='user/user_images/$user_image' width='240' height='240'/></center>
<div id='user_mention'>
<p><strong>Name:<strong> $user_name</p>
<p><strong>Country:<strong> $user_country</p>
<p><strong>Last Login:<strong> $last_login</p>
<p><strong>Member Since:<strong> $register_date</p>
<p><a href='my_messages.php?inbox&u_id=$user_id'>Messages ($count_msg)</a></p>
<p><a href='my_posts.php?u_id=$user_id'>My Posts ($posts)</a></p>
<p><a href='edit_profile.php?u_id=$user_id'>Edit My Account</a></p>
<p><a href='logout.php'>Logout</a></p>
</div>
";
?>
</div>
</div>
<!-- User timeline ends -->
<!-- Content timeline starts -->
<div id="msg" align="center">
<p align="center">
My Inbox ||
Sent Items
</p>
<?php
if(isset($_GET['sent'])){
include("sent.php");
}
?>
<?php if(isset($_GET['inbox'])){ ?>
<table width="800" align="center">
<tr>
<th>Sender:</th>
<th>Subject</th>
<th>Date</th>
<th>Reply</th>
</tr>
<?php
$sel_msg = "select * from messages where receiver='$user_id' order by 1 DESC";
$run_msg = mysqli_query($con, $sel_msg);
$count_msg = mysqli_num_rows($run_msg);
while($row_msg= mysqli_fetch_array($run_msg)){
$msg_id = $row_msg['msg_id'];
$msg_receiver = $row_msg['receiver'];
$msg_sender = $row_msg['sender'];
$msg_sub = $row_msg['msg_sub'];
$msg_topic = $row_msg['msg_topic'];
$msg_id = $row_msg['msg_id'];
$msg_date = $row_msg['msg_date'];
$get_sender = "select * from users where user_id='$msg_sender'";
$run_sender = mysqli_query($con, $get_sender);
$row = mysqli_fetch_array($run_sender);
$sender_name = $row['user_name'];
?>
<tr align="center">
<td>
<a href="user_profile.php?u_id=<?php echo $msg_sender; ?>" target="_blank">
<?php echo $sender_name; ?>
</a>
</td>
<td><?php echo $msg_sub; ?></td>
<td><?php echo $msg_date; ?></td>
<td>Reply</td>
</tr>
<?php } ?>
</table>
<?php
if(isset($_GET['msg_id'])){
$get_id = $_GET['msg_id'];
$sel_message = "select * from messages where msg_id='$get_id'";
$run_message = mysqli_query($con, $sel_message);
$row_message = mysqli_fetch_array($run_message);
$msg_subject = $row_message['msg_sub'];
$msg_topic = $row_message['msg_topic'];
$reply_content = $row_message['reply'];
//updating the unread message to read
$update_unread = "update messages set status='read' where msg_id='$get_id'";
$run_unread = mysqli_query($con, $update_unread);
echo "<center><br/><hr>
<h2>$msg_subject</h2><br/>
<p><b>Message:</b>$msg_topic</p><br/>
<p><b>My reply:</b>$reply_content</p>
<br/>
<form action='' method='post'>
<textarea cols='60' rows='10' name='reply'></textarea><br/><br/>
<input type='submit' name='msg_reply' value='Reply to this'/>
</form>
</center>
";
}
if(isset($_POST['msg_reply'])){
$user_reply = $_POST['reply'];
if($reply_content!='no_reply'){
echo "<h2 align='center'>This message was already replied!</h2>";
exit();
}
else{
$update_msg = "update messages set reply='$user_reply' where msg_id='$get_id'";
$run_update = mysqli_query($con, $update_msg);
echo "<h2 align='center'>Message was replied!</h2>";
}
}
}
?>
</div>
</div>
<!-- Content area ends -->
</div>
<!-- Container ends -->
</body>
</html>
<?php } ?>

var_dump( $row_message)
after fetching from database to see what's in it.
Please consider changing this line
$row_message = mysqli_fetch_array($run_message);
To this
$row_message = mysqli_fetch_assoc($run_message);
mysqli_fetch_array does not return associative array as you want to access further in your code.

Please Make sure you are passing msg_id in query string like someurl?msg_id=1, else you won't see anything inside
if(isset($_GET['msg_id'])){
/* get msg_id details from db and show */
}
this is because here we are checking if msg_id is set then show content. ;)

Related

i am trying to learn php and following a tutorial from youtube $_SESSION is driving me crazy ..i tried everything to make it work

i have declared a session_start() function in the start of both the pages but still the variable is not passing on to the session variable please help
this is where i have included my login.php
<?php
include("template/header.php");
include("template/content.php");
include("template/footer.php");
include("login.php");
?>
this my login.php file where i have passed $email variable to SESSION
<?php
session_start();
include("includes/connection.php");
if(isset($_POST['login'])){
$email= mysqli_real_escape_string($con,$_POST['email']);
$pass= mysqli_real_escape_string($con,$_POST['pass']);
$select_user = "select * from users where user_email= '$email' AND
user_pass='$pass' AND status='verified'";
$query = mysqli_query($con,$select_user);
$check_user= mysqli_num_rows($query);
if($check_user===1){
$_SESSION['usermail']=$email;
echo "<script>window.open('home.php','_self')</script>";
} else {
echo "<script>alert('incorrect details try again')</script>";
}
}
?>
and this is where i have tried to access the session variable but it says undefined:usermail but i dont understand i am giving the session_start() at the beginning and have checked that $email is successfully getting its value from the database then why this is not working
<?php
session_start();
include("includes/connection.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome User!</title>
<link rel="stylesheet" href="styles/home_style.css" media="all"/>
</head>
<body>
<!--container starts-->
<div class="container">
<!--header wrapper starts here-->
<div id="head_wrap">
<!--header starts-->
<div id="header">
<ul id="menu">
<li>Home</li>
<li>Members</li>
<strong>Topics:</strong>
<?php
$get_topics = "select * from topics";
$run_topics= mysqli_query($con,$get_topics);
while($row=mysqli_fetch_array($run_topics)){
$topic_id = $row['topic_id'];
$topic_title = $row['topic_name'];
echo "<li><a href='topic.php?
topic=$topic_id'>$topic_title</a></li>";
}
?>
</ul>
<form method="get" action="results.php" id="form1">
<input type="text" name="user_query" placeholder="search a
topic"/>
<input type="submit" name="search" value="search"/>
</form>
</div><!--header ends-->
</div><!--head wrap ends-->
<!--content area starts-->
<div class="content">
<!--user timeline starts here-->
<div id="user_timeline">
<div id="user_details">
<?php
$user=$_SESSION['usermail'];
var_dump($_SESSION);
$get_user="select * from users where user_email='$user'";
$run_user= mysqli_query($con,$get_user);
$row=mysqli_fetch_array($run_user);
$user_id= $row['user_id'];
$user_name= $row['user_name'];
$user_country= $row['user_country'];
$user_image= $row['user_image'];
$register_date= $row['user_reg_date'];
$last_login= $row['user_last_login'];
$user_posts="select * from posts where user_id='$user_id'";
$run_posts = mysqli_query($con,$user_posts);
$posts =mysqli_num_rows($run_posts);
//getting the number of unread messages
$sel_msg = "select * from messages where receiver='$user_id' AND
status='unread' ORDER by 1 DESC";
$run_msg = mysqli_query($con,$sel_msg);
$count_msg = mysqli_num_rows($run_msg);
echo "
<center>
<img src='users/default.png' width='200' height='200'?>
</center>
<dev id='user_mention'>
<p><strong>Country:</strong>$user_country</p>
<p><strong>Last Login:</strong>$last_login</p>
<p><strong>Member Since:</strong>$register_date</p>
<p><a href='my_messages.php?inbox&u_id=$user_id'>Messages
($count_msg)</a></p>
<p><a href='edit_profile.php?u_id=$user_id'>Edit my account</a>
</p>
<p><a href='logout.php'>Logout</a></p>
</div>
";
?>
</div><!--user details ends here-->
</div><!--user timeline ends here-->
</div><!--content area ends-->
</div><!--container ends-->
</body>
</html>

What is wrong with my php and html code?

<?php
include ("db.php");
session_start();
$user = $_SESSION["user"];
if (isset($_POST["submit"])) {
$target_dir = "pics/";
$target_file = $target_dir . basename($_FILES["img"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
//check if there was an error
if ($uploadOk == 0) {
echo "The file couldnt be upload, please try again";
} else {
if (move_uploaded_file($_FILES["img"]["tmp_name"], $target_file)) {
echo "The file has been uploaded";
} else {
echo "Sorry there was a error";
}
}
$img = $_FILES['img']['name'];
$title = $_POST["cardname"];
$info = $_POST["description"];
$insta = $_POST["insta"];
$snap = $_POST["snap"];
$code = $_POST["code"];
$bg = $_POST["bg"];
$discord = $_POST["discord"];
$sql = "SELECT * FROM cards WHERE code = '".$code."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "Please pick a new id, that one is already taken";
} else {
$sql = "INSERT INTO cards (title, user, link, code, image, description, likes, snap, insta, yt, bg, discord)
VALUES ('$title', '$user', '$link', '$code', '$img', '$info', '0', '$snap', '$insta', '$user', '$bg', '$discord')";
if ($conn->query($sql) === TRUE) {
echo "New card created!";
} else {
echo "There was an error";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="main.css">
<title>Personal cards for your social media or business | SocialCard</title>
</head>
<body>
<style>
.white {
color: white !important;
}
</style>
<div class="jumbotron">
<div class="container text-center">
<h1 class="big">Cards</h1><br>
<?php
$sql3 = "SELECT * FROM cards WHERE user = '$user'";
$result3 = $conn->query($sql3);
if($result3->num_rows > 0) {
//output cards
while($row = $result3->fetch_assoc()) {
?>
<div class="col-sm-4">
<div class="card">
<div class="card-img-top">
<img alt="" src="pics/<?php echo $img; ?>">
</div>
</div>
</div>
</div>
<?php
}
}
?>
<br>
<?php echo $_SESSION["user"]; ?>
<hr>
<div class="card">
<div class="card-body text-center">
<h4>Card Name:</h4>
<form action="" enctype="multipart/form-data" method="post">
<br>
<input type="text" name="cardname">
<br>
<br>
<h4>Profile Image:</h4>
<input type="file" name="img">
<br>
<br>
<h4>Short info:</h4>
<input type="text" name="description">
<br>
<br>
<h4>Instagram link:</h4>
<br>
<input type="text" name="insta">
<br>
<br>
<h4>Snapchat link:</h4>
<br>
<input type="text" name="snap">
<br>
<br>
<h4>Discord Tag:</h4>
<br>
<input type="text" name="discord">
<br>
<br>
<h4>Background Color:</h4>
<br>
<input type="text" name="bg" placeholder="#hexcode">
<br>
<br>
<h4>Unique Id:</h4>
<br>
<input type="text" placeholder="Create One :), remember it" name="code" required>
<br>
<br>
<button type="submit" name="submit" class="btn btn-lg btn-primary">Create Card</button>
<p>To get to your card go to: social-card.pw/view.php?code=youruniqueid</p>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="card bg-dark">
<div class="card-content white">
<h1 class="text-center font1">Purchase Premium</h1>
<p class="text-center">Nothing here yet</p>
<script data-cfasync='false' type='text/javascript' src='//p258030.clksite.com/adServe/banners?tid=258030_546328_0&type=footer&size=37'></script>
</div>
</div>
</div>
</body>
</html>
This is my current php and html code, I must of made a small error because I everytime I go to view it the only thing that shows on my screen is the jumbotron at the top with the test "card" inside it, please help me! I'm sure its just a small error that I cant find out the answer to.
Im assuming its the php code, if anyone can help that would be great.
Assuming that your query is actually retrieving the cards, the display isn't fetching the image from the returned result...
while($row = $result3->fetch_assoc()) {
?>
<div class="col-sm-4">
<div class="card">
<div class="card-img-top">
<img alt="" src="pics/<?php echo $img; ?>">
</div>
</div>
</div>
</div>
<?php
}
Using $img for the image isn't using the data from $row.
You should have something like...
echo $row['image'];
I'm also unsure if there is an extra </div> in the loop, you may need to move the last </div> outside the loop, but check this with the generated source.
Place the session_start() at the first line of page, i.e.:
session_start();
include ("db.php");
session_start(); must be started before any other line or operation.
<div class="jumbotron">
<div class="container text-center">
<h1 class="big">Cards</h1><br>
<?php
$sql3 = "SELECT * FROM cards WHERE user = '$user'";
$result3 = $conn->query($sql3);
if($result3->num_rows > 0) {
//output cards
while($row = $result3->fetch_assoc()) {
?>
<div class="col-sm-4">
<div class="card">
<div class="card-img-top">
<img alt="" src="pics/<?php echo $row['image']; ?>">
</div>
</div>
</div>
<?php
}
}
?>
Check all closing div

Updating a MySQLi database using php and HTML5 forms

On my website i have an admin page where i want to be able to update information in the database, using a form.
This is the code im using to enter information and update what is in my database:
adminform.php
<html>
<head>
<link rel="stylesheet" href="assets/css/main.css" />
</head>
<body>
<header id="header">
<h1>SafeTNet</h1>
<nav id="nav">
<ul>
<li>Admin Page Only</li>
<li></li>
<li>Logout </li>
</ul>
</nav>
</header>
<h1> Select a member </h1>
<br />
<select name="members" onchange="showUser(this.value)">
<option value="">Select a member email</option>
<?php
$query = "SELECT * FROM members";
$mysqli = new mysqli('localhost','root','root','SafeTNetD');
$result = $mysqli->query($query);
while($row = $result->fetch_assoc())
echo '<option value="'.$row["email"].'">'.$row["email"].'</option>';
?>
</select>
<div id="signup">
<h2>Update Your Member Information</h2>
<form method="post" action="admin1.php">
<table>
<tr>
<td>Email</td>
<td><input type="text" name="email" required="required"></td>
</tr>
<tr>
</tr>
<tr>
<td>City </td>
<td><input type="text" name="city"></td>
</tr>
<tr>
</tr>
<tr>
</table>
<br><br>
<div id="buttons">
<input type="submit">
</div>
</body>
</html>
admin1.php
<html>
<head>
<title>Admin</title>
<link rel="stylesheet" href="assets/css/main.css" />
</head>
<body>
<header id="header">
<h1>SafeTNet</h1>
<nav id="nav">
<ul>
<li>Admin Page Only</li>
<li></li>
<li>Logout</li>
</ul>
</nav>
</header>
<br />
<?php
$query = "SELECT * FROM members";
$mysqli = new mysqli('localhost','root','root','SafeTNetD');
$result = $mysqli->query($query);
while($row = $result->fetch_assoc())
echo '<option value="'.$row["email"].'">'.$row["email"].'</option>';
?>
</select>
<br />
<?php
$q=$row["email"];
$mysqli = new mysqli('localhost','root','root','members');
$sql = "SELECT * FROM members WHERE email='".$q."'";
if(array_key_exists('_submit_check', $_POST))
{
$email = $_POST['email'];
$city = $_POST['city'];
$sql = "UPDATE members SET city = '$city' WHERE email = '$q'";
if($mysqli->query($sql) === TRUE)
{
echo 'Record updated successfully<br />';
}
else
{
echo $sql.'<br />' . $mysqli->error;
}
$mysqli->close();
}
?>
<br><br><br>
<footer id="footer">
<img src="logo.jpg" height="50px">
<ul class="copyright">
<li>© SafeTNet. All rights reserved.</li><li> 2016</li>
</ul>
</footer>
</body>
</html>
I can get the form to run but cant get the information to change in the database or echo to the screen.
Thank you in advance.
if(array_key_exists('_submit_check', $_POST))
{
$email = $_POST['email'];
$city = $_POST['city'];
$sql = "UPDATE members SET city = '$city' WHERE email = '$q'";
if($mysqli->query($sql) === TRUE)
{
echo 'Record updated successfully<br />';
}
else
{
echo $sql.'<br />' . $mysqli->error;
}
$mysqli->close();
}
There is no element called '_submit_check' in your form. I guess you forgot the name attribute of your submit-button.
Your script is very vulnerable to SQL-Injection. You really should not simply throw the userinput into your query. You can use mysqli_real_escape_string() or Prepared Statements to protect your application.
To improve the readability of your code you could change the structure a little. In your admin1.php you should do the business logic before outputting any html. So you would first check if the form has been sent, then you do the database operation. The result of the check or the success/error-message of the database operation can be written into a variable until you output the content of your site.
This way everybody who starts reading the code immediately knows 'alright, this script is the target of some form and accesses the database for some write-operation'.

Php Variable Data Allocation

I wonder why the 2nd output $devNO gives me bool(false) which is empty. i have no idea about it. Am I wrong at the mysql_query() part?
$developer = $_POST['dev'];
$platform = $_POST['plat'];
$genre = $_POST['gen'];
var_dump($developer);
echo "<br>";
$devNO = mysql_query("SELECT No FROM developer WHERE Developer = $developer");
$platNO = mysql_query("SELECT No FROM platform WHERE Platform = $platform");
$genNO = mysql_query("SELECT No FROM genre WHERE Genre = $genre");
var_dump($devNO);
Here's my output:
Here I will show my full code for "games.php":
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<html>
<head lang="en">
<meta charset="utf-8" />
<title>Game List</title>
<link rel="stylesheet" href="css.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<style>
legend {font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif}
</style>
</head>
<body>
<div id="big_wrapper">
<header id="top_header">
<h1>Dandy's Game Library</h1>
</header>
<nav id="top_menu">
<ul>
<li><strong></stron>Home</strong></li>
<li><strong>Game List</strong></li>
</ul>
</nav>
<div id="game_wrapper">
<section id="filter">
<form action="games.php" method="post" name="search_form">
<fieldset>
<legend><h3><strong>Search</strong></h3></legend>
<strong>Developer</strong><br>
<select name="dev">
<option value="">--Select--</option>
<?php
include("dbConnection.php");
mysql_connect("localhost","root","");
mysql_select_db("games");
$sql = mysql_query("SELECT Publisher FROM publisher");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Publisher']."'>" .$row[Publisher]. "</option>";
}
?>
</select>
<br/><br/><strong>Game Platform</strong><br>
<select name="plat">
<option value="">--Select--</option>
<?php
$sql = mysql_query("SELECT Platform FROM platform");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Platform']."'>" .$row[Platform]. "</option>";
}
?>
</select>
<br/><br/><strong>Genre</strong><br>
<select name="gen">
<option value="">--Select--</option>
<?php
$sql = mysql_query("SELECT Genre FROM genre");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Genre']."'>" .$row[Genre]. "</option>";
}
$developer = $_POST['dev'];
$platform = $_POST['plat'];
$genre = $_POST['gen'];
?>
</select>
<br><br><input type="submit" name="search" value="Search"></input>
</fieldset>
</form>
</section>
<aside id="items">
<fieldset>
<legend><h3><strong>Game List</strong></h3></legend>
<?php
var_dump($developer);
$devNO = mysql_query("SELECT No FROM developer WHERE Developer = $developer");
$platNO = mysql_query("SELECT No FROM platform WHERE Platform = $platform");
$genNO = mysql_query("SELECT No FROM genre WHERE Genre = $genre");
var_dump($devNO);
$sql = sprintf("SELECT Title, Release_Year, Language, Price FROM games WHERE Developer_NO = $devNO");
$result = mysql_query($sql);
$game_title = 'Title';
$game_year = 'Release_Year';
$game_lan = 'Language';
$game_price = 'Price';
?>
<div id="gamelist">
<?php
if(!$result) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)) {
?>
<div class="row">
<div class="cell"><?php echo $row[$game_title]?></div>
<div class="cell"><?php echo "Year : ".$row[$game_year]?></div>
<div class="cell"><?php echo "Language : ".$row[$game_lan]?></div>
<div class="cell"><?php echo "Price : RM".$row[$game_price]?></div>
</div>
<?php
}
?>
</div>
</fieldset>
</aside>
</div>
</div>
<div id="btm_wrapper">
<footer id="foot">
<strong></strong>
</footer>
</div>
</body>
</html>
The full output will be like this after i search from dropdown:
The method mysql_query() , if successful, returns a resultset, which has to be made an array even if only a single value is being returned from the query.
Ideally there should be a for-each iteration on this array to get result, however if you're sure you'll get at least one value, the first row with desired index('No' in your case).
$dataset= mysql_query("SELECT No FROM developer WHERE Developer = '$developer'");
$row = mysql_fetch_assoc($dataset);
$devNO = $row['No'];
Also note the quote I've used for varchar. It was correctly pointed in the other answer. Try Now.
If $developer is a string use quotes. You need to first fetch results from result set and than use it in code:
<?php
$devNOResult = mysql_query("SELECT No FROM developer WHERE Developer = '$developer'");
$platNOResult = mysql_query("SELECT No FROM platform WHERE Platform = '$platform'");
$genNOResult = mysql_query("SELECT No FROM genre WHERE Genre = '$genre'");
$devNO = mysql_fetch_row($devNOResult);
$sql = sprintf("SELECT Title, Release_Year, Language, Price FROM games WHERE Developer_NO = $devNO");
$result = mysql_query($sql);
$game_title = 'Title';
$game_year = 'Release_Year';
$game_lan = 'Language';
$game_price = 'Price';
?>
<div id="gamelist">
<?php
if(!$result) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)) {
?>

Let users visit other users (profiles) in my own written social community?

The thing I'm asking for is how the users can visit other users profile.
The past week I have been scripting a social network I'm doing, and I'm kind of stuck right now.
I don't know where to start and read or anything.
So I'm asking you guys to kindly help me =)
Im thinking of the url to be like user.php?id=123 looking and get user you are visiting to show up instead of "you" wich is at user.php!
Live demo: http://social.swipper.org
EDIT #2:
Here's the current code i got inside user.php :
<?php
session_start();
if($_SESSION['username']) {
include "config.php";
$username = $_SESSION['username'];
$fetcher = mysql_query("SELECT * FROM userinfo WHERE username='$username'");
while ($data = mysql_fetch_assoc($fetcher)){
$firstname = $data['firstname'];
$lastname = $data['lastname'];
$gender = $data['gender'];
}
// get user's profile image
if (file_exists("users/$username/profile.jpg")) {
$userPhoto = "<img class='pic' src='users/$username/profile.jpg' width='90%' height='30%' />";
}
elseif (!file_exists("users/$username/profile.jpg")) {
$userPhoto = "<img class='pic' src='http://www.uavmedia.com/portal/images/no-user.jpg' width='90%' height='30%' />";
}
// the user's profile image is fetched
// henter profil text
$file = "users/$username/bio.txt";
$contents = file($file);
$profile_text = implode($contents);
// ferdig å hente profil text, vil nå echo ut profil siden.
// henter profil stilsett
$file2 = "users/$username/style.css";
$contents2 = file($file2);
$profile_style = implode($contents2);
// ferdig å hente profil stilsett.
echo
"
<!doctype html>
<html lang='en'>
<head>
<title>Social FW</title>
<meta name='Description' content='A Social network for everyone. Come join us!' />
<meta name='Keywords' content='Social, Network, Framewerk, Framework, FW, Open-Source, Free' />
<link rel='stylesheet' type='text/css' href='user_files/style.css' media='screen' />
<link rel='stylesheet' type='text/css' href='SFW_files/style2.css' media='screen' />
<style type='text/css'>
$profile_style
</style>
<link rel='icon' href='SFW_files/favicon.ico' media='screen' />
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.js'></script>
<script type='text/javascript' src='SFW_files/scripts/login.js'></script>
</head>
<body>
<div id='top'>
<h1>Swipper.org</h1>
</div>
<div id='side-menu'>
<ul>
<li><a href='user.php'><b>".$username."</b></a></li><br/>
<li><a href=''>Inbox</a></li>
<li><a href=''>Guestbook</a></li>
<li><a href=''>Friends</a></li>
<li><a href=''>Pictures</a></li><br />
<div id='showOptions'><li><a href='#'>Edit Profile</a></li></div>
<div id='editOptions' style='display:none;'>
<pre class='user_info'><b>></b><a href='changeText.php'>Edit Profile Text</a>
<b>></b><a href='changeCss.php'>Edit Stylesheet(CSS)</a>
<b>></b><a href='changeProfilePic.php'>Change Profile Image</a></pre>
</div><br />
<a href='logout.php'><b>Logout</b></a>
</ul>
</div>
<div id='side-left'>
<!-- START USER PIC --!>
<center>
$userPhoto<br />
</center>
<!-- END USER PIC --!>
<!-- START USER INFO --!>
<br />
<h3><pre class='user_info'>User Info</pre></h3>
<pre class='user_info'>Name: ".$firstname." ".$lastname."<br />Sex : $gender</pre>
<!-- END USER INFO --!>
</div>
<div id='box_center'>
<h2 style='font-size:30px;' align='center'>$username</h2>
<hr />
<br />
$profile_text
</div>
<div id='footer'>
<p align='center'>Swipper.org © All rights reserved. 2010-2012</p>
</div>
</body>
</html>
";
}
else {
die("You need to login first or register. You can register/login <a href='index.php'>here</a>!");
}
?>
and when the url is like: user.php?id=3 i want to get the user with userid 3 to show up, and get user #3's information instead of the "user" who wants to visit other people.
Are you listing all the users some where? if you are the href needs to be something like
<?php
$sql = mysql_query("SLECET * FROM userinfo");
while ($row = mysql_fetch_assoc($sql)){
echo "<a href='users.php?id=" . $row['id'] . "'>"
}
then in users.php
<?php
if(isset($_REQUEST['id'])) {
if(is_numeric($_REQUEST['id'])){
$uid = $_REQUEST['id'];
$sql = mysql_query("SELECT * FROM userinfo WHERE id='" . $uid . "' LIMIT 1");
}
} else {
$sql = mysql_query("SELECT * FROM userinfo WHERE username='" . $_SESSION['username'] . "' LIMIT 1");
}
then later your html can use that with a fetch_assoc($sql)
<table>
<tr>
<td><?php echo $row['username'] ?>'s Profile</td>
</tr>
</table>
simple example. but i think you get the picture, message me on FB for more on this Stian
This should be your answer:
if (isset($_GET['username']){
$username = mysql_escape_string($_GET['username']);
}else{
$username = $_SESSION['username'];
}
$fetcher = mysql_query("SELECT * FROM userinfo WHERE username='$username'");
if (mysql_num_rows() == 0){
$username = '';
echo 'Username not found'; die();
}
Make your query like
$userID = $_GET['id'];
$fetcher = mysql_query("SELECT * FROM userinfo WHERE userID='$userID'");
and you can accordingly show data for any user with the valid user ID as given in url.
if profile can be viewed without login too then there is no need of session and if its compulsory only check if isset or not. if you are passing username in url as
user.php?user=test1
then your query will be like
$username = $_GET['user'];
$fetcher = mysql_query("SELECT * FROM userinfo WHERE username='$username'");
Hope now its more clear to u.

Categories