Jump to content

[SOLVED] PHP/MySQL Multi Update


SkyRanger

Recommended Posts

I am having a problem with multi update.

 

Form Page

<input type="hidden" name="id" value="<?php echo $row['id]'}; ?>">
<input type="text" name="mid" value="<?php echo $row['mid]'}; ?>">
Menu Name


So it shows:

MID       Menu Name
1           Firstname
2           Secondname
3           Thirdname

 

On the update page I have:

 

$id = $_POST[id];
$mid = $_POST[mid];

mysql_query("update menu set mid='$mid' where id='$id");

echo "--- Update Complete ---<br><a href=index.php>Home</a>";

 

So What I am trying to do is change firstname 2  secondname 3 and thirdname 1

 

 

Link to comment
Share on other sites

Sorry, Yeah

 

 

<form method="post" action="updatemenu.php">
<?php db connect info 

while($rows=mysql_fetch_array($result)){
{
?>
<tr><td>
<input type="hidden" name="id" value="<?php echo $row['id]'; ?>">
<input type="text" name="mid" value="<?php echo $row['mid]'; ?>"> <---This is where I would be changing the order
...reset of form
</td>
</tr>
<?php } ?>

 

I would be changing the mid for example if:

 

id = 1 mid = 1  change mid to 2

id = 2 mid = 2  change mid to 3

id = 3 mid = 3  change mid to 1

 

Link to comment
Share on other sites

Is it possible to see this in a live environment or if not you need to post more code?  I have 1 thing that I can tell will not be working...

 

1.  You do your form and loop through right here...

while($rows=mysql_fetch_array($result)){
{
?>
<tr><td>
<input type="hidden" name="id" value="<?php echo $row['id]'; ?>">
<input type="text" name="mid" value="<?php echo $row['mid]'; ?>">

 

The fault there is that you most likely have multiple hidden values all with the name of "id" correct??

AND...

You have multiple text boxes all with the name of "mid" correct?

 

So then when you open your next page and do this..

$id = $_POST[id];
$mid = $_POST[mid];

mysql_query("update menu set mid='$mid' where id='$id");

 

All that you are doing there is grabbing 1 value.  NOT all of them.  I'm not sure which ones php takes but I would assume the 1st one.

 

Modify your code on the update portion to do this...

$id = $_POST[id];
$mid = $_POST[mid];

echo "<b>ID: </b>".$id."<br />\n";
echo "<b>MID: </b>".$mid."<br />\n";

mysql_query("update menu set mid='$mid' where id='$id");

echo "--- Update Complete ---<br><a href=index.php>Home</a>";

That will show you the actual values of your item.

 

Trouble shoot around with it a little but I'm pretty certain you will have to change the names on your form to this..

<input type="hidden" name="id[]" value="<?php echo $row['id]'; ?>">
<input type="text" name="mid[]" value="<?php echo $row['mid]'; ?>">

 

Then on the "process" portion you will have loop through what will not be an array and individually work with each element.  Post your results on here.

 

Link to comment
Share on other sites

Here is the form page:

 

<form name="form1" method="post" action="finalupdatemenu.php">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td width="40" align="center"><strong>Id</strong></td>
<td width="50" align="center"><strong>MID</strong></td>
<td width="50" align="center"><strong>MSec</strong></td>
<td width="137" align="center"><strong>Title</strong></td>
<td width="217" align="center"><strong>URL</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input size="3" name="mid[]" type="text" id="mid" value="<? echo $rows['mid']; ?>"></td>
<td align="center"><? echo $rows['msect']; ?></td>
<td align="center"><? echo $rows['mtialt']; ?></td>
<td align="center"><? echo $rows['mpgim']; ?></td>
</tr>
<?php
}
?>
<tr>
  <td colspan="5" align="center"> </td>
</tr>
<tr>
<td colspan="5" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>

 

Here is the process form:

 

include "../inc/dbconnect.inc.php";

$id = $_POST[id];
$mid = $_POST[mid];


mysql_query("update menu set mid='$mid' where id='$id'");



echo "--- Update Complete ---<br><a href=index.php>Home</a>";

Link to comment
Share on other sites

Ok.  Well that is because you changed your form to have the values of "id[]" and "mid[]" which are arrays.  Before that they were single values.  Since you now have arrays you will need to loop through all values.  Try this...

 

<?php
for($i=0;$i<=count($id);$i++) 
{
    echo "<b>ID (value #".$i."): </b>".$id[i]."<br />\n";
    echo "<b>MID (value #".$i."): </b>".$mid[i]."<br />\n";
}

The above is JUST for testing to make sure you have the correct values.  I know its overkill but ALWAYS echo values to screen when testing to make sure that you are getting the expected output.  All too often I hear "I know my code is right so why isn't this working."  This is just a very fast and easy way to debug things.

 

IF you then have your correct values then you need to reflect that in your update statement...

<?php
for($i=0;$i<=count($id);$i++) 
{
   mysql_query("update menu set mid='$mid[i]' where id='$id[i]");
}?>

Try that out and let me know what happens

Link to comment
Share on other sites

As a follow up to.  Make sure that you check the exact syntax of the query.  If your id is an integer, you shouldn't need quotes around $id.  On top of that as I look at it there looks like a missing quote in there.

 

IF id (in mysql) is NOT an integer then do this..

mysql_query("update menu set mid='$mid[i]' where id='$id[i]';");

 

IF id (in mysql) IS an integer then do this..

mysql_query("update menu set mid='$mid[i]' where id=$id[i];");

Link to comment
Share on other sites

As a follow up to.  Make sure that you check the exact syntax of the query.  If your id is an integer, you shouldn't need quotes around $id.  On top of that as I look at it there looks like a missing quote in there.

 

IF id (in mysql) is NOT an integer then do this..

mysql_query("update menu set mid='$mid[i]' where id='$id[i]';");

 

IF id (in mysql) IS an integer then do this..

mysql_query("update menu set mid='$mid[i]' where id=$id[i];");

Link to comment
Share on other sites

Ok, I used the test code to see if the data was pushed out

 

 

<?php
for($i=0;$i<=count($id);$i++) 
{
    echo "<b>ID (value #".$i."): </b>".$id[i]."<br />\n";
    echo "<b>MID (value #".$i."): </b>".$mid[i]."<br />\n";
}

 

And this is all I got:

 

ID (value #0):

MID (value #0):

 

So I must be missing something:

 

I has this in my process page just for a test:

 

$id = $_POST[id];
$mid = $_POST[mid];

for($i=0;$i<=count($id);$i++) 
{
    echo "<b>ID (value #".$i."): </b>".$id[i]."<br />\n";
    echo "<b>MID (value #".$i."): </b>".$mid[i]."<br />\n";
}

Link to comment
Share on other sites

Ok, got the arrays to show:

 

Array
(
    [id] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 22
            [3] => 23
        )

    [mid] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

 

So what i need to figure out now is how to update the rows with the array data

 

so if

id = 7 update mid with 1

id = 8 update mid with 2

id = 22 update mid with 3

id = 23 update mid with 4

 

so i would need to update menu set mid = ? where id = ?;

 

No sure how I would do that.

Link to comment
Share on other sites

Rewrote the whole script and figured it out:

 

 

Form Page

<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>

<td align="center"><input size="3" name="mid_<? echo $rows['id']; ?>" type="text" id="mid_<? echo $row['id']; ?>" value="<? echo $rows['mid']; ?>"></td>

 

Submit Page

if($_POST['Submit']){ // If receive Submit button variable.

// Select all data records in table "name_list" and put them into $result.
$result=mysql_query("select id from menu order by id asc");

// Fetch record rows in $result by while loop and put them into $row.
while($row=mysql_fetch_assoc($result)){
// Get the posted value "name_ID value" from form.php. This variable change it's value by while loop.
$mid=$_POST["mid_".$row[id]];

// Update field "name", matching with "id" value by while loop.
mysql_query("update menu set mid='$mid' where id='$row[id]'");
}
}
echo "--- Update Complete ---<br><a href=index.php>Home</a>";

Link to comment
Share on other sites

I have tried a number of ways to get this to work, I am a bit farther ahead than what I was but I am still running into problems.

 

Can anybody please give me a hand.

 

This is what I am trying to do

 

form name="form1" method="post" action="finalupdatemenu.php">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td width="40" align="center"><strong>Id</strong></td>
<td width="50" align="center"><strong>MID</strong></td>
<td width="50" align="center"><strong>MSec</strong></td>
<td width="137" align="center"><strong>Title</strong></td>
<td width="217" align="center"><strong>URL</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? echo $rows['id']; ?></td>
<input type="hidden" name="id[]" value="<?php echo $rows['id']; ?>">
<td align="center"><input size="3" name="mid[]" type="text" id="mid" value="<? echo $rows['mid']; ?>"></td>
<td align="center"><? echo $rows['msect']; ?></td>
<td align="center"><? echo $rows['mtialt']; ?></td>
<td align="center"><? echo $rows['mpgim']; ?></td>
</tr>
<?php
}
?>
<tr>
  <td colspan="5" align="center"> </td>
</tr>
<tr>
<td colspan="5" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>

 

So it would be:

 

ID: 1  MID:5

ID: 2  MID:4

ID: 3  MID:2

ID: 4  MID:1

 

And I need it to go

 

ID: 1 MID:1

ID: 2 MID:2

etc

 

So when I click submit it goes to another page: finalupdatemenu.php and does the update to mysql

 

I can get the arrays to show but not sure how to get it to update the rows I need it to.

 

 

 

 

Link to comment
Share on other sites

After rewriting the code once again, I finally figured it out.

 

          if (isset($_POST[submit])) {
   
   foreach ($_POST as $key) {
          $id=$key[id];
          $mid=$key[mid];
include "../inc/dbconnect.inc.php";
         mysql_query("UPDATE menu set mid = '$mid' where id = '$id'") or die(mysql_error());;

   }
}


...........
connect code for DB + rest of form.............
...........




while($rows=mysql_fetch_array($result)){
$id = $rows['id'];
$mid = $rows['mid'];
?>
<tr>
<td align="center"><? echo $rows['id']; ?></td>
<?php
print	"<input type='hidden' name =".$id."[id] value =".$id.">";
?>
<td align="center"><?php
echo "<input type='text' size='2' name=\"".$id."[mid]\" value=".$mid.">";
?>
</td>
<td align="center"><? echo $rows['msect']; ?></td>
<td align="center"><? echo $rows['mtialt']; ?></td>
<td align="center"><? echo $rows['mpgim']; ?></td>
</tr>
<?php
}

.......+ rest of form

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.