I am trying to build a forum for my school project. Now i want to get the data out of my database so i can print it and people can see and react to it. Now i have the problem that it only print the commenters post and not the creators post. I will ad some picture and the code so you guys can undestand. (PS. i have a form on the previous page where you can search)
Database
Website
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./style.css">
<title><?php echo $_GET["search"];?></title>
</head>
<body>
<?php
//navbar
include "./include/nav.php";
//database connection
include "./include/conn.php";
$search = $_GET["search"];
if($search == ""){
// if empty return to index
echo "
<script>alert('thread does not exist')</script>
<script>window.location = './index.php'</script>
";
} else {
//check if data exist
$sql = "SELECT thread, post, username, status FROM post WHERE thread = ? ";
$query = $conn->prepare($sql);
$query->execute(array($search));
$data = $query->fetch();
if($data){
// print data if exist
echo "<div class='padding'>";
foreach ($query as $test) {
echo "<tr>";
echo "<td>" . $test["thread"] . "</td>";
echo "<td>" . $test["post"] . "</td>";
echo "<td>" . $test["username"] . "</td>";
echo "<td>" . $test["status"] . "</td>";
echo "</tr>" . "</br>";
}
echo "<button onclick='comment()'>Comment</button>";
echo "</div>";
echo "<div id='test'>";
} else {
// if data does not exist return to index
echo "
<script>alert('thread does not exist')</script>
<script>window.location = './index.php'</script>
";
}
}
?>
<script>
// if comment btn is pressed save thread name and send to comment.php
function comment() {
document.getElementById("test").innerHTML =
"<?php
session_start();
$_SESSION['thread'] = $data['thread'];
?>";
window.location.assign("./comment.php")
}
</script>
</body>
</html>
I never used the data that i got out of the database. I should have printed it like this:
if($data){
// print data if exist
echo "<div class='padding'>";
foreach ($data as $test) {
echo "<tr>";
echo "<td>" . $test["thread"] . "</td>";
echo "<td>" . $test["post"] . "</td>";
echo "<td>" . $test["username"] . "</td>";
echo "<td>" . $test["status"] . "</td>";
echo "</tr>" . "</br>";
}
echo "<button onclick='comment()'>Comment</button>";
echo "</div>";
echo "<div id='test'>";
} else {
// if data does not exist return to index
echo "
<script>alert('thread does not exist')</script>
<script>window.location = './index.php'</script>
";
}
}
Related
I cannot find a way to access the username I used to log in with on my php web form.
I have had a look at these posts but I think my case varies slightly as they both declare the username variable at the top. I simply log in using a sql query.
Here is the login script
<?php
$uname=$_POST['uname'];
$password=$_POST['password'];
session_start();
$con=mysqli_connect("localhost","root","g7trj98o6fyr5","login");//mysqli("localhost","username of database","password of database","database name")
$result=mysqli_query($con,"SELECT * FROM `login_info` WHERE `uname`='$uname' && `password`='$password'");
$count=mysqli_num_rows($result);
if($count==1)
{
echo "Login success";
$_SESSION['log']=1;
header("refresh:2;url=welcome.php");
}
else
{
echo "please fill proper details";
header("refresh:2;url=index.php");
}
?>
I'm expecting to be able to do a if check using the currently logged in user to differentiate them from all other users online.
I have this if check which should only put the edit button next to the user who is logged in.
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['uname'] . "</td>";
echo "<td>" . $row['clickrate'] . "</td>";
if($logedInUsername == $row['uname'])
echo "<td>" . $row['yourword'] . "<a href='edityourword.php?edit=$row[yourword]'> edit</a></td>";
else
echo "<td>" . $row['yourword'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
welcome.php (where the if statement is)
<?php
function add_ant(&$connection)
{
mysqli_query($connection, "UPDATE `login_info` SET `clickrate`=`clickrate`+'1' WHERE `uname`='rvbvakama' && `password`='pass'");
}
session_start();
if(isset($_SESSION['log']))
{
$_SESSION['uname'] = $_POST['uname'];
echo "<script type='text/javascript'>alert('$logedInUsername');</script>";
$con=mysqli_connect("localhost","root","pass","login"); //mysqli("localhost","username of database","password of database","database name")
if(array_key_exists('add',$_POST))
{
add_ant($con);
}
$result = mysqli_query($con,"SELECT * FROM login_info");
if (!$result)
{
printf("Error: %s\n", mysqli_error($con));
exit();
}
echo "<table border='1'>
<tr>
<th>username</th>
<th>clickrate</th>
<th>yourword</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['uname'] . "</td>";
echo "<td>" . $row['clickrate'] . "</td>";
if($logedInUsername == $_SESSION['uname'])
echo "<td>" . $row['yourword'] . "<a href='edityourword.php?edit=$row[yourword]'> edit</a></td>";
else
echo "<td>" . $row['yourword'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Add ants</h1>
<button type='button' name="add">ADD</button> <br/> <br/>
<a href="index.php" >logout</a>
</body>
</html>
<?php
}
else
{
echo "please fill proper details";
header("refresh:2;url=index.php");
}
?>
Thanks.
Not safe, but put:
<?php
$uname=$_POST['uname'];
$password=$_POST['password'];
session_start();
$con=mysqli_connect("localhost","root","g7trj98o6fyr5","login");//mysqli("localhost","username of database","password of database","database name")
$result=mysqli_query($con,"SELECT * FROM `login_info` WHERE `uname`='$uname' && `password`='$password'");
$count=mysqli_num_rows($result);
if($count==1)
{
echo "Login success";
$_SESSION['log']=1;
$_SESSION['uname'] = $_POST['uname'];
header("refresh:2;url=welcome.php");
}
else
{
echo "please fill proper details";
header("refresh:2;url=index.php");
}
?>
and then check:
if($logedInUsername == $_SESSION['uname'])
...
also you need to set $logedInUsername to $row['uname'] in welcome.php
or check
if($row['uname'] == $_SESSION['uname'])
in login.php after login success execute this
$_SESSION['uname'] = $_POST['uname'];
in welcome.php execute this
$logedInUsername = $_SESSION['uname'];
Now the currently logged in username is stored in $logedInUsername for use in welcome.php, this is because SESSION is a global var in php and can be accessed from anywhere.
The while loop and if statement in it should now look like this:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['uname'] . "</td>";
echo "<td>" . $row['clickrate'] . "</td>";
if($row['uname'] == $logedInUsername)
echo "<td>" . $row['yourword'] . "<a href='edityourword.php?edit=$row[yourword]'> edit</a></td>";
else
echo "<td>" . $row['yourword'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
try{
include("dbconnectie.php");
$query = $db->prepare("SELECT * FROM shop WHERE id_img = '3'");
$query->execute();
$result = $query->fetchALL(PDO::FETCH_ASSOC);
$image = $result['img_url'];
echo "<table>";
foreach($result as &$data) {
echo "<tr>";
echo "<td>" . $data["brand"] . "</td>";
echo "<td>" . $data["model"] . "</td>";
echo "<td>" . $data["cond"] . "</td>";
echo "<td>" . $data["price"] . "</td>";
echo '<img src="data:image/png;base64,'.base64_encode( $data["image"] ).'"/>';
echo "</tr>";
}
echo "</table>";
} catch(PDOException $e) {
die("Error!: " . $e->getMessage());
}
?>
<html>
<body>
<a src='<?php echo $image; ?>' border='0'></a>
</body>
</html>
in line 7 u can see that i'm trying to define $image as the url that's saved in the database so i could use it all the way at the bottom to project it as an image.
i have a function called displayMenu() that brings data from a table .. this function is inside a php file named functions.php
if (isset($_POST['Starters'])) {
$sql = "SELECT * FROM product WHERE category_id=1";
$sql1= "SELECT cat_name,cat_description FROM category WHERE cat_id=1";
displayMenu();
}
function displayMenu(){
global $db, $sql,$sql1,$nb;
if($result = mysqli_query($db, $sql1)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo '<h1 class="header">' . $row['cat_name'] . '</h2>';
echo '<h3 class="content">' . $row['cat_description'] . '</h3>';
}
}
}
if($result = mysqli_query($db, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['p_code'] . "</td>";
echo "<td>" . $row['p_name'] . "</td>";
echo "<td>" . $row['p_description'] . "</td>";
echo "<td>" . $row['p_price'] . "</td>";
echo "<td>" ;
echo '<form class="nbItem" action="order.php" method="post">Nb of items:';
echo '<input type="number" name="item"> <button type="submit" class="btn" name="addtocart">Add To Cart</button>';
echo '</form>';
echo "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
this actually works .. but i want to create the table inside another file called order.php and call this function from this file
how should i do it ?
If I understand correctly, you just need to include your functions.php file in your order.php file and then call the displayMenu from includes your order file?
order.php
include 'functions.php';
echo displayMenu();
I want more php data to show when the row of $row->student id is clicked. When row->studentid is clicked, it should show the data as an ajax in the showmore div.
Basically this is the part of index.php code.
<div class="studentsinfo">
<div class="studentpicture">
<img src="images/chrissy.jpg" style="width:100%;height:30%;padding-bottom:none;margin-bottom:none;"/>
</div>
<div id="briefinfo">
<?php require_once("db.php");
if ($result = $mysqli->query("SELECT * FROM requests WHERE status = 1 ORDER BY id"))
{
if ($result->num_rows > 0)
{
while ($row = $result->fetch_object())
{
echo "document id:" . $row->id;
echo "<br>";
$studentid=$row->student_id;
echo "student id:" . $studentid;
echo "<br>";
echo "requested: " . $row->document;
echo "<br>";
if ($row->paidstatus != 1){
echo "payment status: not paid";
}
else if ($row->paidstatus = 1){
echo "payment status: paid";
}
/*echo "<td>" . $row->document . "</td>";
echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "<td><a href='unverify.php?id=" . $row->id . "'>unverify</a></td>";
echo "<td><a href='comments.php?id=" . $row->id . "'>comment</a></td>";
echo "<td>" . $row->paymentamount . " pesos";"</td>";
echo "<td><a href='paymentamount.php?id=" . $row->id . "'>set amount</a></td>";*/
}
}
else
{
echo "No results to display!";
}
}
else
{
echo "Error: " . $mysqli->error;
}
?>
</div>
</div>
<div id="showmore> <!-- show more data here -->
So when studentid button is clicked, it will get information from id.php and return it here.
Id.php is this:
<?php
// connect to the database
include('connect-db.php');
// confirm that the 'id' variable has been set
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get the 'id' variable from the URL
$id = $_GET['id'];
require_once("db.php");
if ($result = $mysqli->query("SELECT * FROM requests WHERE student_id=$id"))
{
if ($result->num_rows > 0)
{
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Document #</th><th>Student #</th>
<th>Documents needed</th><th>Edit</th><th>Delete</th>
<th>recorded comment</th><th>unverify</th><th>comment</th><th>payment amount</th><th>set payment</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td>" . $row->id . "</td>";
$studentid=$row->student_id;
echo "<td><a href='id.php?id=" . $row->student_id . "'>$studentid</a></td>";
echo "<td>" . $row->document . "</td>";
echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "<td>" . $row->comment . "</td>";
echo "<td><a href='unverify.php?id=" . $row->id . "'>unverify</a></td>";
echo "<td><a href='comments.php?id=" . $row->id . "'>comment</a></td>";
echo "<td>" . $row->paymentamount . " pesos";"</td>";
echo "<td><a href='paymentamount.php?id=" . $row->id . "'>set amount</a></td>";
echo "</tr>";
}
echo"<br><br>";
echo "</table>";
}
else
{
echo "No results to display!";
}
}
else
{
echo "Error: " . $mysqli->error;
}}
?>
</div>
So basically, i want all the rows in id.php to show in the div of showmore as an ajax. please help.
How would i write the AJAX code to post the studentID when clicked and return PHP data by the studentid.
I think the AJAX jquery code will look something like this:
$.(#studentid).click(){
something here
}
$.ajax({
type : 'POST',
url : 'id.php'
and soemthing else
});
please help.
Have you tried ajax with jQuery? Give a single <tr> an id and perform the ajax request with
//... in your HTML put
<script src="jquery.js"></script>
//...
<tr id="rowId"...> ...
//...
//in your Javascript, put
$(function (){
$('#rowId').click(function(){ //bind row click to ajax
//send request for student data
$.ajax({
url:"getMoreStudentData.php",
type:"POST",
data:'rowId',
dataType:"json",
success: onStudentDataReturned
});
});
});
//do something with your ajaxed info
function onStudentDataReturned(data){
console.log(data);
}
Once you get that working, you just have to pick a way to make all your rows have the appropriate rowId, which will be sent instead of the placeholder rowId.
I am using a form to get the input of username and password to store the value into the database, once the form is submitted i have defined a table to show all the values from the users table, it have 3 fields (id, name, pass) i want to delete each record by it's id .
i am fetching the data from the users table by using the following code:
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "</tr>"; }
i want to add the delete hyperlink to delete the particular records by id.
i tried using the following code and i couldnt achieve it.
if(mysql_num_rows($result_select) > 0) {
if(isset($_POST['id'])) {
$query_delete = "DELETE FROM users WHERE id =" .$_POST['id'];
$result_delete = mysql_query($query_delete);
}
echo "<table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "<td>Delete</td>";
echo "</tr>";
}
echo "</table>";
}
I am a newbie to programming, i would appreciate if someone explain me in simple words.. thank you :)
First of all put your delete code in your page like this:
if(isset($_POST['submit'])) {
for($i = 0; $i < count($_POST['del']); $i++)
{
// check which records to delete
if (isset($_POST['del'][$i]))
{
$query_delete = "DELETE FROM users WHERE id = " . (int) $_POST['del'][$i];
$result_delete = mysql_query($query_delete) or die(mysql_error());
}
}
echo 'Record Deleted !!' . '<br /><br />';
}
Later put your select code and modify it like this:
echo '<form action="" method="POST">';
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "<td><input type=\"checkbox\" name=\"del[]\"></td>";
echo "</tr>";
}
echo '<input type="submit" name="submit">';
echo '</form>';
You need to change $_POST to $_GET, so this should work.
if(isset($_GET['id'])) {
$query_delete = "DELETE FROM users WHERE id =" .$_GET['id'];
$result_delete = mysql_query($query_delete);
and also put values of attributes inside double quotes, like this
echo '<td>Delete</td>';