...

View Full Version : php-mysql data update



nafees
06-20-2006, 10:12 PM
hello,

i'h developed code where the user will give the table name and the quary will fetch the data from that table and display the each field value in text box. but when the user see the table data he can make the value of any text box and then press update. then the table will updated with the new and old field value. the main problem is tht we make a script where the table structure is dynamic and different user can input different table which can have different number of fields.

please help me. :confused:

xconspirisist
06-21-2006, 04:50 AM
What is your question?

nafees
06-21-2006, 07:25 AM
hello,

well my question was i need a script which will update data for any table at a time.. so far what i have done is given below ..

please notice tht the code is for one given table, but i want the user to input the table so that he/she can do all the modification at a time .. the below is only the code to fetch the data and put it in the text box .. but when this user click update all the data will be updated into the database.


<?

$db = mysql_connect("localhost", "root", "");
if (!$db) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
$database="test1";
mysql_select_db("$database",$db);
$user_tablename = "student_info";


$table=$user_tablename;
if(!isset($query)|| empty($query))
{
$query="select *from $table";
}
$query=stripslashes($query);
$result = mysql_query($query);
if(!$result) error_message(sql_error());

$number_cols=mysql_num_fields($result);
echo "<BODY >";
echo "<form method=POST >";
echo "<TABLE width=90%>";
echo "<tr align=center >";
$j = 1 ;
for($i=0;$i<$number_cols;$i++)
{
echo "<th> <FONT size=2>". mysql_field_name($result,$i)."</font></th>\n";
}
echo "</tr>\n";

while($row=mysql_fetch_row($result))
{
echo "<tr align=center onMouseOver=\"this.style.backgroundColor='#336699'\" onMouseOut=\"this.style.backgroundColor='white'\">\n";
for($i=0;$i<$number_cols; $i++)
{
if(empty($row[$i]))
echo "<td><FONT size=2>-</font></td>";
else
{
$t_d= "t".$i.$j;
?>
<td><input type="text" name="<? $t_d ?>" size=10 value = "<? echo $row[$i]; ?>"></td>

<?
echo $t_d ;
}

}
$j=$j+1;
echo "</tr>";
}
echo "</table>";
echo "</body>";

?>


Table

-- phpMyAdmin SQL Dump
-- version 2.8.0.3
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 28, 2006 at 10:53 AM
-- Server version: 5.0.20
-- PHP Version: 5.1.2
--
-- Database: `test1`
--

-- --------------------------------------------------------

--
-- Table structure for table `student_info`
--

CREATE TABLE `student_info` (
`ID` varchar(7) collate latin1_general_ci NOT NULL,
`Name` varchar(50) collate latin1_general_ci NOT NULL,
`Address` varchar(75) collate latin1_general_ci NOT NULL,
`email` varchar(25) collate latin1_general_ci NOT NULL,
`phone` varchar(10) collate latin1_general_ci NOT NULL,
`blood_group` varchar(3) collate latin1_general_ci NOT NULL,
`Gender` varchar(1) collate latin1_general_ci NOT NULL,
`D_o_b` date NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

--
-- Dumping data for table `student_info`
--

INSERT INTO `student_info` VALUES ('0110031', 'Nafees Mansoor', 'flat 7/I, Aziz Housing Society, Shahbagh, Dhaka', 'nafees@iub.edu.bd', '0189412860', 'O-', 'M', '1983-12-31');
INSERT INTO `student_info` VALUES ('0110001', 'test', 'test', 'test', 'test', 'XX', 'X', '2006-05-15');

xconspirisist
06-21-2006, 12:17 PM
Please use CODE tags when pasting code.

You obviously get the table data via a query to the database. It's quite easy to build a form based on the structure of that data. If you include simple <input type = "hidden" /> elements, you should be able to emulate the table structure properly in order that it can be interpreted.

nafees
06-21-2006, 06:37 PM
thanks for the advice .. but i'm a it confused where to put the hidden tag and how it gonna help me ... and can u plz tell me how to update data when user insert the new data in any field ..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum