I have two tables: parts (with codice, pezzi, durata) and magazzino (with codiceM, pezziM, durataM)
I want to add or update some records from parts to magazzino. What I would Like to do is:
check if codice is already present in the table magazzino, if not INSERT a record with codice, pezzi and durata.
if codice is already present in magazzino, sum and UPDATE pezzi and durata associated with codice.
I use phprunner to create database and insert a button that executes the code after selecting a record in parts.
Here is my code that execute with no errors but it gaves me no results.
$record = $button->getNextSelectedRecord();
$cod=$record["codice"]; //variable assignments
$qnty=$record["pezzi"];
$time=$record["durata"];
$control=0; //control variable
$con = new mysqli("localhost","root","","provaMagazzino") or die("sorry not connected");
$sql = "SELECT * FROM magazzino";
$resultq = $con->query($sql);
while($row = mysql_fetch_array($resultq)){ // check and update records in magazzino table
echo($row['codice']);
if ($row['codice']==$cod) {
$row['pezziM']+=$qnty;
$row['durataM']+=$time;
echo('durataM');
$control=1;
break;
}
}
if ($control=0) { //add new records al Magazzino if control variable is zero
$resultq->codiceM = $record["codice"];
$resultq->durataM = $record["durata"];
$resultq->pezziM = $record["pezzi"];
$resultq->descrizioneM = $record["descrizione"];
$resultq->Add();
}