Page 1 of 1
Problem updating multiple text field values
Posted: Wed Jun 02, 2004 10:08 pm
by anjani_piratla
Hi All,
I am using php & mysql.
I am facing problem with updating multiple text field values which come from database. It can be better explained with the following....
input text box 1:
<input type=text name=name value="some value from database(this comes from database">
input text box 2:
<input type=text name=name value="some value from database(this comes from database">
In this way some more text fields will be there which contain values from the database.
At the bottom of this page there will be one "Update All" button when clicked should update the database with the text field values (after user makes changes to these text fields)
If anybody can solve this problem I'll be very much glad.
Thank you very much in advance.
Re: Problem updating multiple text field values
Posted: Wed Jun 02, 2004 10:26 pm
by scorphus
Hi,
anjani_piratla wrote:(...)
input text box 1:
<input type=text name=name value="some value from database(this comes from database">
input text box 2:
<input type=text name=name value="some value from database(this comes from database">
(...)
1. Are all these text fileds named the same: <input type=text name=name>?
2. How are you populating these fileds from db?
Let's solve this part first.
- Scorphus
Posted: Thu Jun 03, 2004 10:44 am
by anjani_piratla
First of all thanks a lot for the reply.
All the text fields do not have the same name, because I've added id to the each text field i.e. "name_id". These text field are in separate html file. I am retrieving the data from the database through php script i.e.
$sql ="stmt";
$result = mysql_query($sql);
while(.........)
{
$value...
}
I am using template system. So this is how I get the values from the database.
Thank you
Posted: Thu Jun 03, 2004 2:29 pm
by scorphus
Ok, no problem.
See this script:
Code: Select all
<form name="form1" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<?php
foreach (range(1, 10) as $val)
echo "<input type="text" name="fieldName_$val" value="Text_$val" /><br />";
?>
<input type="submit" name="btnsubmit" value="Update">
</form>
<?php
if (isset($_POST['btnsubmit'])) {
$regExp = '#(fieldName)_([0-9]*)#si';
echo "<pre>\n";
foreach ($_POST as $key => $val)
if (preg_match($regExp, $key, $regs)) // [php_man]preg_match[/php_man](), [php_man]PCRE[/php_man]
echo "update table_name set ${regs[1]} = '$val' where table_id = ${regs[2]}\n";
echo "</pre>\n";
}
?>
and part of it's output:
Code: Select all
update table_name set fieldName = 'Text_1' where table_id = 1
update table_name set fieldName = 'Text_2' where table_id = 2
update table_name set fieldName = 'Text_3' where table_id = 3
update table_name set fieldName = 'Text_4' where table_id = 4
update table_name set fieldName = 'Text_5' where table_id = 5
update table_name set fieldName = 'Text_6' where table_id = 6
update table_name set fieldName = 'Text_7' where table_id = 7
update table_name set fieldName = 'Text_8' where table_id = 8
update table_name set fieldName = 'Text_9' where table_id = 9
update table_name set fieldName = 'Text_10' where table_id = 10
Also, a checkbox could be added beside each text field and a JavaScript function to check it when the corresponding text filed is changed (onChange=). Then PHP looks to see if the update is really necessary. JavaScript reference and guide:
JavaScript Central.
Good luck,
Scorphus.
Posted: Fri Jun 04, 2004 4:30 am
by anjani_piratla
Thanks for the reply.
I think I've not explained my problem clearly to you. So let me try to explain my problem clearly to you as below.....
I am using template system.
So I am having separate file as a template with the name xyz.tpl. In this template I am having the followin text field....
<input type="text" name="name_{ID}" value="{NAME}">
I am using this template and fetching the values from the database and displaying the some no.of text fields which contain database values.
I am using separate php file to fetch all those values and for displaying like in the following manner......
$template -> set_var(array('ID' = > $row['id']));
In the above mentioned manner I am fetching the values.
Now please tell me what is the solution.
Thank you very much for your instant response.

Posted: Fri Jun 04, 2004 5:02 pm
by scorphus
Yet not sure if I got you right.
If I understand you now, I think you are upgrading one row of your DB table. To do this you fetch the desired fileds and place them in text <input>s of a form. Each text <input> have a name like "name_<filed_name>". And you use this form to update the row in DB.
See the following code. It is the code above with few modifications:
Code: Select all
<form name="form1" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<?php
foreach (range(1, 10) as $val)
echo "<input type="text" name="fieldName_$val" value="Text_$val" /><br />";
?>
<input type="submit" name="btnsubmit" value="Update">
</form>
<pre><?php
if (isset($_POST['btnsubmit'])) {
$sql = 'update table_name set';
$regExp = '#(fieldName)_([0-9]*)#si';
foreach ($_POST as $key => $val)
if (preg_match($regExp, $key, $regs))
$sql .= ' ' . $regs[1] . ' = ''' . $val . ''',';
$sql = substr($sql, 0, -1) . ' where (CONDITION)'; // [php_man]substr[/php_man]
echo $sql;
}
?><pre>
returns this:
Code: Select all
update table_name set fieldName = 'Text_1', fieldName = 'Text_2', fieldName = 'Text_3', fieldName = 'Text_4', fieldName = 'Text_5', fieldName = 'Text_6', fieldName = 'Text_7', fieldName = 'Text_8', fieldName = 'Text_9', fieldName = 'Text_10' where (CONDITION)
How about this? Does it solve or help to solve?
-- Scorphus.