Update DB Code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Update DB Code

Post by facets »

Hi All,

I'm having some trouble calling this function to update the DB.
Can anyone give me any pointers? All it seems to be doing is resetting / reloading the page and giving errors :

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

tia, Will

Code: Select all

function updateSummary() {

if(empty($summaryId)) error_message('Empty Summary!');

$field_str .= " paperCategoryId = '$paperCategoryId', ";
$field_str .= " colloPaperName = '$colloPaperName', ";
$field_str .= " manufacturerName = '$manufacturerName', ";
$field_str .= " cpl = '$cpl', ";
$field_str .= " stockId = '$stockId', ";
$field_str .= " adhesiveId = '$adhesiveId', ";
$field_str .= " linerId = '$linerId', ";
  
$query = "UPDATE ausapapersummary SET $field_str WHERE summaryId = '$summaryId'";

$result = mysql_query($query);
  if(!$result) error_message(sql_error());

$num_rows = mysql_affected_rows($link_id);
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Code: Select all

$field_str .= " linerId = '$linerId', ";
Remove the comma from that line and give it a try.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your code doesn't set or bring in $summaryId either.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

still no joy there.
It appears that the data is being passed via $_POST as I can see the changes using the following code.

Code: Select all

echo '<pre>';
var_dump($_GET);
var_dump($_POST);
echo '</pre>';
But the change don't update in the DB. i'm using submit with the following code.

Code: Select all

switch($action) {
case "updateSummary": updateSummary(); break;
    default: editSASummary(); break;
   }

echo "<input type=\"submit\" name=\"updateSummary\" class=\"btn\" value=\"Edit Summary\">";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the fact remains, your function doesn't recieve the variables you are using.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

i think the submit button is working but I don;t believe the updateSummary function is being exectuted.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

sorry my mistake.. it should read more like this..

Code: Select all

$summaryId = isset($_GET['summaryId']) ? $_GET['summaryId'] : '';

$summaryId = $_POST['summaryId'];
$paperCategoryId = $_POST['paperCategoryId'];
$colloPaperName = $_POST['colloPaperName'];
$manufacturerName = $_POST['manufacturerName'];
$cpl = $_POST['cpl'];
$stockId = $_POST['stockId'];
$adhesiveId = $_POST['adhesiveId'];
$linerId = $_POST['linerId'];

if(empty($summaryId)) error_message('Empty Summary!');

$field_str .= " paperCategoryId = '$paperCategoryId', ";
$field_str .= " colloPaperName = '$colloPaperName', ";
$field_str .= " manufacturerName = '$manufacturerName', ";
$field_str .= " cpl = '$cpl', ";
$field_str .= " stockId = '$stockId', ";
$field_str .= " adhesiveId = '$adhesiveId', ";
$field_str .= " linerId = '$linerId', ";

$query = "UPDATE ausapapersummary SET $field_str WHERE summaryId = '$summaryId'";

 $result = mysql_query($query);
  if(!$result) error_message(sql_error());

  $num_rows = mysql_affected_rows($link_id);
  
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that doesn't make the variables available in the function. Functions has a difference scope of memory than outside of them. You have to pass variables in if they aren't superglobals. Read up on the global keyword, or function arguments.
Post Reply