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"]) {
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>";
}
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>";
hello guys i am trying to make a foreach loop in a while loop. in foreach loop i try to produce list elements with divs inside them but i have very strange output
here is my code
$countercat= 0;
$classvar= 1;
echo "<div class='selector-page'>";
echo "<div class='selector'>";
echo "<ul>";
while ($countercat <= 8){
$stmt=$conn->prepare('SELECT eidos, name, meta_keys, address, telephone, perioxi, st_img, st_open, st_close, lat, longtit FROM magazia WHERE perioxi= :perioxi AND eidos= :eidos ORDER BY st_id ASC');
$stmt->bindParam(':perioxi', $name, PDO::PARAM_STR);
$stmt->bindParam(':eidos', $eidos, PDO::PARAM_STR);
$eidos= $c_titles[$countercat]['c_name'];
$stmt->execute();
$allrows=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($allrows as $row) {
echo "<li>";
echo "<div class='p". $classvar . " w3-card targetDiv w3-margin-top cardsmar'>";
echo "<img src='../uploads/" . $row['st_img'] . "' class='cccard' alt='" . $row['name'] . "'";
echo "<div class='w3-container w3-center'>";
echo "<h3>" . $row['name'] ."</h3>";
echo "<p>" . $row['eidos'] . "</p>";
echo "<p>" . $row['address'] . " , " . $row['perioxi'] . "</p>";
echo "<p>" . $lang['wrlt'] . " : " . $row['st_open'] . "-" . $row['st_close'] . "</p>";
echo "<a href='katastimata.php?name=" . $row['name'] . "' role='button' class='w3-button w3-round w3black'>" . $lang['t9'] . "</a><br />";
echo "<a href='https://www.google.com/maps?q=loc:" . $row['lat'] . "," . $row['longtit'] . "' role='button' class='w3-button w3-round w3-green btnmar'>" . $lang['spot2'] . "</a>";
echo "</div>";
echo "</div>";
echo "</li>";
}
$countercat++;
$classvar++;
}
echo "</ul>";
echo "</div>";
echo "</div>";
}
?>
here is an image from my debugger consonle
as you see in the image inside in the ul tag exists only one li elemenemt
and the rest of them are out side ul /ul.
my first thought was that is not valid to put div tag in a li tag but this is not true if i use this in the top of my file
DOCTYPE html PUBLIC "-//W3C// DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/html1-transitional.dtd"
html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang:"en"
i am stack here for so long
what am i missing guys?
thanks in advance
vaggelis
You Didn't Close the <img> tag here.
echo "<img src='../uploads/" . $row['st_img'] . "' class='cccard' alt='" . $row['name'] . "'";
You Must Close the tag as
echo "<img src='../uploads/" . $row['st_img'] . "' class='cccard' alt='" . $row['name'] . "'/>";
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;
}
I am developing a php site to sort data for the exporting of terrestrial laser scanning data in a electrical design company. When one row of data is present the forms update in MYSQL fine, but as soon as I display 2 or more rows of data the multiple rows conflict with each other when the submit but is click.
My aim is to individually sort through each row of data separately and update via MYSQL when I click submit on the row. But like I said, multiple rows conflict. Can I some how re-code it to specify data entered in forms only on a certain row?
the problem also stems from wanting to display current existing data in each cell through the forms so I know which is entered and what is not.
I apologize immensely for the untidy coding, and as you may judge I am very new to this. It's not my profession.
while ($row = $result->fetch_object()) {
echo "<tr>";
echo '<td>' . $row->point_number . ' Delete</td>';
echo "<td>" . $row->easting . "</td>";
echo "<td>" . $row->northing . "</td>";
echo "<td>" . $row->reduced_level . "</td>";
echo "<td><input type='text' name='description' value='$row->description' size='2'/></td>";
echo "<td><input type='number' name='string_number' value='$row->string_number' size='1'/></td>";
echo "<td><input type='number' name='pole_number' value='$row->pole_number' size='1'/></td>";
// drop down menu for poles size
echo '<td><select name="pole_length">';
echo '<option>' . $row->pole_length . '</option>';
echo '<option value=' . $pole30 . '>30'</option>';
echo '<option value=' . $pole35 . '>35'</option>';
echo '<option value=' . $pole11 . '>11</option>';
echo '<option value=' . $pole12 . '>12</option>';
echo '<option value=' . $pole40 . '>40'</option>';
echo '<option value=' . $pole125 . '>12.5</option>';
echo '<option value=' . $pole13 . '>13</option>';
echo '<option value=' . $pole45 . '>45'</option>';
echo '<option value=' . $pole14 . '>14</option>';
echo '<option value=' . $pole15 . '>15</option>';
echo '<option value=' . $pole155 . '>15.5</option>';
echo '<option value=' . $pole17 . '>17</option>';
echo '<option value=' . $pole185 . '>18.5</option>';
echo '<option value=' . $pole20 . '>20</option>';
echo '</select></td>';
// echo '<td>' . $row[8] . '</td>';
// drop down menu for Pole strength
echo '<td><a>' . $row->pole_strength . ' </a><select name="pole_strength">';
echo '<option></option>';
echo '<option value=' . $poleA . '>A</option>';
echo '<option value=' . $poleB . '>B</option>';
echo '<option value=' . $pole5 . '>5</option>';
echo '<option value=' . $pole8 . '>8</option>';
echo '<option value=' . $pole12 . '>12</option>';
echo '<option value=' . $pole16 . '>16</option>';
echo '<option value=' . $pole20 . '>20</option>';
echo '</select></td>';
//echo '<td>' . $row[9] . '</td>';
// drop down menu for Pole Type
echo '<td><a>' . $row->pole_type . ' </a><select name="pole_type">';
echo '<option></option>';
echo '<option value=' . $poleWOOD . '>Wood</option>';
echo '<option value=' . $poleCONC . '>Concrete</option>';
echo '<option value=' . $poleSTEEL . '>Steel</option>';
echo '<option value=' . $poleSTOBIE . '>Stobie</option>';
echo '<option value=' . $poleIBCONC . '>I-be...Conc</option>';
echo '<option value=' . $poleFABST . '>Fab Steel</option>';
echo '</select></td>';
echo '<td>' . $row->pole_top. '</td>';
echo "<td><input type='text' name='remark' value='$row->remark'/></td>";
echo "<td>" . $row->circuitheight_1 . "</td>";
echo "<td>" . $row->circuitinsulatorleft_1 . "</td>";
echo "<td>" . $row->circuitremark_1 . "</td>";
echo "<td>" . $row->circuitheight_2 . "</td>";
echo "<td>" . $row->circuitinsulatorleft_2 . "</td>";
echo "<td>" . $row->circuitremark_2 . "</td>";
echo "<td>" . $row->circuitheight_3 . "</td>";
echo "<td>" . $row->circuitinsulatorleft_3 . "</td>";
echo "<td>" . $row->circuitremark_3 . "</td>";
echo "<td>" . $row->circuitheight_4 . "</td>";
echo "<td>" . $row->circuitinsulatorleft_4 . "</td>";
echo "<td>" . $row->circuitremark_4 . "</td>";
echo "<td>" . $row->circuitheight_5 . "</td>";
echo "<td>" . $row->circuitinsulatorleft_5 . "</td>";
echo "<td>" . $row->circuitremark_5 . "</td>";
echo "<td>" . $row->stayattachmentheight1 . "</td>";
echo "<td><button type='submit' name='submit' value=' . $row->point_number . '/>SEND</td>";
echo "</tr>";
}
echo "</table>";
if (isset($_POST['submit']))
{
$point_no = $_POST['submit'];
$point_number = substr($point_no, 2, 3);
$description = htmlentities($_POST['description'], ENT_QUOTES);
$string_number = htmlentities($_POST['string_number'], ENT_QUOTES);
$pole_number = htmlentities($_POST['pole_number'], ENT_QUOTES);
$pole_length = htmlentities($_POST['pole_length'], ENT_QUOTES);
$pole_strength = htmlentities($_POST['pole_strength'], ENT_QUOTES);
$pole_type = htmlentities($_POST['pole_type'], ENT_QUOTES);
$remark = htmlentities($_POST['remark'], ENT_QUOTES);
if ($description == '' || $string_number == '')
{
$error = 'ERROR: Please fill in all required fields!';
//echo "<h2>" . $row->$point_number . $easting . $northing . $reduced_level ."</h2>";
}
else
{
if ($stmt = $mysqli->prepare("UPDATE final SET description = ?, string_number = ?, pole_number = ?, pole_length = ?, pole_strength = ?, pole_type = ?, remark = ? WHERE point_number=?"))
{
$stmt->bind_param("siissssi", $description, $string_number, $pole_number, $pole_length, $pole_strength, $pole_type, $remark, $point_number);
$stmt->execute();
$stmt->close();
}
else
{
echo "ERROR: could not prepare SQL statement.";
}
}