I'm kind of a beginner in php but here is my question. I got this var div filled with an array from my database but somehow it doesn't display the first row. Does any of you may have an idea why this could be?
$sqlposts = "SELECT * FROM `posts` ";
$result = $conn->query($sqlposts);
$row = $result->fetch_assoc();
$div = array();
$div[] = "<div>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
$div[] = "<br>" . "<br>" . "Email: " . $row["email"] . "<br>" .
"Titel: " . $row["titel"] . " <br> " . "Message: " .
$row["post"] .
"<br>" . "<form action='' method='post'>
<a href='adjust.php?action=delete&id=" . $row['ID'] . "'>
delete</a>
<a href='adjust.php?action=aanpassen&id=" . $row['ID'] ."'>
aanpassen</a>
</form>
<br>";
print_r($row);
}
} else {
echo "0 results";
}
$div[] = "</div>";
Related
The following php code produce a form with 2 inputs tag, the problem is that the form self-closes at exactly after opening it.
There are no other tags closing between (I read it could have been a parent closing inside the form).
echo "<form method='POST' action='player.php'>
<input type='submit' value='Info' name='submit'>
<input type='hidden' name='id' value='" . $row["id"] . "'>
</form>";
This echo is inside a while fetch array, result of a mysql query.
The result, from chrome dev tools is:
EDIT:
The complete loop is (it is pretty messy and there are a few things that are not best practice):
Basically I am showing a ranking, the top 3 is displayed inside a div, and the rest of the ranking are displayed in a table. The form that sends to the player.php page in the table works, but the one in the div (that is the exact same form without doesn't).
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//if top 3 show in a div
if ($row["rank"] == 1 or $row["rank"] == 2 or $row["rank"] == 3) {
if($row["rank"] == 1) {
echo "<div class='center'>";
echo "<div class='player first'>";
}
else if($row["rank"] == 2) {
echo "<div class='player second'>";
}
else if($row["rank"] == 3) {
echo "<div class='player third'>";
}
//player img
echo "<img class='player' src='";
$position = strpos($row["name"], ",");
$filename = substr($row["name"], 0, $position);
if (file_exists("src/players/" . $filename . ".png")) {
echo "src/players/" . $filename . ".png";
}
else {
echo "src/players/404.png";
}
echo "'>
<h2>" . $row["rank"] . "° - " . $row["rating"] . "</h2>
<h1>" . $row["name"] . "</h1>";
echo "<h3>" . $row["country_name"] . "</h3>
<h5>" . $row["title_name"] . " - " . $row["games"] . " games</h5>";
//possibility to add to favourites only to login users
if ($_SESSION["is_login"]) {
$id_user = $_SESSION["id"];
$id_player = $row["id"];
$sql2 = "SELECT * FROM fav_players WHERE id_user = $id_user AND id_player = $id_player";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
echo "<i class='fas fa-heart' onclick='addPlayerToFav(this, " . $row["id"] . ")'> </i>";
}
else {
echo "<i class='far fa-heart' onclick='addPlayerToFav(this, " . $row["id"] . ")'> </i>";
}
}
else {
echo "<i class='far fa-heart' onclick=\"window.location.href='account.php'\"></i>";
}
//the form that doesn't work
echo "<form method='POST' action='player.php'>
<input type='hidden' name='id' value='" . $row["id"] . "'>
<input type='submit' value='Info' name='submit'>
</form>";
echo "</div>";
continue;
}
//the table
echo "<tr>";
echo "
<td>" . $row["rank"] . "°</td>
<td>" . $row["name"] . "</td>
<td>" . $row["rating"] . "</td>
<td>" . $row["country_name"] . "</td>
<td>" . $row["title_name"] . "</td>
<td>" . $row["games"] . "</td>";
//the form that works
echo "<td class='center'><form method='POST' action='player.php'><input type='hidden' name='id' value='" . $row["id"] . "'>
<input type='submit' value='Info' name='submit'></form></td>";
echo "<td class='center'>";
if ($_SESSION["is_login"]) {
$id_user = $_SESSION["id"];
$id_player = $row["id"];
$sql2 = "SELECT * FROM fav_players WHERE id_user = $id_user AND id_player = $id_player";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
echo "<i class='fas fa-heart' onclick='addPlayerToFav(this, " . $row["id"] . ")'></i>";
}
else {
echo "<i class='far fa-heart' onclick='addPlayerToFav(this, " . $row["id"] . ")'></i>";
}
}
else {
echo "<i class='far fa-heart' onclick=\"window.location.href='account.php'\"></i>";
}
echo "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
echo "</table>"
The form is auto-closing due to a div not closed (the class="center" div). When the div with the top 3 is closed and the first table row is opened the browser auto-closes the form.
Fixed adding the following code before the table echo to close the div only after the 3 player, so after the podium is finished.
if($row["rank"] == 3) {
echo "</div>";
}
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;
I have written this code, sorry I'm pretty new to PHP.
I have a loop which grabs all the horses from the database.
I have created a sell button underneath each entry, the problem I have is that when I hit "Sell" on a single entry it "Sell"s ALL of the entries.
Here is my code;
$query = "SELECT * FROM horses WHERE (owner = '$id') AND (status = 'active')";
if($result = $connect->query($query)) {
echo "<ul id='horses' class='row'>";
while ($row = $result->fetch_assoc()) {
echo "<li class='col-sm-4'>";
echo "<div class='horse-wrap'>";
echo "<h2>" . $row['name'] . "</h2>";
echo "<div class='details'>";
echo "<strong>Age:</strong> " . $row['age'] . "<br>";
echo "<strong>Colour:</strong> " . $row['colour'] . "<br>";
echo "<strong>Country:</strong> " . $row['country'] . "<br>";
echo "<strong>Value:</strong> £" . number_format($row['value']) . "<br>";
echo "</div>";
echo "<h3>Record</h3>";
echo "<ul class='history row'>";
echo "<li class='col-sm-3'>Runs: " . $row['runs'] . "</li>";
echo "<li class='col-sm-3'>Wins: " . $row['first'] . "</li>";
echo "<li class='col-sm-3'>2nd: " . $row['second'] . "</li>";
echo "<li class='col-sm-3'>3rd: " . $row['third'] . "</li>";
echo "</ul>";
echo "<ul class='skills'>";
echo "<h4>Speed</h4><li><span class='skill' style='width: " . $row['skill_speed'] . "%;'>" . $row['skill_speed'] . "</span></li>";
echo "<h4>Jumping</h4><li><span class='skill' style='width: " . $row['skill_jump'] . "%;'>" . $row['skill_jump'] . "</span></li>";
echo "<h4>Temperament</h4><li><span class='skill' style='width: " . $row['skill_temperment'] . "%;'>" . $row['skill_temperment'] . "</span></li>";
echo "<h4>Endurance</h4><li><span class='skill' style='width: " . $row['skill_endurance'] . "%;'>" . $row['skill_endurance'] . "</span></li>";
echo "</ul>";
// SELL HORSE BUTTON
echo "
<form action='./stables.php' method='post'>
<input type='hidden' value='" . $row['value'] . "' name='sellvalue' />
<input type='hidden' value='" . $row['name'] . "' name='sellname' />
<input type='submit' value='Sell Horse' name='sell' class='sell' />
</form>
";
echo "</div>";
echo "</li>";
// SELL HORSE
if(!empty($_POST["sell"])) {
//REMOVE HORSE FROM USER
$horseName = $row['name'];
$status = 'sold';
if($updateHorse = $connect->prepare("UPDATE horses SET status = ? WHERE name = ?")) {
$updateHorse->bind_param('ss', $status, $horseName);
$updateHorse->execute();
$updateHorse->close();
}
//ADD MONEY TO USER
$updateAmount = $money + $row['value'];
if($updateUser = $connect->prepare("UPDATE users SET money = ? WHERE id = ?")) {
$updateUser->bind_param('ss', $updateAmount, $id);
$updateUser->execute();
$updateUser->close();
}
}
}
echo "</ul>";
} else {
echo "You currently have no horses.";
}
You're doing the update inside of a loop, but you're never checking to see if the loop's current horse is the one the customer is trying to sell. As it's written, it is going to execute for every one.
You're checking to see if the form was submitted:
// SELL HORSE
if(!empty($_POST["sell"])) {
This is the perfect place to also make sure you have the right horse:
// SELL HORSE
if(!empty($_POST["sell"]) && $row["name"] === $_POST["sellname"]) {
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>
I'm trying to do a condition within an if statement.
I have a current if statement which determines odd and even for css styling.
Within this I want to add another if statement for the reference number. (outlined as Ref in the code).
I'm not sure how to go about this but I want to add something along the lines of
if ref == 1 then echo ref, I've tried putting an if within an if and I just keep getting syntax errors. Can someone point me in the right direction please?
include "db_connect.php";
$result1 = mysql_query("SELECT * FROM updates ORDER BY id DESC limit 5");
$result2 = mysql_num_rows($result1);
$x = $result2;
while ($row1 = mysql_fetch_array($result1)) {
if (++$x % 2) {
echo "<blockquote class='example-right'>" . $row1['update'] .
"<div class='ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
echo "</p>";
} else {
echo "<blockquote class='example-obtuse'>" . $row1['update'] .
"<div class='ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
the bit I tried to do with 2nd if statement:
include "db_connect.php";
$result1 = mysql_query("SELECT * FROM updates1 ORDER BY id DESC limit 5");
$result2 = mysql_num_rows($result1);
$x = $result2;
while ($row1 = mysql_fetch_array($result1)) {
if (++$x % 2) {
$sch = $row1['schuedled'];
else if ($sch == '1') {
echo "<blockquote class='example-right'>" . $row1['update'] .
"<div class='ref'>Ref: " .$row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
else {
echo "<blockquote class='example-right'>" . $row1['update'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
echo "</p>";
} else {
echo "<blockquote class='example-obtuse'>" . $row1['update'] .
"<div class='assyst-ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
Try something like this:
while($row1 = mysql_fetch_array($result1)){
$html = '';
if (++$x % 2 ){
$html .= "<blockquote class='example-right'>";
} else {
$html .= "<blockquote class='example-obtuse'>";
}
$html .= $row1['update'];
if($row1['schuedled'] == '1'){
$html .= "<div class='ref'>Ref: " .$row1['ref'] . "</div>";
}
$html .= "more html";
$html .= "more html";
$html .= "more html";
$html .= "</blockquote>";
echo $html;
}