Jump to content

[SOLVED] updating multiple rows in a table in mysql


gevo12321

Recommended Posts

ok so im making this form that updates multiple rows in the mysql database but the thing is that the number of rows is a variable so once it may be one row once it may be 31 rows. is there anyway i can do this using a while loop? or even is there a way to do this period?

Link to comment
Share on other sites

well thats the thing i dont have a code, not yet anyway, because i dont know how i would do it

 

i was thinking of

mysql_query("UPDATE layout SET  name='$name', source='$source'
WHERE id='$id'");

and just putting it in a while loop but the $name, $source and $id change for each row and i just dont know what i could do to make it work. because it is not always a set number of rows.

 

i really appreciate ur help

thx

Link to comment
Share on other sites

Where is the data going to be coming from? A form? When/why will the number of rows change?

Without anymore details, we will largely be guessing, but assuming you're using a form, and the data is in arrays, a while or foreach loop will do fine:

 

<?php
foreach($ids as $value){
$name = $names[$value];
$source = $sources[$value];
mysql_query("UPDATE `layout` SET `name`='$name', `source`='$source' WHERE `id`='$id'") or die(mysql_error());
}
?>

 

So this is assuming you have 3 arrays(ids, names, sources) where the id being updated is the key of the names and sources array. But it all depends on how you want to do this.

Link to comment
Share on other sites

ok

 

so it is a form and the form has the following code:

<?php
require_once('../connect.php');

$query = mysql_query("SELECT * FROM layout") or die(mysql_error());

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<title>E Layout</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
</head>
<body>
<a href=\"eenglish.htm\">English<a> > <a href=\"eeditlayout.php\">E Layout<a>
<hr>
<form action=\"esubmiteditlayout.php\" method=\"post\">
<table width=\"100%\"  border=\"0\">";
while($row=mysql_fetch_object($query))
{
$id=$row->id;
$name=$row->name;
$source=$row->source;
echo "
  <tr>
    <td valign=\"top\" width=\"2%\"><input type=\"checkbox\" name=\"checkbox";
echo $id;
echo "\"></td>
    <td valign=\"top\" width=\"5%\">Button ";
echo $id; 
echo ":</td>
    <td valign=\"top\" width=\"10%\"><input type=\"text\" name=\"button";
echo $id;
echo "\" value=\"";
echo $name;
echo "\"/></td>
    <td valign=\"top\" width=\"8%\">Button ";
echo $id;
echo " Source:</td>
    <td valign=\"top\" align=\"left\"><input type=\"text\" name=\"button";
echo $id;
echo "src\" value=\"";
echo $source;
echo "\"/></td>
  </tr>";
}
echo "
</table>
<input type=\"submit\" name=\"edit\" value=\"Edit\">
<input type=\"submit\" name=\"insert\" value=\"Insert\">
<input type=\"submit\" name=\"del\" value=\"Delete\" onclick=\"return deldel()\">
<script language=\"javascript\">
function deldel()
{
   var agree=confirm(\"Are you sure you want to delete the checked buttons from the Side Bar?\");
   if(agree)
      return true;
   else
      return false;
}
</script>
</form>
</body>
</html>";

mysql_close($link);
?>

the number of rows will change depending on weather i want to add new rows or delete some.

 

i probably do need to use arrays but I'm completely new to this and I've tried reading about arrays but i haven't a single clue as to what they do.

 

I'm completely dazed and confused ;D

 

plz help me

Link to comment
Share on other sites

This is what I would do...

I would update say an name by ID; so the first thing

you would do is check your database and display

a form with all names and.

 

check_name.php

<?php
include("connect.php");
$sql = mysql_query("SELECT * 
FROM ` account`");
while($data = mysql_fetch_array($sql))
{
    echo('<form method="post" action="edit.php">');
    echo('<input type="hidden" name="id[]">');
    echo('<input type="text" name="name">');
    echo('<input type="submit" value="submit">
    echo('</form>');
}
?>

 

now then, time to update every change you have made

 

edit.php (exactly how Thorpe stated)

<?php
$List = implode(",", $_POST['id']);
mysql_query("UPDATE `accounts` 
SET `name`='".$_POST['name']."' 
WHERE `id` IN ($List);
?>

Now

Link to comment
Share on other sites

ok. then move the submit button...

<?php
include("connect.php");
$sql = mysql_query("SELECT * 
FROM ` account`");

echo('<form method="post" action="edit.php">');
while($data = mysql_fetch_array($sql))
{
    echo('<input type="hidden" name="id[]">');
    echo('<input type="text" name="name">');
}
echo('<input type="submit" value="submit">
echo('</form>');
?>[/code'];


Tudh done.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.