Saving a fetched result back into the database - php

i want know how I can save a result i fetched from my database back into the database.
I selected and fetched a row from a table in my database, there is a column from the row i want to insert in another table in the same database. I tried to turn the $row['example']; to a variable but it wont work, i echo the variable to be sure if its working but its not showing .
please i need a help , below is my codes.
<?php
$query =mysqli_query ($dbconnect, "select * from stuprofile where username ='$user' limit 1");
while ($row= mysqli_fetch_assoc($query)){?>
<?php
$uname= "USERNAME :";
$ebc= "EBC16";
$id ="MATRIC NO : ";
$wel = "WELCOME ";
$name ="FIRSTNAME : ";
$lname ="LASTNAME : ";
$sex ="SEX : ";
$add ="ADDRESS : ";
$mob ="MOBILE : ";
$inst ="INSTITUTION : ";
$chap ="CHAPTER : ";
$marr ="MARITAL STATUS : ";
$lc ="LOCAL CHURCH : ";
$bap ="BAPTISM : ";
$baptised ="BAPTISED : ";
$bank ="BANK : ";
$teller ="TELLER NUMBER : ";
$pay ="PAYMENT NUMBER : ";
$email ="EMAIL : ";
// dont forget to style your above varibles
//<span id ="ty">
?>
<div class="wel">
<?php
echo "<p>" . $wel . "<div id='ten' >" . " " . $row['firstname'] . " " . $row['lastname'] . "</div>" . "<p>" . "<br />" ;
?>
</div>
<!--<section><img src= "images/egliselogocomp.jpg" height="310" width: 50px> </section>-->
<p class= "names">
<?php
echo
$id . "<span >" . $ebc . $row['id']. "</span >" . " <br />" .
$name . "<span >" . $row['firstname']. "</span >" . "<br />" .
$lname . "<span >" .$row['lastname']. "</span >" . "<br />" .
$uname . "<span >" .$row['username']. "</span >" . "<br />" .
$sex . "<span >" .$row['sex'] . "</span >" . "<br />" .
$add . "<span >" .$row['address']. "</span >" . "<br />" .
$email . "<span >" .$row['email']. "</span >" . "<br />" .
$mob . "<span >" .$row['mobile']. "</span >" . "<br />" .
$inst . "<span >" .$row['institution']. "</span >" . "<br />" .
$chap . "<span >" .$row['chapter']. "</span >" . "<br />" .
$marr . "<span >" .$row['marital_status']. "</span >" . "<br />" .
$lc . "<span >" .$row['local_church']. "</span >" . "<br />".
$bap . "<span >" .$row['baptised']. "</span >" . "<br />" .
$baptised . "<span >" .$row['baptism']. "</span >" . "<br />" .
$bank . "<span >" .$row['bank']. "</span >" . "<br />" .
$teller . "<span >" .$row['teller_no']. "</span >" . "<br />".
$pay . "<span >" .$row['payment_date']. "</span >" . "<br />";
}
?>
</p>
<?php
} else {
header("location: ebcreg.php");
}
I need to insert the . $ebc . $row['id'] back into the database.
How do I do that please.
thanks

mysqli_query($dbconnect, "INSERT INTO ....");
The same way you selected your data, just change your query.

Related

foreach loop in while loop with div inside list element

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'] . "'/>";

Run UPDATE on single row within PHP loop

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"]) {

php if statement not sure how to do 2 if's

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;
}

Line break in table cell PHP

I am trying to perform several line break (br) in my table cell.
But I have problem with the syntax.
Below is my code to perform line break (br) within a single cell, but it doesn't work.
How should I go about doing it?
echo("<tr><td>".$row[0].
<br/>
$row[1].
<br/> $row[2].
<br/>
"<img src=images/".$row["skuCode"]."_1.jpg>".
<br/>
View".
"</td></tr>");
You're not using quotes properly, but I think it would be much easier to use a heredoc.
echo <<<HTML
<tr><td>$row[0]<br/>$row[1]<br/>$row[2]<br/>
<img src=images/$row[skuCode]_1.jpg<br/>
<a href=viewProductItem.php?skuCode=$row[0]>View</a>
</td></tr>
HTML;
You forgot the quotes around the <br/>s , try:
echo("<tr><td>".$row[0].
"<br/>" .
$row[1].
"<br/>". $row[2].
"<br/>" .
"<img src=images/".$row["skuCode"]."_1.jpg>".
"<br/>".
"View".
"</td></tr>");
Or alternatively:
echo("<tr><td>{$row[0]}
<br/>
{$row[1]}
<br/> {$row[2]}
<br/>
<img src=images/.$row[skuCode]._1.jpg>
<br/>
<a href=viewProductItem.php?skuCode={$row[0]}>View</a>.
</td></tr>");
You forgot to open and close the quotes in your string:
<?php
echo "<tr><td>".$row[0]."<br/>".$row[1]."<br/>".$row[2]."<br/><img src=images/".$row["skuCode"]."_1.jpg><br/>View</td></tr>";
?>
Try this:
echo "<tr><td>{$row[0]}<br/>{$row[1]}<br/>{$row[2]}<br/><img src=\"images/{$row['skuCode']}_1.jpg\"><br/>View</td></tr>";
You forgot to wrap the br tags around brackets, also you can use curly braces to never have to exit out of your string and concat it in php
echo "String with $variable works";
echo "String with {$array['key']} works with curly bracers."
If you want to make its HTML output well-formatted, then you could check this:
echo "<tr>\n";
echo " <td>" . $row[0] . "<br />\n";
echo " " . $row[1] . "<br />\n";
echo " " . $row[2] . "<br />\n";
echo ' <img src="images/' . $row["skuCode"] . '_1.jpg"><br />' . "\n";
echo ' View</td><br />' . "\n";
echo "</tr>\n";
Or this:
echo "<tr>\n";
echo " <td>" . $row[0] . "<br />\n";
echo " " . $row[1] . "<br />\n";
echo " " . $row[2] . "<br />\n";
echo " <img src=images/" . $row["skuCode"] . "_1.jpg><br />\n";
echo " View</td><br />\n"
echo "</tr>\n";
Or if you did not like those, look at this:
echo("<tr><td>" . $row[0] . "\n" .
"<br />\n" .
$row[1] . "\n" .
"<br />" . $row[2] . "\n" .
"<br />\n" .
"<img src=images/" . $row["skuCode"] . "_1.jpg>\n" .
"<br />\n" .
"View\n" .
"</td></tr>\n");

php pass parameter issue

This my code:
<?php
$lijstDoelmannen = mysql_query("SELECT * FROM Speler WHERE positie = 'Doelman' ORDER BY familienaam, voornaam");
$teller = 1;
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
The problem is that in the hyperlink the parameter spelerId = spaces (not filled in). If I echo $rij["id"], it gives me the right value.
You have a ' in the wrong spot in your href.
"...<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">..."
This should be:
"...<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>..."
<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">
You need to move the apostrophe:
<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>
It's currently ending the link, before the variable is added.
You can also do:
echo "<td><a href='spelerDetail.php?spelerId={$rij['id']}'
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
I prefer writing the above code this way to avid these types of issues:
while($rij = mysql_fetch_array($lijstDoelmannen)){
if($teller < 5){ ?>
<td><a href="spelerDetail.php?spelerId=<?php echo $rij['id'] ?>">
<img src="images/spelers/unknown.png" alt="" width="50" />
<br /><br /><?php echo $rij['id'] . " " . $rij['familienaam'] . " " . $rij['voornaam'] ?></a></td>
<?php }} ?>

Categories