Author Topic: [SOLVED] updating mutiple rows using php  (Read 367 times)

0 Members and 1 Guest are viewing this topic.

Offline AdRockTopic starter

  • Devotee
  • Posts: 861
    • View Profile
[SOLVED] updating mutiple rows using php
« on: October 01, 2008, 05:38:48 PM »
I have a form which lists a list of records from my database.

I would like to check the boxes of any records I want to update becuase the field i want to update all has the same value

I can't get them to update and need some help

here is my form
Code: [Select]
echo '<form name="archive-form" action="'.$_SERVER['REQUEST_URI'].'" method="post">';
// Iterate through the results

$i = 0;

while ($row = $result->fetch()) {
         echo "<p><input type='checkbox' name='id[$i]' value='{$row['id']}' />{$row['title']}</p>"
         $i++;
}
echo "<input type='submit' value='submit' name='archive' />";

echo "</form>";

and the function that is called when the form is submitted
Code: [Select]
if ( isset ($_POST['archive']) ) {

     $id = $_POST['id'][$i];
     $result = content_archive('articles', 'archived', 'title', $id);

and the function that should so the update
Code: [Select]
function content_archive($table, $field1, $field2, $content_title) {

global $host,$dbUser,$dbPass,$dbName;

require_once("../php/database/connection.php");
require_once("../php/database/MySQL.php");

// Connect to the database and grab the email
$db = & new MySQL($host,$dbUser,$dbPass,$dbName);

// find out how many records there are to update
$size = count($_POST['id']);

// start a loop in order to update each record
$i = 0;
while ($i < $size) {

$sql="UPDATE $table SET $field1='y' WHERE $field2=$content_title' LIMIT 1";

//query to enter form data into database    
$result = $db->query($sql);

$i++;
}

if(!$result) {
return 'There has been an error adding to the database.  Please consult Adam and advise of the problem';
}
else {
return 'Correct';
}
}

I am not sure bu ti don't know if i am passing an array of ids to the fnction so it can do the update on each record
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

Offline kristolklp

  • Irregular
  • Posts: 3
    • View Profile
Re: updating mutiple rows using php
« Reply #1 on: October 01, 2008, 06:06:14 PM »
I do not think you are passing the array to the function.  It looks like you are passing only 1 element.

Try passing this instead: 

Code: [Select]
$id = $_POST['id'];
$result = content_archive('articles', 'archived', 'title', $id);

Then in your function, try changing this line to reference your passed array:
Code: [Select]
// find out how many records there are to update
$size = count($content_title);

Offline AdRockTopic starter

  • Devotee
  • Posts: 861
    • View Profile
Re: [SOLVED] updating mutiple rows using php
« Reply #2 on: October 02, 2008, 02:55:14 AM »
I fixed it but using a foreach loop on the $_POST
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still