cant get id value in to the html form - php

I'm trying to update the table in DB and every row (result) has its own update button which is a form. When i click the button nothing happens because I don't know how to transfer value of id to a form and then to a UPDATE query.
while(list($naziv,$tvrtka_id)=mysqli_fetch_row($resultA))
{
echo "<tr>";
echo "<td>".$naziv."</td>";
echo "<td>"?>
<form action="" method="POST">
<input type="hidden" name="id" value="<?php $tvrtka_id; ?>">
<input type="submit" name="odobriZahtjev" value="Odobri zahtjev">
</form> <?php "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
} //this is from a if statment which creates table
if(isset($_POST['odobriZahtjev']))
{
$firmId = $_POST['id'];
$updateAnswers = "UPDATE tvrtka
SET zahtjev = '0', preostaliOdgovori=preostaliOdgovori + 10
WHERE tvrtka.tvrtka_id='$firmId'";
$result=queryDB($connect,$updateAnswers);
}
When button is clicked the value of a request for answers is set to 0 and 10 answers are added to company.
tvrtka_id is the id which is supposed to go to a UPDATE query.

When using a PHP var inside HTML you have to print it to the HTML (using echo in your case).
<input type="hidden" name="id" value="<?php echo $tvrtka_id; ?>">
This will probably solve the problem with your update query when the form is submitted too

Related

Delete data from database using php and sql

So I have a php page that looks like this
<?php
echo "<table border='1' width= 300px >
<tr>
<th>Friend Names</th>
<th>Remove Friends</th>
</tr>";
while($row = mysqli_fetch_assoc($result2))
{
$friend_id_got = $row['friend_id2'];
$query3 = "SELECT profile_name
from friends
where friend_id = '$friend_id_got' ";
$result3 = $conn->query($query3);
$final3 = mysqli_fetch_assoc($result3);
echo "<tr>";
echo "<td>" . $final3['profile_name'] . "</td>";
echo "<td>"
?>
<form action="friendlist.php" method= "POST">
<button id="add-friend-btn" type= 'submit' name = 'submit'>Unfriend</button>
</form>
<?php
"</td>";
echo "</tr>";
}
echo "</table>";
When I press the button, I need the corresponding name to delete it. The only problem I'm facing is that How do I get the name corresponding to the button.
I think we need to relate the buttons and the name somehow, so when a specific button is pressed i get the corresponding name
From the question and comments, and a glance at your code it sounds like you probably actually need two pieces of data to be submitted to the server when your button is clicked:
The action which should be undertaken in response to the request (i.e. unfriending)
The ID of the person being unfriended
To achieve that you can add some hidden fields to your form. These are invisible to the user but will be available to PHP in the $_POST data when the form is submitted.
Something like this:
<form action="friendlist.php" method= "POST">
<input type="hidden" name="unfriend_id" value="<?=$friend_id_got ?>" />
<input type="hidden" name="action" value="unfriend" />
<button id="add-friend-btn" type="submit" name= "submit">Unfriend</button>
</form>
Following on from the comment from #ADyson:
<form action="friendlist.php" method= "POST">
<input type="hidden" name="cancel_id" value="<?=$friend_id_got ?>" />
<button id="add-friend-btn" type="submit" name="submit">Unfriend</button>
</form>
By including a hidden field in the form, you're able to store more information.
You can see that I'm storing the ID of the friend you're unfriending in the value of the hidden field, so when the form is submitted (the button is clicked) you'll have access to "cancel_id" in the POST data, which will obviously contain the ID of the friend to unfriend.

Get specific data after output everything from database

I've got here a table with all the data from my database. On the end on every row, I have put a checkbox with the value 1, to update the 'accept' status in my database, which is default by 0.
My problem is, that the value of a ticked checkbox should update the 'accept' status ONLY in the entry in its row after the submit button got pressed.
So basically I need to check if the checkbox is ticked, and if its ticked, the 'accept' status in the row of the checkbox gets the value of 1. For that, I think I need to get the 'match_id' of the row.
This is how it looks on the website:
This is the table:
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db('lr') or die(mysql_error());
$result = mysql_query("SELECT * FROM `challenge`") or die(mysql_error());
echo "<table class='match-table'>";
echo "<thead><tr> <th><h1>match_id</h1></th> <th><h1>Team</h1></th><th><h1>Accept Status</h1></th><th><h1>Accept</h1></th> </tr></thead><tbody>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>";
echo $row['match_id'];
echo "</td><td>";
echo $row['team'];
echo "</td><td>";
echo $row['accept'];
echo "</td><td>";
?>
<form action="" method="post">
<input type="checkbox" name="challenge_accept" value="1"/>
<?php
echo "</td></tr>";
}
echo "</tbody></table>";
?>
<input type="submit" value="Save"/>
<input type="reset" value="Delete"/>
</form>
Any suggestions how I can do that?
Your HTML is utterly invalid, with MULTIPLE <form> being opened and only one </form> at the end of the page. You're also splitting your tags into garbage like
<tr><td><form></td></tr>
[many repeats]
</table>
</form>
Your form fields also don't include ANY method of identifying which row was actually clicked on, e.g. <input type="submit" name="selected_row" value="<?php echo $row['id'] ?>" />. As it stands right now, you've hardcoded the value 1, so no matter which of however many buttons you spit out, they'll ALL be sending 1 back to your code.

Putting fetched array into <a href> value in PHP

I have a SQL query statement that will return a particular set of result. Such as ID, Names, Price. I have no problem with that.
However i am trying to add a link within the echo loop and set ID as the value so that i can post it to another page. Would that be possible ?
while ($row = mysql_fetch_array($result)) {
echo
"".$row{'Url'}."<br>";
echo
"Name:".$row{'Name'}."<br>";
echo
"Price: $ ".$row{'Price'}."<br>";
echo
'<div class = "qwerty" data-countdown= '.$row{'Time'}.'></div>';
echo
"Location:".$row{'Location'}."<br>";
echo
"Description:".$row{'Description'}."<br>";
echo ''.$row{'ID'}.'';
echo 'Show Comments
<form id="displayComments" style="display:none" target="jsScript()" method="post">
<input type="hidden" name="run" value=".$row{'ID'}." />
</form>';
You missed quotes. value="'.$row['ID'].'"
echo 'Show Comments
<form id="displayComments" style="display:none" target="jsScript()" method="post">
<input type="hidden" name="run" value="'.$row['ID'].'" />
</form>';
And php array use [ ]
http://ua2.php.net/manual/en/language.types.array.php

Posting a variable that is unique to a row, how do I do this?

I am POSTING two things. The comment, which works ok, but the second item I need to post is the $list['id'] that is unique to this each row. How do I include this unique id, when the user clicks POST so that it can be used on the page that it is being posted to.
foreach ($posts as $key => $list){
echo " <tr valign='top'>\n";
echo " <tr>$list['id']
<div class='comment_text'>
<form method='post' action='add_comment.php'>
<textarea name='comment'</textarea>
<input class='btn' type='submit' value='Post'/>
</form>
</div>
</td>\n";
echo "</tr>\n";
}
The page I am posting to looks like this:
<?php
$commenter_user_id = $_SESSION['user_id'];
$body = substr($_POST['comment'],0,400);
$post_id=;
add_comment($commenter_user_id,$post_id,$body);
$_SESSION['message'] = "Your comment has been added!";
header("Location:/social_learning/site_pages/profile.php");
?>
You can use hidden input:
<input type="hidden" name="postName" value="<?= $list['id'] ?>" />
Then in your PHP it's available in $_POST['postName'] (in accordance to the name attribute of the hidden input)

Putting SQL information into a HTML/PHP form

I've been having a rather irritating issue regarding capturing SQL information and then placing it into a PHP form (in theory, it should be kinda easy).
Here's the code for the SQL database information:
<?
$select = "SELECT * FROM beer WHERE country_id = 3";
$data = mysql_query($select) or die("Unable to connect to database.");
while($info = mysql_fetch_array($data)) {
echo '<center>';
echo '<h2>'.$info['name'].'</h2>';
echo '<table style="padding:0px;"><tr>';
echo '<tr><td><b>ABV%:</b></td><td width="570">'.$info['abv'].'</td></tr>';
echo '<tr><td><b>Bottle Size:</b></td><td width="570">'.$info['bottleSize'].'</td></tr>';
echo '<tr><td><b>Case Size:</b></td><td width="570">'.$info['caseSize'].'</td></tr>';
echo '<tr><td><b>Price:</b></td><td width="570">$'.$info['price'].'</td>';
echo '</tr></table>';
echo '</center>';
echo '<br/>';
echo '<img src="" border="0"><br><br>';
echo '<form name="cart" method="post" action="cart.php"> <table border="0"> <tr>';
echo '<td><input type="hidden" name="bname" value="'.$info['name'].'"><input type="hidden" name="price" value="'.$info['price'].'"></td>';
echo '<td><b>Quantity:</b></td>';
echo '<td><input type="text" name="qty" size="3"></td>';
echo '<td><input type="submit" value="Add to Cart" a href="cart.php?name=foo&price=bar" /a></td>';
echo '</tr></table></form>';
}
?>
I want when the submit value is pressed to somehow transmit the price, quantity and name to a basic HTML form (so that all the user has to do is add name, address, etcetc). I am completely stumped on how to do this.
If anyone could help, it would be much appreciated.
As you mentioned Amazon checkout, here is one thing you probably don't understand.
Amazoin doesn't use the form to move items data between server and browser to and fro.
It is stored in a session on a server time. All you need is some identifier put into hidden field.
To use a session in PHP you need only 2 things:
call session_start() function before any output to the browser on the each paghe where session needed.
Use `$_SESSION variable.
That's all.
Say, page1.php
<?
session_start();
$_SESSION['var'] = value;
and page2.php
<?
session_start();
echo $_SESSION['var'];
You wrote that code? because it's simply the same code as here.
You'll need to write an HTML form in your cart.php file
and use the $_POST variable to show the values of the price , quanitity and name.
For example:
<form method='post'>
<input type='text' name='price' value='<?=$_POST['price']?>'>
<input type='text' name='quanitity' value='<?=$_POST['qty']?>'>

Categories