/    Sign up×
Community /Pin to ProfileBookmark

Updating Multiple Fields in MySQL with PHP

I have a table in MySQL that I am able to edit through PHP but only one row at a time. My client is wanting to be able to edit all the data at one time. There are not many rows but each row has multiple fields. So I need to be able to edit all the rows at one time. Displaying all data in a editable format is no problem, but when it generates I already have the name listed:

[code=php]
while ($row = mysql_fetch_array($result)) {
echo “<tr align=’center’>”;
echo “<td>”, $row[‘name’], “</td>”;
echo “<td><input type=’text’ value='”, $row[‘d_e’], “‘ size=’10’ name=’d’ /></td>”;
echo “<td><input type=’text’ value='”, $row[‘c’], “‘ size=’10’ name=’c’ /></td>”;
echo “<td><input type=’text’ value='”, $row[‘b’], “‘size=’10’ name=’b’ /></td>”;
echo “<td><input type=’text’ value='”, $row[‘a’], “‘ size=’10’ name=’a’ /></td>”;
echo “</tr>”;
}
[/code]

Then the update looks like this:

[code=php]
$id = $_GET[“id”];
$a = $_POST[“a”];
$b = $_POST[“b”];
$c = $_POST[“c”];
$d = $_POST[“d”];

$query = “UPDATE rates SET a = ‘{$a}’, b = ‘{$b}, c = ‘{$c}’, d_e = ‘{$d}’ WHERE id = ‘{$id}’ “;
[/code]

The problem should be obvious. Each different row will have different data but the name will be the same so it would confuse the database. Any help on what I can do differently.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@ellisglAug 10.2007 — $query = "UPDATE rates SET a = '$a', b = '$b', c = '$c', d_e = '$d' WHERE id = '$id' ";
Copy linkTweet thisAlerts:
@monarch_684authorAug 10.2007 — That doesn't help me with my current problem
Copy linkTweet thisAlerts:
@ellisglAug 10.2007 — You were missing a single quote and the {} is only need if you are taking a value from an array - $a['dog'] with in a double quoted statement and you do not want to break out of it like ".$a['dog']."

As for the question - you can make an array of the ids with a hidden field. then each field you can dynamically name="c<?php echo $row['id'];?>";

so you would loop thru the id array and with each loop just do use $_POST['c'.$val];
Copy linkTweet thisAlerts:
@monarch_684authorAug 10.2007 — Ok so this is what I have come up with with your help.

[code=php]
while ($row = mysql_fetch_array($result)) {
echo "<tr align='center'>";
echo "<td>", $row['name'], "</td>";
echo "<td><input type='text' value='", $row['d_e'], "' size='10' name='d", $row['id'], "' /></td>";
echo "<td><input type='text' value='", $row['c'], "' size='10' name='c", $row['id'], "' /></td>";
echo "<td><input type='text' value='", $row['b'], "'size='10' name='b", $row['id'], "' /></td>";
echo "<td><input type='text' value='", $row['a'], "' size='10' name='a", $row['id'], "' /></td>";
echo "</tr>";
}

[/code]


Then the update:

[code=php]
$id = $_GET["id"];
$a = $_POST["a".$val];
$b = $_POST["b".$val];
$c = $_POST["c".$val];
$d = $_POST["d".$val];


$query = "UPDATE rates SET
a = '$a',
b = '$b',
c = '$c',
d_e = '$d'
WHERE id = '$id' ";

[/code]


Look right?
Copy linkTweet thisAlerts:
@ellisglAug 10.2007 — Something more along the lines of this:
[code=php]
<?php
$cnta = mysql_num_rows($result);
$cntb = 1;
while($row = mysql_fetch_array($result))
{
echo "<tr align='center'>",
"<td>", $row['name'], "</td>".
"<td><input type='text' value='", $row['d_e'], "' size='10' name='d", $row['id'], "' /></td>",
"<td><input type='text' value='", $row['c'], "' size='10' name='c", $row['id'], "' /></td>",
"<td><input type='text' value='", $row['b'], "'size='10' name='b", $row['id'], "' /></td>",
"<td><input type='text' value='", $row['a'], "' size='10' name='a", $row['id'], "' /></td>",
"</tr><input type ='hidden' value='", $row['id'],"' name='ids[]'/>n";
}
?>

<?php
foreach($_POST['ids'] as $val)
{
$id = $_GET["id"];
$a = $_POST["a".$val];
$b = $_POST["b".$val];
$c = $_POST["c".$val];
$d = $_POST["d".$val];

$query = "UPDATE rates SET
a = '$a',
b = '$b',
c = '$c',
d_e = '$d'
WHERE id = '$val' ";
}
?>
[/code]
Copy linkTweet thisAlerts:
@monarch_684authorAug 10.2007 — Ok now it is not updating at all. Here is what I got.

This is the table layout with the edit link:
[code=php]
$connection = mysql_connect("localhost", "username", "password");
@mysql_select_db("database", $connection) or die( "Unable to select database" . mysql_error());

$query = "SELECT * FROM rates ORDER BY id";
$result = mysql_query($query)
or die ("Couldn't query data" . mysql_error());

echo "<div id='table2'>";
echo "<table border='1' cellpadding='2'>";
echo "<tr align='center'>";
echo "<th rowspan='3' valign='bottom'>ID</th>";
echo "<th rowspan='3' valign='bottom'>Open End</th>";
echo "<th rowspan='3' valign='bottom'>Closed End</th>";
echo "<th rowspan='2' valign='bottom'>Collateral (Consumer)</th>";
echo "<th>E &amp; D</th>";
echo "<th>C</th>";
echo "<th>B</th>";
echo "<th>A &amp; A+</th>";
echo "<th rowspan='3' valign='bottom'>Manage</th>";
echo "</tr>";
echo "<tr>";
echo "<th>(0-619)</th>";
echo "<th>(620-649)</th>";
echo "<th>(650-679)</th>";
echo "<th>680-Up)</th>";
echo "</tr>";
echo "<tr>";
echo "<th>Collateral (Real Estate)</th>";
echo "<th>(0-599)</th>";
echo "<th>(600-639)</th>";
echo "<th>(640-679)</th>";
echo "<th>(680-Up)</th>";
echo "</tr>";


while ($row = mysql_fetch_array($result)) {
echo "<tr align='center'>";
echo "<td>", $row['id'], "</td>";
echo "<td>", $row['open_end'], "</td>";
echo "<td>", $row['closed_end'], "</td>";
echo "<td>", $row['name'], "</td>";
echo "<td>", $row['d_e'], "%</td>";
echo "<td>", $row['c'], "%</td>";
echo "<td>", $row['b'], "%</td>";
echo "<td>", $row['a'], "%</td>";
echo "<td><a href='edit.php'>edit</a></td>";
echo "</tr>";
}

echo "</table>";
echo "</div>";
[/code]


This is the edit.php:
[code=php]
$id = $_GET["id"];

$connection = mysql_connect("localhost", "username", "password");
@mysql_select_db("database", $connection) or die( "Unable to select database" . mysql_error());

$query = "SELECT * FROM rates";
$result = mysql_query($query)
or die ("Couldn't query data" . mysql_error());

echo "<form method='post' action='editsubmit.php'>";
echo "<table>";

$cnta = mysql_num_rows($result);
$cntb = 1;
while ($row = mysql_fetch_array($result)) {
echo "<tr align='center'>";
echo "<td>", $row['name'], "</td>";
echo "<td><input type='text' value='", $row['d_e'], "' size='10' name='d", $row['id'], "' /></td>";
echo "<td><input type='text' value='", $row['c'], "' size='10' name='c", $row['id'], "' /></td>";
echo "<td><input type='text' value='", $row['b'], "'size='10' name='b", $row['id'], "' /></td>";
echo "<td><input type='text' value='", $row['a'], "' size='10' name='a", $row['id'], "' /></td>";
echo "</tr><input type ='hidden' value='", $row['id'],"' name='ids[]'/>n";
echo "</tr>";
}

echo "</table>";
echo "</div>";




mysql_close();
?>
[/code]


Last it submits the data:

[code=php]
foreach($_POST['ids'] as $val)
{
$id = $_GET["id"];
$a = $_POST["a".$val];
$b = $_POST["b".$val];
$c = $_POST["c".$val];
$d = $_POST["d".$val];

$query = "UPDATE rates SET
a = '$a',
b = '$b',
c = '$c',
d_e = '$d'
WHERE id = '$val' ";
}
$connection = mysql_connect("localhost", "username", "password");
@mysql_select_db("database", $connection) or die( "Unable to select database1" . mysql_error());

$result = mysql_query($query)
or die ("Couldn't query data" . mysql_error());

mysql_close();
[/code]
Copy linkTweet thisAlerts:
@ellisglAug 10.2007 — I'm going to make a working sample..
Copy linkTweet thisAlerts:
@monarch_684authorAug 10.2007 — You can see what I have done.

http://www.jamiemcraney.cihost.com/secure/admin.php

User: demo

Pass: demo
Copy linkTweet thisAlerts:
@ellisglAug 10.2007 — Ok - He's my working example.
[code=php]
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'testdb';
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die('Could not connect to the DB server');

mysql_select_db($db, $link) or die('Could not select the DB');

if($_POST)
{
foreach($_POST['ids'] as $val)
{
$sql = 'UPDATE tblcategories SET Name = ''.$_POST['a'.$val].'', pid = ''.$_POST['b'.$val].'' WHERE id = ''.$val.''';
mysql_query($sql) or die($sql.': '.mysql_error());
}
echo 'Records have been updated.';
}
else
{
$sql = 'SELECT * FROM tblcategories';
$qry = mysql_query($sql) or die($sql.' '.mysql_error());
$html = '<form action="" method="post"><table>';

while($row = mysql_fetch_assoc($qry))
{
$html .= '<tr>'.
'<td>'.$row['id'].'</td>'.
'<td><input type="text" name="a'.$row['id'].'" value="'.$row['Name'].'" /></td>'.
'<td><input type="text" name="b'.$row['id'].'" value="'.$row['pid'].'" /></td>'.
'</tr><input type="hidden" name="ids[]" value="'.$row['id'].'" />';
}
$html .= '<tr><td colspan="3"><input type="submit" name="submit" value="Submit"></td></tr></table></form>';
echo $html;
}
?>
[/code]


SQL (just for S and G's)

<i>
</i>/*Table structure for table <span><code>tblcategories</code></span> */

CREATE TABLE <span><code>tblcategories</code></span> (
<span><code>id</code></span> int(11) NOT NULL,
<span><code>Name</code></span> varchar(100) collate latin1_general_ci NOT NULL,
<span><code>pid</code></span> int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

/*Data for the table <span><code>tblcategories</code></span> */

insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (1,'Hardware',0);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (2,'Software',0);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (3,'Movies',0);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (4,'Mouse',1);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (4,'Mouse',1);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (5,'Keyboard',1);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (5,'Keyboard',1);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (6,'Monitor',1);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (7,'Harddisk',1);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (8,'CD ROM',1);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (9,'DVD',1);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (9,'DVD',1);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (10,'Desktop',2);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (11,'Web Application',2);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (12,'Mobile Application',2);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (13,'Hindi',3);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (14,'English',3);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (15,'Punjabi',3);
insert into <span><code>tblcategories</code></span>(<span><code>id</code></span>,<span><code>Name</code></span>,<span><code>pid</code></span>) values (16,'French',3);
×

Success!

Help @monarch_684 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...