Add link to image when echoing in PHP - php

In my post form, I have a "show"-page, where I echo out everything in the database. I'm trying to add a link to the image-line. But when I refresh the page, it's just white.
<?php
$sql_connection = mysql_connect("localhost", "user", "root");
mysql_select_db("database_name", $sql_connection);
$result = mysql_query("SELECT * FROM content ORDER BY id DESC");
while ($row = mysql_fetch_array($result)) {
echo "<div>";
echo "<img src=\"http://placehold.it/80x80\"> ";
echo "<br />";
echo "<br />";
echo "<h3>" . $row["who"] . "</h3><br />";
echo "<h4>" . $row["what"] . "</h4><br />";
echo "<h4>" . $row["where"] . "</h4><br />";
echo "<h3>" . $row["contact"] . "</h3><br />";
echo "<img src=\"../img/share_facebook.png\"> ";
echo "</div>";
}
mysql_close($sql_connection);
?>
Someone who can explain why this happens?

echo "<img src=\"http://placehold.it/80x80\"> ";
you have to escape() ALL your " or change to:
echo '<img src="http://placehold.it/80x80">';

Related

How to get a post in php where use sql like that :

How do i get the row id?
I'm doing a site of music, i need do the admin page,this part is when the admin gonna edit the albums. I need the id to specify what music gonna change the informations.
<?php
$db = mysqli_connect("localhost", "root", "", "datamusic");
$result = mysqli_query($db, "SELECT * FROM musics");
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img src='images/" . $row['Image'] . "' >";
echo "<p>" . $row['Band/Singer'] . "</p>";
echo "<form method='POST' action='editInformations.php'><p name=" . $row['id'] . ">ID = " . $row['id'] . "</p></form>";
echo "<a href='editInformations.php'><button type='submit'>Editar</button></a>";
echo "</div>";
}
?>
I try something like that, but doesn't works
//editInformations.php
$id =$_POST[$row['id']];
echo $id;
Get edit page GET funciton. You can put id for edit.
editInformations.php?id=".$row['id']."
Change code with this.
<?php
$db = mysqli_connect("localhost", "root", "", "datamusic");
$result = mysqli_query($db, "SELECT * FROM musics");
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img src='images/" . $row['Image'] . "' >";
echo "<p>" . $row['Band/Singer'] . "</p>";
echo "<form method='POST' action='editInformations.php'><p name=" . $row['id'] . ">ID = " . $row['id'] . "</p></form>";
echo "<a href='editInformations.php?id=".$row['id']."'><button type='submit'>Editar</button></a>";
echo "</div>";
}
?>
//editInformations.php
$id =$_GET['id'];
echo $id;

How to display two distinct MySQL tables on same PHP page

I have two sql tables I'm reading from and I would like to display each table as its own, separate table. First the internal, and then the external. And I'd like to have a simple title prefacing each one. For instance:
INTERNAL
Table here
EXTERNAL
Table here
What's happening though is the INTERNAL and EXTERNAL titles keep appearing
on the same line, or both come before the tables. Like this:
INTERNAL
EXTERNAL
Internal Table here
External Table here
I've tried including the titles as part of the php tags. Those are the currently commented out lines. And I've also tried adding the html outside of the php tags like this:
<br><br>
<b>INTERNAL</b>
<br><br>
<?php Internal Table here ?>
And then:
<br><br>
<b>EXTERNAL</b>
<br><br>
<?php External Table here ?>
But that still results in both titles appearing first, before the tables. Does the html get processed prior to the php? I must be interpreting this too linearly, like a script because it's certainly not being processed in the order in which it is coded on my page.
This is my code as is, which has the titles commented out. The tables appear sandwiched together which tells me they are not being interpreted as two distinct elements on the page. Do I need to put each one in it's own tag?
<?php
$internal = getInternalNetworkTable();
if ($internal->num_rows > 0){
//echo " <b>INTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $internal->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
} else {
echo "No results founds";
}
?>
<?php
$external = getExternalNetworkTable();
if ($external->num_rows > 0){
//echo " <b>EXTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $external->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
} else {
echo "No results founds";
}
$conn->close();
?>
EDIT:
Needed to add:
echo "</table>";
New code:
<?php
$internal = getNovatoInternalNetworkTable();
if ($internal->num_rows > 0){
echo " <b>INTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $internal->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
echo "</table>";
} else {
echo "No results founds";
}
?>
<?php
$external = getNovatoExternalNetworkTable();
if ($external->num_rows > 0){
echo " <b>EXTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $external->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
echo "</table>";
} else {
echo "No results founds";
}
$conn->close();
?>
You're missing </table> in both display of tables.
Therefore your table will be merge in each other.
echo '</table>';
Before both of your } else { should do the trick

SQL query not returning array

Working on getting a page to build off of an array that is returned from a DB to post a story, not sure what it is not working. The page URL looks like this: https://ohcrap.ninja/games/ps4/article.php?id=1
Here is the code that should be generating the content:
<?php
$id = $_GET['id'];
$query = mysqli_query($con,'SELECT * FROM `PS4` WHERE `id` =' .$id) or die(mysqli_error($con));
while ($row = mysqli_fetch_array($query));
// Echo page content
echo "<div class='col s12 m12 l12'>";
echo "<div class='card small grey darken-3'>";
echo "<div class='card-stacked'>";
echo "<div class='card-content'>";
echo "$id";
echo "<span class='card-title'>" . $row['title'] . "</span>";
echo "<hr color='black'>";
echo "<P>By:<i> " . $row['author'] . "</i></P>";
echo "<P>Published: " . $row['published'] . "</P>";
echo "<br>";
echo "<P class='truncate'>" . $row['story'] . "</P>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
Your while loop is not doing anything useful, because you're immediately ending it with that ;.
while ($row = mysqli_fetch_array($query)) {
// all those echoes
}

My dropdown list would not drop down

I tried to use php to create a dropdown list populated by a SELECT query but it would not dropdown. Here is my php script
<?php
require_once ('mysqli_connect.php');
#mysqli_select_db($dbc,"test");
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = #mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
// while($row = #mysqli_fetch_array($result))
// {
// echo "<p>$row[0] $row[1]</p>\n"; //this works
// }
echo "<td bgcolor=\"#E6E6E6\"";
echo "<strong>Category:</strong>";
echo "<select name=\"category\">";
while($row = #mysqli_fetch_array($result))
{
// echo "<p><option value='".$row[0]."'>".$row[1]."</option></p>\n";
echo "<option value='".$row[0]."'>".$row[1]."</option>";
}
mysqli_close($dbc);
echo "</select>";
echo "</td>";
?>
The query worked (the commented while loop outside the dropdown list displayed 15 records). But the dropdown list only showed one category_name and would not dropdown. Can someone help me figure out the problem? Thanks.
There are a few problems with your code.
You can either remove both echo "<td bgcolor=\"#E6E6E6\""; and echo "</td>";
which is the reason why your data is not showing.
Or, add the appropriate <table></table> tags.
Plus this echo "<td bgcolor=\"#E6E6E6\""; should be echo "<td bgcolor=\"#E6E6E6\">"; there was a missing > so that alone is breaking your <td>.
So in order to have the proper table syntax do:
Sidenote: I added . "\n" in order to get clean and well-aligned HTML source.
<?php
require_once ('mysqli_connect.php');
#mysqli_select_db($dbc,"test");
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = #mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
echo "<table>" . "\n";
echo "<tr>" . "\n";
echo "<td bgcolor=\"#E6E6E6\">" . "\n";
echo "<strong>Category:</strong>" . "\n";
echo "<select name=\"category\">" . "\n";
while($row= mysqli_fetch_array($result)){
echo "<option value='".$row[0]."'>".$row[1]."</option>" . "\n";
}
echo "</select>" . "\n";
echo "</td>" . "\n";
echo "</tr>" . "\n";
echo "</table>" . "\n";
mysqli_close($dbc);
?>
Edit: (Try this) since I don't know what your full code looks like.
<form id="form1" name="form1" method="post" action="form1_handler.php">
<?php
require_once ('mysqli_connect.php');
#mysqli_select_db($dbc,"test");
if(isset($_POST['submit'])){
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = #mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
echo "<table>" . "\n";
echo "<tr>" . "\n";
echo "<td bgcolor=\"#E6E6E6\">" . "\n";
echo "<strong>Category:</strong>" . "\n";
echo "<select name=\"category\">" . "\n";
while($row= mysqli_fetch_array($result)){
echo "<option value='".$row[0]."'>".$row[1]."</option>" . "\n";
}
echo "</select>" . "\n";
echo "</td>" . "\n";
echo "</tr>" . "\n";
echo "</table>" . "\n";
mysqli_close($dbc);
} // brace for if(isset($_POST['submit']))
?>
<input type="submit" name="submit" value="Submit">
</form>

Php - how to check whether a row exists and display different things based on the result?

I have an attraction page that displays some information from the database, and I've put a favorite button with each display. This is my code:
$villageId = $_GET['village'];
$sql = "SELECT `AttractionID`, `Name`, `Location`, `Description`, `Airport`, `imglink`,`imglink2`,`imglink3`,`imglink4`,`imglink5` FROM `attractions` WHERE `AttractionID`='$villageId'";
$result = mysql_query($sql, $link);
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo "<div class='attractions6'>";
echo "<div class='attractions5'>";
echo $row['Name'];
echo "<span title='Add to Favorites'>" . "<a href='favourite.php?village=" . $row['AttractionID'] . "'>" .
"<img src='img/fav.png'></a>";
echo "</span>" ;
echo "<br />" ;
echo "<br />";
echo "</div>";
echo $row['Description'];
echo "<br />" ;
echo "<br />";
echo " Location: ";
echo $row['Location'] ." ";
echo "<br />" ;
echo "<br />";
echo " Nearest Airport: ".$row['Airport'] ." ";
echo "<br />" ;
echo "<br />";
echo "</div>";
echo '<img src="'.$row['imglink'].' " height="200" width="550">';
echo "<br />";
echo '<img src="'.$row['imglink2'].' " height="180" width="180">';
echo "&nbsp";
echo '<img src="'.$row['imglink3'].' " height="180" width="180">';
echo "&nbsp";
echo '<img src="'.$row['imglink4'].' " height="180" width="180">';
echo "&nbsp";
echo '<img src="'.$row['imglink5'].' " height="180" width="180">';
echo " ";
//This is the favorite button. If the user clicks on it the script
//will redirect to the favourite.php page where the database will be
//checked for duplicates in the attractionID field and will save the favorite.
echo "<br />";
echo "<br />";
}
mysql_free_result($result);
?>
I have the following code which I'm using on other places of the website that can check whether the input already exists:
$sql="SELECT `ID`,`AttractionID` FROM `favourites`
WHERE `ID`='$ID' AND `AttractionID`='$villageId' ";
$result = mysqli_query($con,$sql);
if ($result->num_rows > 0) {
header('location:attraction.php?village='.$villageId);
}
On the attraction page how can I make it so that if the user already saved this entry to favorites - I will display picture1 and if they didn't - I will display picture two?
Thanks.

Categories