Jump to content

[SOLVED] updating mutiple rows using php


AdRock

Recommended Posts

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

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

if ( isset ($_POST['archive']) ) {

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

 

and the function that should so the update

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

Link to comment
https://forums.phpfreaks.com/topic/126677-solved-updating-mutiple-rows-using-php/
Share on other sites

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: 

 

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

 

Then in your function, try changing this line to reference your passed array:

// find out how many records there are to update
$size = count($content_title);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.