how to update mysql from a multiple input

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stanley
Forum Newbie
Posts: 6
Joined: Mon Feb 14, 2005 8:37 am

how to update mysql from a multiple input

Post by stanley »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a program who list out the data from MySql database using a do command, below is the example:

Code: Select all

<?
require_once('connect.php');   // this file store the password
$db = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database, $db);

$query_Recordset1 = "SELECT * FROM customer ORDER BY name";
$Recordset1 = mysql_query($query_Recordset1, $db) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

do {

?>

<html>
<table border="1" width="100%">
  <tr>
    <td width="50%">Customer Name:</td>
    <td width="50%"><input name="name" type="text" id="name" value="<? echo $row_Recordset1['name']; ?>" size="30" maxlength="30">td>
  </tr>
</table>

<?
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) ;

?>
My problem is as follow:

let say my database contains about 50 records and all 50 records will be output on the screen. The input program will allow user to key-in the new value on all the 50 records. How do I update the value into MySql database ?


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I don't quite understand what you are talking about but...

First, use

Code: Select all

tags around all PHP code.

Second, you may want to look up the UPDATE and INSERT sql commands.
stanley
Forum Newbie
Posts: 6
Joined: Mon Feb 14, 2005 8:37 am

Post by stanley »

Sorry for the unclear post, the problem I'm facing is actually how to collect the data from $_POST command since the display are from the loop, the loop will output all the data from mysql files such as:

1. name1
2. name2
3. name3
4. name4
........

all the above 1,2,3,4 ..... are using input command to display <input name="name" type="text" id="name" value="<? echo $row_Recordset1['name']; ?>" size="30" maxlength="30"> in order to allow user to input new value.

When user click on the UPDATE button on the form, name1, name2, name3, name4 etc will carry the same value which is the last input in the form, in this example value are from name4. If you see carefully name1, name2, name3 etc are using the same variable call "name".

How can I have actual value key-in by user for name1, name2, name3 etc so I can insert into Mysql database without any problem.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

First, you might want to look into the HTML array thing:

Code: Select all

<input type="text" name="name[]" value="'.$row_Recordset1['name'].'" />
The reason that you are only get the value from the last input is because they all have the same name. The name attribute of an input tag has to be unique, or an array thingy. Otherwise it just gets over-written.

Another suggestion to not create 800 tables and html tags on your page:

Code: Select all

while ($row_Recordset1 = mysql_fetch_assoc($Recordset1))
{
   print '<tr>';
   print '<td width="50%">Customer Name:</td>';
   print '<td width="50%"><input name="name[]" type="text" value="'.$row_Recordset1['name'].'" size="30" maxlength="30"></td> ';
   print '</tr>';
}
stanley
Forum Newbie
Posts: 6
Joined: Mon Feb 14, 2005 8:37 am

Post by stanley »

I need some more help form you, how do I read the data using a post command? normally I use it this way:

$name = $_POST['name'];

and later I will update $name into mysql database. Since the above input is using array, how do I collect all the data in from the array so I can update it into mysql database.
Post Reply