[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Update All Records

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
amazonebayhalfprice
Asked:
amazonebayhalfprice
  • 11
  • 8
1 Solution
 
ldbkuttyCommented:
<?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
 
ldbkuttyCommented:
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
 
amazonebayhalfpriceAuthor Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
amazonebayhalfpriceAuthor Commented:


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
 
amazonebayhalfpriceAuthor Commented:
Some how the code didn't any update
0
 
ldbkuttyCommented:
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
 
amazonebayhalfpriceAuthor Commented:

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
 
amazonebayhalfpriceAuthor Commented:
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
 
amazonebayhalfpriceAuthor Commented:
So like do you have any idea what wrong with the code
0
 
ldbkuttyCommented:
please hold on, testing the script.
0
 
ldbkuttyCommented:
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
 
ldbkuttyCommented:
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
 
amazonebayhalfpriceAuthor Commented:
please wait
0
 
amazonebayhalfpriceAuthor Commented:

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
 
ldbkuttyCommented:
so it works!, Replace the echo with mysql_query()
0
 
amazonebayhalfpriceAuthor Commented:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in
0
 
amazonebayhalfpriceAuthor Commented:
in this line

             mysql_query()"UPDATE table_name SET columna = '" . $_POST["columna"]["$i"] . "' WHERE id = '" . $_POST["ids"]["$i"] ."' <br><br>";
0
 
ldbkuttyCommented:
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
 
amazonebayhalfpriceAuthor Commented:
thank you

I got it.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 11
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now