How do I make an individual url for each ticket? - php

I am trying to make a forum-like website from scratch and this is the first big problem that I encountered so far, I am trying to make an individual link with ?info[id] for each ticket/topic but I simply can't, no matter what id I put in the url, I see all of them instead of the specific one, you have all the code in the video.
Video Link
My Details.php Code
<?php
require('Includes/Header.php');
$query = "SELECT * FROM forum";
$result = mysqli_query($conn ,$query);
?>
<?php while($row = mysqli_fetch_array($result)) {?>
<div class="FormAfter">
<label>ID</label><br><br>
<span><?php echo htmlspecialchars($row['id']); ?></span><br><br>
<label>Titlu</label><br><br>
<span name="Titlu"><?php echo htmlspecialchars($row['titlu']);?></span><br><br>
<label>Categorie</label><br><br>
<span><?php echo htmlspecialchars($row['categorie']); ?></span><br><br>
<label>Descriere</label><br><br>
<span name="Descriere" cols="30" rows="10" readonly><?php echo htmlspecialchars($row['descriere']);?></span>
</div>
<?php
}
mysqli_close($conn);
?>
This is my Topics.php code
<?php
require('Includes/Header.php');
$query = "SELECT * FROM forum";
$result = mysqli_query($conn ,$query);
?>
<div class="TopicListBig">
<span id="IdTitlu">ID <strong>|</strong> Titlu <strong>|</strong> Categorie</span> <br> <br> <br>
</div>
<?php
while($row = mysqli_fetch_array($result)){ ?>
<div class="RandomSpan">
<span class="TopicList"><?php echo htmlspecialchars($row['id']); ?></span>
<span class="TopicList"><?php echo htmlspecialchars($row['titlu']); ?></span>
<span class="TopicList"><?php echo htmlspecialchars($row['categorie']); ?></span>
<span class="TopicList">Info</span><br><br>
</div>
<?php
}
mysqli_close($conn);
?>

Your SQL query is explicitly fetching all records: "SELECT * FROM forum" It's the exact same query in Details as it is in Topics and nowhere in the code for Details do you make use of the id parameter in the URL's query string.
What you're looking for in that SQL query is the WHERE keyword. For example:
SELECT * FROM forum WHERE id=?
Within your WHERE clause you identify the specific filter to find the exact record(s) you want. Then you bind your value to that parameter before executing the query. While that link shows the (generally preferred) object-oriented style, you can also use the procedural style you currently use. For example:
$query = mysqli_prepare($conn, 'SELECT * FROM forum WHERE id=?');
mysqli_stmt_bind_param($query, 's', $_GET['id']);
mysqli_stmt_execute($query);
$result = mysqli_stmt_get_result($query);
while ($row = mysqli_fetch_array($result)) {
// your output
}

Related

mysqli_num_rows generates unlimited amount of data

so, I'll try to explain it as good as I can with my knowledge in english.
I am trying to count data from my database, basically - how many fields I have with the same ID. here's image from database.
image from database
for example, in posts i have review system and it works, i have this PHP code to count how many same fields i have with review and id.
Here's the code:
<?php
$revposid = $res['content_id'];
$pos="SELECT review FROM comments WHERE review='positive' and postid=$revposid";
$neg="SELECT review FROM comments WHERE review='negative' and postid=$revposid";
$neu="SELECT review FROM comments WHERE review='neutral' and postid=$revposid";
if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}
if ($result=mysqli_query($_db,$neg)){$rowcountneg=mysqli_num_rows($result);}
if ($result=mysqli_query($_db,$neu)){$rowcountneu=mysqli_num_rows($result);}
?>
<div class="reviews" id="reviews">
<span class="good"><b class="fa fa-thumbs-up"></b><?php echo $rowcountpos ?></span>
<span class="neutral"><b class="icon-thumbs-up"></b><?php echo $rowcountneu ?></span>
<span class="bad"><b class="fa fa-thumbs-down"></b><?php echo $rowcountneg ?></span>
</div>
and when I try to use the same code
$revposid = $cont['content_id'];
$pos="SELECT content_id FROM user_content_like WHERE content_id=$revposid";
if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}
in my other script I have like system, it should show all my likes and under likes total likes of the post but when I use it it shows unlimited amount of data, i have no idea why. Here's the full code, I would appreciate some help or explanation.
<?php $ususername = $_GET['user_username'];$sql = "SELECT * FROM user_details WHERE user_username='$ususername'";$usresult = mysqli_query($_db,$sql);?>
<?php if( ! mysqli_num_rows($usresult) ) {
echo " Ooops? <br> <br>User <b>".$_GET["user_username"]."</b> doesn't exist.";
} else {
while($usrow = mysqli_fetch_array($usresult,MYSQLI_BOTH)) {?>
<?php
$current_user = $usrow['user_id'];
if ($_db->connect_error) {
die("Connection failed: " . $_db->connect_error);
}
$sql = "SELECT * FROM user_content_like WHERE user_id=$current_user ORDER BY date_added DESC;";
$result = $_db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$content_id = $row['content_id'];
$sql1 = "SELECT * FROM content WHERE content_id=$content_id";
$result1 = $_db->query($sql1);
if ($_SESSION['user_id'] == $usrow['user_id']) {$output = '
<button type="button" class="unlike_button" onclick="unlike(this);" name="like_button" data-content_id="'.$row["content_id"].'" ><i class="fa fa-minus"></i></button>
';} else {
$output = '';
}
while($cont = $result1->fetch_assoc()) {
$revposid = $cont['content_id'];
$pos="SELECT content_id FROM user_content_like WHERE content_id=$revposid";
if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}
echo '
<div class="community-feed-thread">
<div class="community-icon-thread"></div>
<div class="community-comments-thread">'.$output.'</div>
<a href="'.$cont["content_id"].'" class="community-title-thread"><h3>'.$cont["title"].'</h3>
<span class="likes-desc"> Total likes: '.$rowcountpos.'</span>
</a>
</div>
';
}
}}
else {
echo " hmmmmmmmmmmmm.<Br><br>". $usrow["user_username"]." doesn't like anything. ";
}
$_db->close();
?>
<?php }}?>
this is how i want it to look
This is how it looks
Your re-using the same variable for different result sets in the code...
$result = $_db->query($sql);
and
if ($result=mysqli_query($_db,$pos))
You will need to ensure you only use the variable name once or it may have side effects in other loops.

Getting rows from database but printing each one of them where I want

I have a number of rows I want to retrieve from the database but I want to print each row in the exact place I want and only even rows.
Is this possible? If yes, how can I achieve it?
<?php
$sql="Select * from plays where usertype=2 and idgame=$idgame";
$result=mysqli_query($link, $sql);
while($row = mysqli_fetch_assoc($result)) {
$aword = $row['word'];
} ?>
<body>
<div id="row">
<span class="attemp">1 :</span>
<span class="word"><?php print //I want to print here only the 2nd fetched row ?></span>
</div>
<div id="row">
<span class="attemp">2 :</span>
<span class="word"><?php print //I want to print here only the 4th fetched row ?></span>
</div>
</body>
If when retrieving the data, you store it into an array, you can then output the data as you need to (remembering that arrays are 0 based)...
$aword = [];
while($row = mysqli_fetch_assoc($result)) {
$aword[] = $row['word'];
} ?>
<body>
<div id="row">
<span class="attemp">1 :</span>
<span class="word"><?php echo $aword[1]; ?></span>
</div>
<div id="row">
<span class="attemp">2 :</span>
<span class="word"><?php echo $aword[3]; ?></span>
</div>
</body>
Also I would suggest that if you only want the one column from the data, then select just that column rather than *...
$sql="Select word from plays where usertype=2 and idgame=".(int)$idgame;
and the obligatory comment about using prepared statements to stop SQL injection and some common errors.
$query = "Select * from plays where usertype=2 and idgame=$idgame";
$result = mysqli_query(isConnect(),$query);
while($row = mysqli_fetch_array($result));
{
echo "$row[0]";
}
?>

How do I insert a horizontal rule after a set of PHP MySQL Select Queries?

I am currently working on developing a PHP website, and I would like to add a section to the home page that displays news headlines. The information to populate this section, like all other content on the site, will be pulled from a MySQL database. I have added the code that will let me do this and a little CSS before each part to format the display on the page and that works wonderfully. But, what I cannot work out is how to display all the content I want and insert a horizontal rule after each MySQL row is read. Right now, if I insert more than one row into the database table, it only displays the information from the new row and replaces the info from the previous. Refer to the image below to see how the page currently appears.
Below is the code that I am using.
<div id="news">
<?php
$query = "SELECT news_date FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) {
$date = $row['news_date'];
}
?>
<?php
$query = "SELECT news_headline FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) {
$headline = $row['news_headline'];
}
?>
<?php
$query = "SELECT news_body FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) {
$body = $row['news_body'];
}
?>
<div class="date"><?php echo $date . "<br />" ?>
<div class="headline"><?php echo $headline . "<br />" ?>
<div class="headlinebody"><?php echo $body . "<hr />" ?>
</div>
As I've said, this will display the info the way I want it, but as soon as I add another "headline", the new replaces the old. I would like for all headlines to appear with a horizontal rule between the headlines (rows) and not each part. Any help you can provide would be appreciated. Thank you much in advance.
You can do this in 1 query and 1 loop.
<div id="news">
<?php
$query = "SELECT news_date, news_headline, news_body FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) { ?>
<div class="date"><?php echo $row['news_date']; ?></div><br />
<div class="headline"><?php echo $row['news_headline']; ?></div><br />
<div class="headlinebody"><?php echo $row['news_body']; ?></div><hr />
<?php }
?>
</div>

Issue with bootstrap grid and mysql response

I have an issue with bootstrap and creating a 4 column responsive grid from a mysql response.
The problem is that if the second mysql query has a variable number of results, it brakes the grid.
Here is my code (where the first query has 9 results and the second query has a variable number of results):
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result)) {?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $row['username'];
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
which gives me this:
enter image description here
instead of this (obviously with many 'Hello world'):
enter image description here
Any help greatly appreciated!
Bootstrap doesn't claim to do any kind of elegant bin-packing on panels with different sizes. You could do some programming or css work to make all your panels the same size.
If that doesn't work for your application, you're going to need a layout library that does bin-packing so these panels of different sizes align properly.
There are a few such libraries available as jQuery plugins.
In this, $row[username] is wrong as it should be $row['username'].
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
BTW, I changed your code bit. Please Try this.
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result))
{
$username=$row['username'];
?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $username;
$b = "SELECT * FROM $table_presents WHERE bought_for='$username' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
[NOTE: Users can inject your SQL commands. Use prepared statements and parameterized queries. For more info, click Prevent SQL Injections

MySQL select on PHP can't get it to work

I am having a problem retrieveing information from a MySQL Data Base. Could you help me out? This is the code I wrote:
<?php
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
while($nt = mysql_fetch_array($result)) {
?>
<div class="nuevos_1">
<div class="over_descripcion">
<div class="over_title">
<h3><?php echo $nt[titulo] ?></h3>
</div>
<div class="over_autor">
<span><b>Por: </b><?php print ucwords(str_replace("-", " ", $nt[autor])); ?> <?php echo $nt[fecha] ?></span>
</div>
<div class="over_texto">
<span><b><?php echo strtoupper(str_replace("-", " ", $nt[categoria])) ?></b> <?php echo substr(strip_tags($nt[texto]), 0, 240)."..." ?></span>
</div>
<div class="over_ver_mas">
<input type="button" value="Leer más" onclick="location.href='/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>'" />
</div>
</div>
<a href="/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>">
<img src="http://<?php echo $nt[imagen] ?>" border="0" />
</a>
</div>
<?php }; ?>
Nothing inside the SELECT notas database is showing. It is simply gone...
Thanks!
Limit 1 has to go after the ORDER BY
change your query to
SELECT * FROM notas ORDER BY id DESC LIMIT 1
what does the error message say
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
if (mysql_error()) {
die(mysql_error());
}
The other answers above have it correct - you've got a syntax error in your query. If you'd done the basic step of having:
$result = mysql_query(..) or die(mysql_error());
you'd have seen the error message. Never EVER assume that a query succeeds. Always check for error conditions.
As well, you've got a minor syntax bug in your output:
... <a href="/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>"> ...
^^^^^
Your array keys are not quoted, which means they're interpreted as define()'d constants. PHP is polite and will treat those constants as strings if there actually is not constant with that name, but it will issue a warning for every occurence. It should be:
<?php echo $nt['fecha'] ... ?>
I don't too well about PHP, but your select statement limits to just one result. You might want to change this line:
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
You will not get more than one result as it is.

Categories