Jump to content

Easier way to update records / won't update


KeeganWolf

Recommended Posts

Hello,

 

I'm trying to create a series of functions that will retrieve integer values from a table, reduce them by a value of 1 or 2, and then update the table with the new information. This is sort of a template. My issue is 1. Is there an easier way to write this while still working as a function with values passed to it, and 2. This will not update the records in my sql table.

<?php
//session_start(); 

// Connect to database
include_once "../scripts/connect_to_mysql.php";
$store = 4071;
$f1 = "SELECT * FROM Inventory$store WHERE invnum = 11522";
$f2 = "SELECT * FROM Inventory$store WHERE invnum = 11011";
$f3 = "SELECT * FROM Inventory$store WHERE invnum = 11121";
$f4 = "SELECT * FROM Inventory$store WHERE invnum = 11459";
$f5 = "SELECT * FROM Inventory$store WHERE invnum = 11466";
$f6 = "SELECT * FROM Inventory$store WHERE invnum = 11228";
$f7 = "SELECT * FROM Inventory$store WHERE invnum = 11463";
$f8 = "SELECT * FROM Inventory$store WHERE invnum = 11139";
$g1 = mysql_query($f1);
$g2 = mysql_query($f2);
$g3 = mysql_query($f3);
$g4 = mysql_query($f4);
$g5 = mysql_query($f5);
$g6 = mysql_query($f6);
$g7 = mysql_query($f7);
$g8 = mysql_query($f8);
$row1 = mysql_fetch_array($g1);
$row2 = mysql_fetch_array($g2);
$row3 = mysql_fetch_array($g3);
$row4 = mysql_fetch_array($g4);
$row5 = mysql_fetch_array($g5);
$row6 = mysql_fetch_array($g6);
$row7 = mysql_fetch_array($g7);
$row8 = mysql_fetch_array($g8);
echo $row1['case'];
echo $row2['case'];
echo $row3['case'];
echo $row4['case'];
echo $row5['case'];
echo $row6['case'];
echo $row7['case'];
echo $row8['case'];
$res1 = ($row1['case'] - 1);
$res2 = ($row2['case'] - 1);
$res3 = ($row3['case'] - 1);
$res4 = ($row4['case'] - 2);
$res5 = ($row5['case'] - 2);
$res6 = ($row6['case'] - 1);
$res7 = ($row7['case'] - 1);
$res8 = ($row8['case'] - 1);
$h1 = "UPDATE Inventory$store SET case='$res1' WHERE invnum = 11522";
$h2 = "UPDATE Inventory$store SET case='$res2' WHERE invnum = 11011";
$h3 = "UPDATE Inventory$store SET case='$res3' WHERE invnum = 11121";
$h4 = "UPDATE Inventory$store SET case='$res4' WHERE invnum = 11459";
$h5 = "UPDATE Inventory$store SET case='$res5' WHERE invnum = 11466";
$h6 = "UPDATE Inventory$store SET case='$res6' WHERE invnum = 11228";
$h7 = "UPDATE Inventory$store SET case='$res7' WHERE invnum = 11463";
$h8 = "UPDATE Inventory$store SET case='$res8' WHERE invnum = 11522";
mysql_query($h1);
mysql_query($h2);
mysql_query($h3);
mysql_query($h4);
mysql_query($h5);
mysql_query($h6);
mysql_query($h7);
mysql_query($h8);


if (!$g1) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}
?>

Any help is greatly appreciated, it's late and I'm so tired lol

Yikes! I don't like any part of that, but try this -

 

<?php

function updateInventory ($store, $invnum, $difference) {
     $sql = 'UPDATE `Inventory' . $store . '` SET case = case - ' . $difference . ' WHERE invnum = "' . $invnum . '" LIMIT 1;';
     return mysql_query($sql);
}

updateInventory(4071, 11522, 1);

That's an improvement at least. :-\

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.