Page 1 of 1

How to get table values into textbox

Posted: Wed Jan 25, 2012 11:36 pm
by phpjawahar
Please help me on this.

I have a file called 'category.php' which display some of the categories from my database. Also have 'edit' button for each categories displaying.
On clicking the edit button, 'edit category.php' will be diplayed. The thing i need is, I want to bring the category names into the 'edit category.php' pages. so that i can edit and update the categories in to my database using these pages.

The coding for both these files are displayed here.

Here is category.php

Code: Select all

<?php
$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("ooi_admin", $con);
$query = "SELECT * FROM `specialization`";
$result=mysql_query($query, $con);
?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div align="center">
<table width="300" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="2"><div align="right">Add Category</div></td>
  </tr>
  <tr>
    <td><div align="center">Category Name</div></td>
    <td><div align="center">Manage</div></td>
  </tr>
  <?php
    while($row=mysql_fetch_array($result))
	{
     echo "<tr>";
	 $category=$row['s_name'];
     echo "<td>";
     echo "$category";
	 echo "<td>";
	 echo "<a href='edit category.php'><img src='images/edit.gif'/></a>";
	 echo "</td>";
	 echo "</tr>";
    }
?>
</table>
</div>
</body>
</html>
And here is edit category.php

Code: Select all

<?php
$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("ooi_admin", $con);
$query = "SELECT * FROM `specialization`";
$result=mysql_query($query, $con);
?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div align="center">
<table width="300" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="2">Edit Category</td>
  </tr>
  <tr>
    <td height="39"><div align="center">Category Name</div></td>
    <td><div align="center"><input type="text" name="category name"></div></td>
  </tr>
</table>
</div>
</body>
</html>

Re: How to get table values into textbox

Posted: Thu Jan 26, 2012 1:29 am
by social_experiment
You need to modify the category.php code a bit; currently when going to a page there is no way to select a specific record: add something unique for each record to the query string

Code: Select all

<?php
// add something unique to the url for edit category.php, maybe an id from the database
// or the category name
echo "<a href='edit category.php'><img src='images/edit.gif'/></a>";
?>
The sql query on the edit category page will also require editing to select only the record with the specific unique value. Also don't select everything (*) but select just the fields you need to use. The hidden field contains the unique value for the category which will be used when updating the category on submission.

Code: Select all

<?php
$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("ooi_admin", $con);
// this will change so something like SELECT id, category FROM specialization WHERE ... (your conditional code here)
$query = "SELECT * FROM `specialization`";
$result=mysql_query($query, $con);
//
$ary = mysql_fetch_array($result);
?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div align="center">
<table width="300" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="2">Edit Category</td>
  </tr>
  <tr>
    <td height="39"><div align="center">Category Name</div></td>
    <td><div align="center"><input type="text" name="category name" value="<?php echo htmlentities($ary['categoryField'], ENT_QUOTES); ?>" ></div></td>
    <input type="hidden" name="categoryId" value="<?php echo htmlentities($ary['categoryId'], ENT_QUOTES); ?>" >
  </tr>
</table>
</div>
</body>
</html>
?>
Hth