?
Solved

Update All Records

Posted on 2005-03-13
19
Medium Priority
?
230 Views
Last Modified: 2013-12-12
Please help me to write the code.
I had the design but still having a problem with writing the code.

I'm trying to update all of the records which is pull from the database


//*****Pullrecords.php*****
//Pull the data from the database
$res = mysql_query("select * from $table_name limit $queryStart,10") or die("Sql error : " . mysql_error());
while($row = mysql_fetch_array($res)) { // Get records

//print out the result & prepair for update

echo "<form method=\"POST\" action=\"updateall.php\">\n";

echo $row["id"];
echo "<input value=\"";
echo $row["columna"];
echo "\" name=\"columna\" size=\"60%\" ";

echo "<input type=\"submit\" value=\"Submit\" name=\"B1\"></p>";
}



//***updateall.php****
//do some update here

some code here that can understand how many code that we need to update

like something update columna where id =id
but we are not just update one 1id what I need is to update multiple records all at one








0
Comment
Question by:amazonebayhalfprice
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
19 Comments
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13531940
<?php

$res = mysql_query("select * from $table_name limit $queryStart, 10") or die("Sql error : " . mysql_error());

?>

<form name="update_form" action="updateall.php" method="post">

<?php

while($row = mysql_fetch_array($res)) { // Get records
  echo "<input type='hidden' name='ids[]' value='" . $row["id"] . "'/>";
  echo "<input name='columna[]' size='60%' value='" . $row["columna"] . "'/><br>";
}

?>

<input type="submit" name="update_record" value="Update All"/>
</form>

And in your updateall.php :
=================

<?php

if( isset($_POST['update_record']) )
{
    if( array_key_exists("ids", $_POST) && is_array($_POST["ids"]) && array_key_exists("columna", $_POST) && is_array($_POST["columna"]) )
    {
         for( $i = 0; $i < count( $_POST["ids"] ); $i++ )
         {
             mysql_query("UPDATE table_name SET columna = '" . $columna[$i] . "' WHERE id = '" . $ids[$i] ."'") or die("Update Sql error: " . mysql_error());
         }
    }
}

?><?php

$res = mysql_query("select * from $table_name limit $queryStart,10") or die("Sql error : " . mysql_error());

?>

<form name="update_form" action="updateall.php" method="post">

<?php

while($row = mysql_fetch_array($res)) { // Get records
  echo "<input type='hidden' name='ids[]' value='" . $row["id"] . "'/>";
  echo "<input name='columna[]' size='60%' value='" . $row["columna"] . "'/>";
}

?>

<input type="submit" name="update_record" value="Update All"/>
</form>

And in your updateall.php :
=================

<?php

if( isset($_POST['update_record']) )
{
    if( array_key_exists("ids", $_POST) && is_array($_POST["ids"]) && array_key_exists("columna", $_POST) && is_array($_POST["columna"]) )
    {
         for( $i = 0; $i < count( $_POST["ids"] ); $i++ )
         {
             mysql_query("UPDATE table_name SET columna = '" . $columna[$i] . "' WHERE id = '" . $ids[$i] ."'") or die("Update Sql error: " . mysql_error());
         }
    }
}

?>
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13531945
Oops, I copy pasted twice !

Take only from the starting of my comment till this line :

?><?php

// Ofcourse remove the <?php in that line.
0
 

Author Comment

by:amazonebayhalfprice
ID: 13535325
I put everything together but I didn't see any update yet!

I think we need to do some counting on the first page b



//*************First Page*******

<?php

$res = mysql_query("select * from $table_name limit $queryStart, 10") or die("Sql error : " . mysql_error());

?>

<form name="update_form" action="updateall.php" method="post">

<?php

while($row = mysql_fetch_array($res)) { // Get records

// I think we need to do some counting on the two lines below like
  echo "<input type='hidden' name='ids[]' value='" . $row["id"] . "'/>";
  echo "<input name='columna[]' size='60%' value='" . $row["columna"] . "'/><br>";
}

?>

<input type="submit" name="update_record" value="Update All"/>
</form>




//******And in your updateall.php *****

<?php

if( isset($_POST['update_record']) )
{
    if( array_key_exists("ids", $_POST) && is_array($_POST["ids"]) && array_key_exists("columna", $_POST) && is_array($_POST["columna"]) )
    {
         for( $i = 0; $i < count( $_POST["ids"] ); $i++ )
         {
             mysql_query("UPDATE table_name SET columna = '" . $columna[$i] . "' WHERE id = '" . $ids[$i] ."'") or die("Update Sql error: " . mysql_error());
         }
    }
}

?>
0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 

Author Comment

by:amazonebayhalfprice
ID: 13539052


I think we had some error on the first page

//*************First Page*******

<?php

$res = mysql_query("select * from $table_name limit $queryStart, 10") or die("Sql error : " . mysql_error());

?>

<form name="update_form" action="updateall.php" method="post">

<?php

while($row = mysql_fetch_array($res)) { // Get records

// I think we need to do some counting on the two lines below like
  echo "<input type='hidden' name='ids[]' value='" . $row["id"] . "'/>";
  echo "<input name='columna[]' size='60%' value='" . $row["columna"] . "'/><br>";
}

?>

<input type="submit" name="update_record" value="Update All"/>
</form>




//******2nd page updateall.php *****

<?php

if( isset($_POST['update_record']) )
{
    if( array_key_exists("ids", $_POST) && is_array($_POST["ids"]) && array_key_exists("columna", $_POST) && is_array($_POST["columna"]) )
    {
         for( $i = 0; $i < count( $_POST["ids"] ); $i++ )
         {
             mysql_query("UPDATE table_name SET columna = '" . $columna[$i] . "' WHERE id = '" . $ids[$i] ."'") or die("Update Sql error: " . mysql_error());
         }
    }
}

?>

0
 

Author Comment

by:amazonebayhalfprice
ID: 13539056
Some how the code didn't any update
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13539067
Can you try this :

<?php

if( isset($_POST['update_record']) )
{
    if( array_key_exists("ids", $_POST) && is_array($_POST["ids"]) && array_key_exists("columna", $_POST) && is_array($_POST["columna"]) )
    {
         for( $i = 0; $i < count( $_POST["ids"] ); $i++ )
         {
               echo $i . " record id is: " . $_POST[$ids["$i"]] . " columna value is : " . $_POST[$columna["$i"]] . "<br>";
         
               mysql_query("UPDATE table_name SET columna = '" . $_POST[$columna["$i"]] . "' WHERE id = '" . $_POST[$ids["$i"]] ."'") or die("Update Sql error: " . mysql_error());
         }
    }
}

?>

If that displays anything in the page, put print_r($_POST); in your updateall.php and see if the values are posted.

// There isn't any need to change in the first page, but only in the submitted page. //
0
 

Author Comment

by:amazonebayhalfprice
ID: 13539363

I had a problem on the second page
Notice: Undefined variable: ids in
updateall.php on line
 
echo $i . " record id is: " . $_POST[$ids["$i"]] . " columna value is : " . $_POST[$columna["$i"]] . "<br>";



Notice: Undefined index: in
updateall.php on line

 echo $i . " record id is: " . $_POST[$ids["$i"]] . " columna value is : " . $_POST[$columna["$i"]] . "<br>";

seem like the first page is fine

this is the out for the update.php
Array ( [ids] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 ) [columna] => Array ( [0] => cat 1 [1] => cat 2 [2] => cat 3 [3] => cat 4 [4] => cat for 5 [5] => cat for 6 [6] => cat for 7 [7] => cat for 8 [8] => cat for 9 [9] => cat for 10 ) [update_record] => Update All )


and the warning message above
0
 

Author Comment

by:amazonebayhalfprice
ID: 13539460
I also see a lot of
Notice: Undefined index: in updateall.php on line 25
2 record id is: columna value is :

Notice: Undefined variable: columna in line 27

0
 

Author Comment

by:amazonebayhalfprice
ID: 13539694
So like do you have any idea what wrong with the code
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13539713
please hold on, testing the script.
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13539781
Alrite, this works fine :

The first code is working fine for me, replace the mysql_query with print and see what you get :

<?php

if( isset($_POST['update_record']) )
{
    if( array_key_exists("ids", $_POST) && is_array($_POST["ids"]) && array_key_exists("columna", $_POST) && is_array($_POST["columna"]) )
    {
         for( $i = 0; $i < count( $_POST["ids"] ); $i++ )
         {
             echo "UPDATE table_name SET columna = '" . $_POST["columna"]["$i"] . "' WHERE id = '" . $_POST["ids"]["$i"] ."' <br><br>";
         }
    }
}

?>

// Replace the echo with mysql_query after testing. //
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13539788
Neglect this line :

>>>> The first code is working fine for me, replace the mysql_query with print and see what you get : <<<<

I forgot to remove it.
0
 

Author Comment

by:amazonebayhalfprice
ID: 13539838
please wait
0
 

Author Comment

by:amazonebayhalfprice
ID: 13539860

okay this is what i got now

UPDATE table_name SET columna = 'cat 1' WHERE id = '1'

UPDATE table_name SET columna = 'cat 2' WHERE id = '2'

UPDATE table_name SET columna = 'cat 3' WHERE id = '3'

UPDATE table_name SET columna = 'cat 4' WHERE id = '4'

UPDATE table_name SET columna = 'cat for 5' WHERE id = '5'

UPDATE table_name SET columna = 'cat for 6' WHERE id = '6'

UPDATE table_name SET columna = 'cat for 7' WHERE id = '7'

UPDATE table_name SET columna = 'cat for 8' WHERE id = '8'

UPDATE table_name SET columna = 'cat for 9' WHERE id = '9'

UPDATE table_name SET columna = 'cat for 10' WHERE id = '10'
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13539868
so it works!, Replace the echo with mysql_query()
0
 

Author Comment

by:amazonebayhalfprice
ID: 13539883
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in
0
 

Author Comment

by:amazonebayhalfprice
ID: 13539920
in this line

             mysql_query()"UPDATE table_name SET columna = '" . $_POST["columna"]["$i"] . "' WHERE id = '" . $_POST["ids"]["$i"] ."' <br><br>";
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 540 total points
ID: 13539930
Like this :

   mysql_query("UPDATE table_name SET columna = '" . $_POST["columna"]["$i"] . "' WHERE id = '" . $_POST["ids"]["$i"] ."'") or die("Update query error: " . mysql_error());
0
 

Author Comment

by:amazonebayhalfprice
ID: 13539945
thank you

I got it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question