display database field in textbox - php

I am trying to display a database input in a textbox for editing. I cant seem to get it working?!
The page links from a forum post edit button. Firstly, I don't know how to get the database info for that particular field to be displayed and also I don't understand how to make sure the post that the user clicks to edit is the post that is displayed.
My code looks like this
<?php
#data preparation for the query
$id=$_GET['id'];
# selects title and description fields from database
$sql = "SELECT a_answer FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<h3>Edit</h3> <form action="save_edit.php" enctype="multipart/form-data" method="post" name="myForm" />
<table>
<tr>
<td><b>Answer</b></td>
<td><textarea cols="80%" rows="10" name="a_answer"><?php echo $row['$a_answer']; ?></textarea></td>
</tr>
</table> <input name="id" type="hidden" value="<? echo $id; ?>">
<input name="enter" type="submit" value="Edit"> </form>
<?php mysql_close(); ?>

confirm if the question id is integer or varchar in your database. if it is integer then remove the single quotes. ( just a hint may it helps you. ).
One more thing you do not need to put the multipart in your form if you not uploading any files another tip form me.

Two errors in your code: Firstly, you are using row instead of rows. The second is the dollar sign you 're using when accessing the field. The code below should work, provided your database query is correct.
<?php
#data preparation for the query
$id=$_GET['id'];
# selects title and description fields from database
$sql = "SELECT a_answer FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<h3>Edit</h3> <form action="save_edit.php" enctype="multipart/form-data" method="post" name="myForm" />
<table>
<tr>
<td><b>Answer</b></td>
<td><textarea cols="80%" rows="10" name="a_answer"><?php echo $rows['a_answer']; ?></textarea></td>
</tr>
</table> <input name="id" type="hidden" value="<? echo $id; ?>">
<input name="enter" type="submit" value="Edit"> </form>
<?php mysql_close(); ?>
Another way to go about things would be to use prepared statements, and let PHP and MySQL quess the correct type. Here can be found a simple example.

Related

How to search tracking no like 1Z981Y1ED342132798 in PHP & MySQL

I have a tracking no. 1Z981Y1ED342132798 . But when I search query directly in phpmyadmin it shows the result but when I run the following through php the result will not show. I am sending the field value in GET method during submit my search form. My code is as follows
<?php
$test_qry = mysql_query(Select * from `table_name` where `tracking_no` like '%".$_REQUEST['srch']."%');
?>
<form name="search_form" action="" method="get">
<td>Search:</td>
<td>
<input type="text" name="srch" value="<?php echo $_REQUEST['srch']; ?>"> </td>
<td>
<input type="submit" name="Search" value="Search"> </td>
</form>
Very weird issue, tried a lot, any help will be appreciated. Thanks!
There is a convention error in your code
like '%".$_REQUEST['srch']."%')
Try replacing $_REQUEST['srch'] with $srch variable.
$srch = $_REQUEST['srch']

Form in Form is not allowed, but what else

I fetch SQL data with a while loop and insert the data into a table. With a form and a submit button i can save modified values.
My simplified code:
<table>
<tr>
<?php
//SQL QUERY
//...
while ($row = mysql_fetch_array($sql_header)) {
$var_ab = $row['ab'];
$var_id = $row['id'];
?>
<form id="form1" action="" method="POST">
<td>
<input type="text" value="<?php echo $var_ab; ?>"
</td>
<td>
<input type="text" value="<?php echo $var_id; ?>"
</td>
//PLACEHOLDER FOR SECOND FORM
<?php
}
?>
</tr>
<td>
<input type="submit" name="save" value="SAVE" class="classsubmit" />
</td>
</form>
</tr>
</table>
So far, so good. So, how can I insert a second form to delete an entry? I've tried to place this code (PLACEHOLDER FOR SECOND FORM - see above)
<td>
<form id="form2" action="" method="POST">
<input type="text" value="<?php echo $var_id;?>"
</form>
</td>
but it's not working and it's not allowed to nest forms.
Any suggestions?
If you only want to delete an entry on the page or in the database you could try a button or a span with an onclick function.
for example:
<span onclick="window.location='?delete=<?php echo $row[(unique-value-in-database-from-this-row)]; ?>'; return false">Delete entry</span>
Make sure you add return false or the first form will be submitted. If you use a button make sure it has type="button"
On this page could be a PHP code like this:
if(isset($_GET['delete']))
{
$item = $_GET['delete'];
//SQL connect
$result = mysql_query($conection ,"DELETE FROM table WHERE uniquevalue='$item'");
}
I hope gives an idea for a solution.

Getting specific table row based on id mysql DB

I have a database where I view all the records, the last column is the table ID, I click it and I want to be able to edit that rows data only, here's what I got but it doesn't waork after updating phpmyadmin:::
<?php
include "db.inc.php";
$id=$_GET['id'];
$order = "SELECT * FROM ircb where id='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="update.php">
<input type="hidden" name="id" value="<? echo "$row[id]"?>">
<tr>
<td>Date</td>
<td>
<input type="text" name="cdate"
value="<? echo "$row[cdate]"?>" size="30" style="color: black;background-color:#FFFF11">
</td>
</tr>
<tr>
<td>Item</td>
<td>
<input type="text" name="item"
value="<? echo "$row[item]"?>" size="30" style="color: black;background-color:#FFFF11">
</td>
</tr>
I end up getting no results returned, when I hover over the link it does display the correct table row id but when I click the link I get empty boxes containing some of the code like <? echo in the fields..no true values though..and the page header after clicking the link does show ::: ...../edit_form.php?id=8 for row 8 so I assume something in my query is not quite right.
thanks
It is because you try to GET a name "id" from a form which is POSTed ;) And also embed PHP in html more clearly. For example, value="<?php echo $row['id']; ?>" is more "true" rather than value="<? echo "$row[id]"?>" ;)

how to pass value of checked checkbox in php

Hi i am generating checkbox list by using a for each loop, I want to pass the value of checked checkbox into database.
foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list" value="1" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
when i checked the checkbox i want to pass the label name into the database can someone help.
Check this code. You need to take array as checkbox name i.e. list[]. Once form is submitted you can use it's value to insert in to database.
<?
if(isset($_POST['submit'])) // once form is submitted build your logic
{
$list = $_POST['list'];
foreach($list as $value)
{
echo "<br />Checked: $value";
// use $value to insert into database
$sql = "insert into...";
mysql_query($sql);
}
}
?>
<form name="frm" method="post">
<table>
<? foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list[]" value="<?php echo $list->nList ?>" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
<tr>
<td><input name="submit" value="Submit" type="submit" /></td></tr></table>
</form>
Ok, you've got a few problems there..
Firstly, please avoid opening php tags and closing them constantly. Just echo back the HTML. Going in and out of php so often will cause performance issues.
So, first off, you need to set up a form on here, your form needs to point to a second page (or self-process using an isset function, but let's go with the former solution for clarity). The second page is what's going to dump things into the DB for you based on what the user ticked.
So, here's the HTML for your form;
<form action="database.php" method="POST">
<div>
<label for="checkbox1">This is the first option:</label>
<input type="checkbox" id="checkbox1" name="checkbox1" />
</div>
<div>
<label for="checkbox2">This is the second option:</label>
<input type="checkbox" id="checkbox2" name="checkbox2" />
</div>
<div>
<input type="submit" />
</div>
</form>
Again, I'd recommend echo'ing this back in your foreach loop - a short function could accomplish this for you.
So, once someone hits the "Submit" button, they'll be taken to your second page (database.php as we've called it), and their options will be stored in the POST array. For good practice, always use POST rather than GET when dealing with DB entries.
So on this page, we'd check what checkboxes were selected, and then update the db as required;
if (isset($_POST['checkbox1']))
{
// user ticked checkbox1
$sql = "UPDATE table
SET 'checkbox1' = 1
WHERE 'user' = 'username';"
mysqli_query($sql);
}
Note that's just a rough solution - you haven't told me exactly how you're doing this, so I'm assuming your DB is tied to user's votes etc. You can also update checkbox1 by incrementing the value.. etc etc.
Hope this helps.
Eoghan
In the name you should use [] (because it's an array). So in this case it'd be name="list[]". Then when you submit it (either post or get) you just get the checked data on the list[] array. I'm not sure if I understood your question. If I didn't; please share a little more information.
I think this is what you're looking for:
foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list[<?php echo $list->nList ?>]" value="1" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
Then in PHP, you would do foreach($_POST['list'] as $listval) to loop over it again. Substitute $_POST for $_GET if you are using GET in your form.
Edit: I just noticed where you did echo $list->nList that this variable is either not going to change in your loop or it's a typo. Assuming it's a typo, it should be echo $lst->nList instead. If it's not a typo, then you want to just remove it and have name="list[]" in your HTML

How to get textbox value in to another page... php / MySQL

I need to get textbox value to another php page. By clicking 'Box' icon i want to submit perticular row only. I already got row ID to 'Box' icon. How can i do that with the while loop.
thanks in advance
Tharindu
You should arrange the HTML code for this in the following fashion:
<table>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
</table>
Then once you hit the image, it will submit the form. Add this code to the top of your PHP script "<?php print_r($_POST); ?> and you will see that you can now process the posted contents based on the data that was posted without the need for any while loop.
let the you have posted be list.php
in the text box like
<input type=text name="rid" value= " <?php echo $rid ?>" onclick="location.href='view.php'"/>
get the row id in next page that is view.php
$id = $_GET['rid'];
pass it as hidden in view.php
<input type="hidden" name="id" value="<?php echo $id; ?> "/>
Make sure all your db connection goes perfect and echo all data whatever you want from row.
In the original php file generate a different html form for each box.
<form action="page2.php" method="post">
<input name="Z067DA" />
</form>
In the page2.php file use code similar to this. $value contains the user
submitted information.
foreach($_POST as $key=>$value)
{
// Use submitted value.
}
If you know the input tag names in advance you can just access them directly in your php code.
$value = $_POST['Z067DA'];

Categories