Domanda

I am having some difficulty finding anything quite similar to what I am trying to accomplish with my code. I have a while loop that executes for each instance of a variable located in a mysql database.

$lref = mysqli_query($con,"SELECT * FROM builtl");

while($lrow = mysqli_fetch_array($lref))
{
   //variables from builtl
   $partnum = $lrow['m3num'];
   $lqty = $lrow['qty'];
   //POST to DB
   postcode($partnum,$lqty);
   echo $lqty;
...//More code exists that is not part of the question

The user defined function postcode() only executes when the form submission button (excluded in sample code) is set. Below is part of the function itself.

function postcode($partnum,$lqty)
{
    $bchk = $partnum."sb";
    if(isset($_POST[$bchk]))
    {
    //builtl
    $lqty=$lqty-1;
    $bltmod="UPDATE builtl SET qty=$lqty WHERE m3num=$partnum";
    if (!mysqli_query($con,$bltmod))
    {
    die('Error: ' . mysqli_error($con));
    }
    }
}

When the code executes, the database is updated inside of the function like it should, but the while loop has already been generated and doesn't pull the new value of $lqty in. I want some of my later code inside the while loop to use the updated value of $lqty. Would using return somewhere help me with this or would reloading the page to pull the new DB value be easier?

È stato utile?

Soluzione 2

try changing

function postcode($partnum,$lqty)

to

function postcode($partnum,&$lqty)

See http://www.php.net/manual/en/language.references.pass.php

Altri suggerimenti

Use this line to call postcode():

$lqty = postcode($partnum,$lqty);

and return $lqty from postcode() at the end of the function:

return $lqty;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top