Display data from sql database to a textbox - php

I am performing a search of my sql database, and assigning the data from each row to a variable. Ex.
$query = mysql_query("SELECT * FROM `PropertyInfo` WHERE `sitestreet` LIKE '$street'");
// display query results
while($row = mysql_fetch_array($query))
{
$sitestreet = $row['sitestreet'];
$sitestate =$row['sitestate'];
}
Now my question is how do I get that info to display as text in a text box?
Thanks!

By echoing a textbox with those variables in the value="" attribute
echo "<input type='text' id='sitestreet' value='$sitestreet' />"
echo "<input type='text' id='sitestate' value='$sitestate' />"

It's as simple as:
<input value='<?php echo $sitestreet; ?>'>

You can fetch and assign in some time:
while($row = mysql_fetch_array($query))
{
echo "<input type='text' id='sitestreet' value='$row['sitestreet']' /><br />
<input type='text' id='sitestate' value='$row['sitestate']' />";
}

Related

Data from PHPMYADMIN implement in PHP Page into HTML Form to Send via PHP POST

I have data in my PHPMYADMIN in a few columns and rows.
Via PHP I read the Data out of my SQL Table and print it into a table.
The last of my table is a action button. So the whole table is a echo and within the table there's a html form in the last , but only with a submit button (input type="hidden") but the value should be the "id" out of my SQL table.
Here's the problem. How can I get the id of one row into the value of an input field? . $row["id"]. doesn't work. How can I fix this problem?
This is for a Website where the user can vote a table row up and then with the html form it is sending via http post to another page where it overrides the current number in the database with +1
$sql = "SELECT * FROM votingpoll ORDER BY votecount DESC";
$result = $conn->query($sql);
echo "$id";
echo "<table>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>
<form action='vote.php' method='post'>
<input type='text' name='id' value='$id'>
<input type='submit' value='VOTE'>
</form>
</td>
</tr>";
}
} else {
echo "0 results";
}
Thank you!!!
You can do $row[id] i.e. leave the quotes off when the array reference is used inside a double quoted string.
$sql = "SELECT * FROM votingpoll ORDER BY votecount DESC";
$result = $conn->query($sql);
// remove this it does not appear to do anything useful here
//echo "$id";
echo "<table>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>
<form action='vote.php' method='post'>
<input type='text' name='id' value='$row[id]'>
<input type='submit' value='VOTE'>
</form>
</td>
</tr>";
}
} else {
echo "0 results";
}
Or if you prefer you could use
<input type='text' name='id' value='{$row['id']}'>
It looks like you aren't setting $id anywhere. Try setting it from the results like $id = $row["id"];
Full example:
while($row = $result->fetch_assoc()) {
$id = $row["id"];
echo "<tr>
<td>
<form action='vote.php' method='post'>
<input type='text' name='id' value='$id'>
<input type='submit' value='VOTE'>
</form>
</td>
</tr>";
}

How do I autochange the name of radio buttons from while loop? php

Here is the scenario. in my database, I have like 4 questions. each questions have individual 5 radio buttons. I tried to retrieve the information from database. it shows my list of questions and radio buttons individually, but the BIG problem here is they are in the same group. example. in question 1, I picked 1st radio button, then in question 2, I picked the 2nd radio button. in question 1, the radio button I choose disappeared. so basically. the whole loop have single radio button name. how do I fix this dynamically? like auto change of radio-button-name for each question?
<form method='post' action='test.php'>
<?php
include 'db_connect.php';
$query = "SELECT * FROM test";
$result = $conn->query($query);
$num_results = $result->num_rows;
#if ($num_results > 0) {
#}
while ($row = $result->fetch_assoc()) {
extract($row);
echo $row['test1'];
echo "<input type='radio' name='question_button' value='1'>";
echo "<input type='radio' name='question_button' value='2'>";
echo "<input type='radio' name='question_button' value='3'>";
echo "<input type='radio' name='question_button' value='4'>";
echo "<input type='radio' name='question_button' value='5'>";
echo "<br>";
}
$testbutton = isset($_POST['question_button']) ? $_POST['question_button'] : "";
if (isset($_POST['submit'])) {
echo $testbutton;
}
?>
<html>
<input type='submit' name='submit' value='submit'>
</form>
</html>
P.S. Edit. my original intention is to add or get the sum of the radio buttons. what syntax should i use?
to change button name dynamically
change like following
$question=0;
while ($row = $result->fetch_assoc()) {
extract($row);
echo $row['test1'];
$question++;
echo "<input type='radio' name=".$question." value='1'>";
echo "<input type='radio' name=".$question." value='2'>";
echo "<input type='radio' name=".$question." value='3'>";
echo "<input type='radio' name=".$question." value='4'>";
echo "<input type='radio' name=".$question." value='5'>";
echo "<br>";
}
$question is just for example,
you can change redio button name according to your need.
I can't see what your database query is producing, but at a guess, I'd say you should be assigning a radio button id to your question in the database field. Then, when you get the results back, you can assign it with something like:
while ($row = $result->fetch_assoc()) {
extract($row);
echo "<input type='radio' name='".$row['question_name']."' value='".$row[question_id]."'>";
echo "<br>";
}
This should make every radio box unique so long as the data in the database is unique. If this doesn't help, may I see the results of the database query and I can edit my answer.
Simply give the radio button a group name differently as bellow example
// 1. Get the handler for counter.
$counter = 1;
// 2. Iterating through the results.
while ($row = $result->fetch_assoc()) {
$name = "question_" . $counter . "_button";
extract($row);
echo $row['test1'];
echo "<input type='radio' name=$name value='1'>";
echo "<input type='radio' name=$name value='2'>";
echo "<input type='radio' name=$name value='3'>";
echo "<input type='radio' name=$name value='4'>";
echo "<input type='radio' name=$name value='5'>";
// Increment the counter by 1 for each question.
$counter += 1;
}

PHP While Loop - Hidden Input Values - SQL

I'm trying to shows a number of rows one after the other, with a verify button underneath, which one clicked will go to the update (php) method. I've got it working, however it is always the same value (first alphabetical last name).
What I need to try and do is get it to pick the lastname from the row when submitted. Here is the code:
<div class="container">
<form action="update.php" method="POST">
<?php
//executes the SQL query
$result = mysql_query("SELECT * FROM Sheet1");
//returns the result from the database
while ($row = mysql_fetch_array($result)) {
$census = $row['Date'];
$WebsiteAddress = $row['WebsiteAddress'];
$LastName = $row['LastName'];
echo "<input type='hidden' name='HiddenInput' value='".$LastName."'>";
echo "<b>Last Name:</b> $LastName<br><br> <b>Website:</b> $WebsiteAddress<br><br> <b>Date:</b> $census<br><br>";
echo "<input name='".$LastName."' type='submit' value='Verify' /><hr>";
}
?>
</form>
</div>
You need to create array of input to get all the values like
<input type='hidden' name='HiddenInput[]' value='".$LastName."'>
Then $_POST['HiddenInput'] will return array that contain all value of $LastName
take a look at this code.. give different names to all the hidden fields..
$i = 0;
while ($row = mysql_fetch_array($result)) {
$census = $row['Date'];
$WebsiteAddress = $row['WebsiteAddress'];
$LastName = $row['LastName'];
echo "<input type='hidden' name='HiddenInput_".$i."' value='".$LastName."'>";
echo "<b>Last Name:</b> $LastName<br><br> <b>Website:</b> $WebsiteAddress<br><br> <b>Date:</b> $census<br><br>";
echo "<input name='".$LastName."' type='submit' value='Verify' /><hr>";
$i++;
}

how to display four radio buttons dynamically inside a dyanamically created html table?

this my table code
$mem=mysql_query("SELECT * FROM tbl_user_profile WHERE comp_id= $cmp");
while($result=mysql_fetch_array($mem))
{
echo"<tr>" ;
echo"<td><font color='red' size=4> ".$result['fname']." </font></td>";
echo"<td><font color='red' size=4 >".$result['emp_id']."</font></td>";
inside this table in the third column i tried while loop to show radio buttons.
$query = "SELECT * FROM tbl_house where event_id = $eid";
$hid = mysql_query($query);
while ($row = mysql_fetch_array($hid))
{
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<td> . <input type=\"checkbox\" name=\"q1\" value=\"$house_id\" />$house_name </td>";
}
echo "<td> <a href='house_member.php?operation=dele&id=".$result['profile_id']."'><input type='button' value='add' id='btn'></a></td>";
echo"</tr>" ;
echo"</table>";
}
its not working properly. somebody please help. i'm just a beginner in php.
waiting for a better solution.
if you want them all to be in the same column, don't put <td> around each of them, do that outside the loop. And the type of the input should be radio, not checkbox.
echo "<td>";
while ($row = mysql_fetch_array($hid)) {
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<input type='radio' name='q1' value='$house_id'/>$house_name ";
}
echo "</td>";
please change the <Input type="checkbox"> to <input type "radio">
and
echo "<input type='radio' name='q1' value='$house_id'/>$house_name "; this is the right way to display html code into php
Are you trying to add radio buttons in the third column or do you want to create a column for each radio??
my guess is that you remove the from within the while loop and place the starting and ending tags outside the while loop. something like
<td>
while loop
</td>
hope this helps
You have to use the following code at the place of your while loop.....
<td>
while ($row = mysql_fetch_array($hid))
{
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<input type='radio' name='q1' value='$house_id' />$house_name ";
}
</td>

What am I doing wrong with my PHP text boxes piece of code?

// Hy..I have the following piece of code:
mysql_select_db("baza_chestionar", $con);
$result = mysql_query("SELECT intrebari.descriere_intrebare,intrebari.nume_admin,intrebari.id_chestionar FROM intrebari WHERE intrebari.nume_admin='".$_SESSION['nume_admin']."' AND intrebari.id_chestionar='".$_SESSION['nr_chestionar']."' ");
$i=0;
while ($row = mysql_fetch_array($result))
{
$i++;
echo $i.")&nbsp". $row['descriere_intrebare'];
echo "<br><br>";
echo "<form method='POST' action='selectare_raspuns.php' id='myform' >
<input type='text' size='30' name='intrebare[]'>
</form> ";
echo "<br><br>";
}
echo "<input type='submit' value='Salveaza raspunsuri' onclick='myform.submit()'/>";
mysql_close($con);
// This select some data from a table and display it and for each data a textbox. I have another page that takes the data from the textboxes and insert it in another table. Here is the code:
foreach($_POST['intrebare'] AS $textbox)
{
$sql="INSERT INTO raspunsuri values ('','$textbox')";
var_dump($sql);
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}
// But it only inserts the first value of the first text box. What am I doing wrong? I declared the name of the text box as an array an I loop thru with a foreach statement..
You're looping the myform form as as well. The FORM tag should be taken out of the while loop and the submit button should be within the form tag (so you can use it without JS). Also, just print_r your POST data to confirm you have the data there.
Your problem is that you use more than one form tag in your page. So only one value text box is send to your second page. You must declare the form tag before the while loop and you close the tag after your loop.
Your form tag is echoed inside the loop. Put is outside.
echo "<form method='POST' action='selectare_raspuns.php' id='myform' >\n";
while ($row = mysql_fetch_array($result))
{
$i++;
echo $i.") ". $row['descriere_intrebare'] ."<br /><br /><input type='text' size='30' name='intrebare[]'><br /><br />";
}
echo "<input type='submit' value='Salveaza raspunsuri' name="submit" />\n";
."</form>\n";

Categories