Using Triggers and Procedures to change active inventory - php

First starting with this table:
Table estoque_folha
And your records:
Records
My complete database code : pastebin.com/v30s9Aa1
I wanted to know how a trigger / procedure for when the quantidade field is zero, the line stow with fild ativo == 1 , it zero to set this record and after it to set 1 in the ativo field where the espera field is 1 . That is:
When quantidade == 0 where ativo == 1, to set ativo == 0 and then looking where espera == 1 and set ativo == 1
At the moment I have this trigger, that when the inventory is zero, disables and adds date in the field data_saida
CREATE TRIGGER `tr_setaDataeDesativa` BEFORE UPDATE ON `estoque_folha`
FOR EACH ROW
IF NEW.quantidade = 0 THEN
SET NEW.data_baixa = current_date;
SET NEW.ativo = '0';
ELSEIF NEW.quantidade > 0 THEN
SET NEW.data_baixa = 0000-00-00;
END IF
I need to create another trigger or can take advantage of this?
Thank you

Related

Count Not Null Columns using PHP

i have a table "tbl_userpersonal",
what i want to achieve is check which columns are filled and which are not so i can calculate or display the profile completion percentage.
\
So far i've tried a lot of different techniques and codes and my code is counting all columns but i want it to count empty value columns as 0.
please help me with a solutions.
So far if there is even a single entry for a user in any column it's giving me 100% otherwise 0% there is no inbetween
Currently if there is value inside "father_name" & "mother_name" & "DOB" column for a user with user_authtoken = "app_7837hfjd57hdj" the expected output should be
3
50% i.e (3/6)*100
what is happening right now is
if there is value inside either of these columns for a user with user_authtoken = "app_7837hfjd57hdj" the output is showing as
6
100% i.e (6/6)*100
or if there is no entry for user with user_authtoken = "app_7837hfjd57hdj" the output is giving
0
0%
here is the php code
$personal = mysqli_query($con,"
SELECT father_name,
mother_name,
DOB,
adhar_no,
address,
religion,
CASE WHEN father_name IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN mother_name IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN DOB IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN adhar_no IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN address IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN religion IS NOT NULL THEN 1 ELSE 0 END AS personal
FROM user WHERE `user_authtoken` = '$user_ath'
") or die(mysqli_error($con));
$data=mysqli_fetch_assoc($personal);
$pro_count = $data['personal'];
echo $pro_count ;
echo '<br>';
$percentage = ($pro_count /6)*100;
echo $percentage;
i think you have empty value in table not null try this
this will check for null as well as empty
$personal = mysqli_query($con,"
SELECT father_name,
mother_name,
DOB,
adhar_no,
address,
religion,
CASE WHEN father_name IS NOT NULL AND father_name<>'' THEN 1 ELSE 0 END +
CASE WHEN mother_name IS NOT NULL AND mother_name<>'' THEN 1 ELSE 0 END +
CASE WHEN DOB IS NOT NULL AND DOB<>'' THEN 1 ELSE 0 END +
CASE WHEN adhar_no IS NOT NULL AND adhar_no<>'' THEN 1 ELSE 0 END +
CASE WHEN address IS NOT NULL AND address<>'' THEN 1 ELSE 0 END +
CASE WHEN religion IS NOT NULL AND religion<>'' THEN 1 ELSE 0 END AS personal
FROM user WHERE `user_authtoken` = '$user_ath'
") or die(mysqli_error($con));

How to using if statement in mysql

i want to minus 1 (-1) value from durasi_pre_u if durasi_pre_u have basic value bigger then 0 and update Premium to Member when durasi_pre_u have value = 0.
here what i try
UPDATE user
SET durasi_pre_u = IF(durasi_pre_u > 0, durasi_pre_u - 1, durasi_pre_u),
pangkat_u = IF(durasi_pre_u <> 1 AND pangkat_u = 'Premium', pangkat_u = 'Member', pangkat_u)
this code working -1 durasi_pre_u but not working change Premium to Member
i try to follow tutorial from this site https://www.w3resource.com/mysql/control-flow-functions/if-function.php
You have an error in your query, firstly you only want to change the value of pangkat_u when the current value of durasi_pre_u is 1 (so it is about to change to 0) so you need to change the <> to =. Secondly you have pangkat_u = 'Member' as the new value, which is treated as a boolean expression (i.e. a value of 1 or 0) by MySQL. What you actually want is just 'Member'. So your query should be:
UPDATE user
SET durasi_pre_u = IF(durasi_pre_u > 0, durasi_pre_u - 1, durasi_pre_u),
pangkat_u = IF(durasi_pre_u = 1 AND pangkat_u = 'Premium', 'Member', pangkat_u)
In If function, you only need to specify the values to be set in case of true or false. Dont specify the field name = value (pangkat_u = 'Member')
Use the following :
UPDATE user
SET durasi_pre_u = IF(durasi_pre_u > 0, durasi_pre_u - 1, durasi_pre_u),
pangkat_u = IF(durasi_pre_u <> 1 AND pangkat_u = 'Premium', 'Member', pangkat_u)

How to give serial no to records with condition in Mysql query

I have a table in where two type of products are stored. One table for single item with unique id and the other is for combination of items having the same Id for one combination like in the image below:
I have to run an update query in my SQL, which will give serial number Sno for this.
If the DealCode is the same, assign same serial number, otherwise increment it. Pleae note that if the DealCode is 0 then it should be incremented. 0's are for different product.
may be this will help you-
you can perform the mentioned task via creating a stored procedure for this
lets suppose you have a table named "product"
stored procedure defination --
DELIMITER $$
DROP PROCEDURE IF EXISTS assignSerialNo$$
CREATE PROCEDURE `assignSerialNo`()
BEGIN
DECLARE x INT;
DECLARE y INT;
DECLARE data INT;
DECLARE countNonZeroRecord INT;
DECLARE countZeroRecord INT;
SET x = 1;
SET countNonZeroRecord = ( SELECT count(*) FROM product where DealCode != 0 );
SET countZeroRecord = ( SELECT count(*) FROM product where DealCode = 0 );
WHILE x <= countZeroRecord DO
SET data = ( SELECT WebTransactionDetailNo from product WHERE Sno = 0 LIMIT 1 );
UPDATE product SET Sno = x WHERE WebTransactionDetailNo = data; SET x = x + 1;
END WHILE;
SET y = 1;
WHILE y <= countNonZeroRecord DO
SET data = ( SELECT DealCode from product WHERE Sno = 0 AND DealCode !=0 LIMIT 1 );
UPDATE product SET Sno = x WHERE DealCode = data; SET x = x + 1;
SET y = y + 1;
END WHILE;
END
Call the procedure for setting the serial no. for records by using the syntax -
call assignSerialNo()
**
For PHP:
if you want to do the above task with core php then you can use the below code :
hope this will help you
$connection = mysqli_connect("hostName","userName","password","databaseName");
$x=0;
$nonZeroDealCodeResult = runQuery("SELECT count(*) as countRecords FROM product where DealCode != 0",$connection);
$zeroDealCodeResult = runQuery("SELECT count(*) as countRecords FROM product where DealCode = 0",$connection);
while($zeroDealCodeResult['countRecords']) {
$x++;
$searchQuery = "SELECT WebTransactionDetailNo from product WHERE Sno = 0 AND DealCode = 0 LIMIT 1";
$data = runQuery($searchQuery,$connection);
mysqli_query($connection, "UPDATE product SET Sno = {$x} WHERE WebTransactionDetailNo = {$data['WebTransactionDetailNo']}");
$zeroDealCodeResult['countRecords']--;
}
while($nonZeroDealCodeResult['countRecords']) {
$x++;
$searchQuery = "SELECT DealCode from product WHERE Sno = 0 AND DealCode !=0 LIMIT 1";
$data = runQuery($searchQuery,$connection);
mysqli_query($connection, "UPDATE product SET Sno = {$x} WHERE DealCode = {$data['DealCode']}");
$nonZeroDealCodeResult['countRecords']--;
}
function runQuery($searchQuery,$connection) {
$dataQuery = mysqli_query($connection, $searchQuery);
$data = mysqli_fetch_assoc($dataQuery) ;
return $data;
}
?>

How can I select data from table based on one or multiple condition?

I am working using MySQL/PHP, I wanted to select data from a table based on one or more condition but using minimum if statements.
For example if I have a table
Students | Register | End | Points
Mark | 14/1 | 18/6 | 14
Dina | 18/12 | 19/2 | 1
Karl | 1/1 | 1/2 | 9
I have a form where user can look for data based on one or more criteria (register, end, points) based on the data he chose to set.
If he only set one of the criteria lets say after an date, he will get all results based on that date.
If he set two or three conditions maybe (register and end) or (end and points) or even (register and end and points) he will get the result based on all conditions joint together not only one of them or all of them separately.
when I use AND between conditions and the user don't set one of the conditions it returns empty query.
when I use OR between conditions and the user choose multiple conditions it returns data but based on each of conditions separately not joining it.
Queries I tried:
SELECT * FROM table WHERE Register >= '$choice1' OR End <= '$choice2' OR Points = '$choice3';
SELECT * FROM table WHERE Register >= '$choice1' AND End <= '$choice2' AND Points = '$choice3';
What is the best way to do that?
Use php to build your WHERE clause, something like this:
$condition = '';
if($choice1 != '')
$condition .= "Register >= '$choice1'";
if($choice2 != '')
$condition .= ($condition != '' ? " AND " : '') . "End <= '$choice2'";
if($choice3 != '')
$condition .= ($condition != '' ? " AND " : '') . "Points = '$choice3'";
$sql = "SELECT * FROM table" .($condition != '' ? " WHERE $condition;" : ";");

PHP / SQL give rest of users a value

What I did was to give 2 users in my DB a categorie_bit value of 1,
When I click on a button. That works so far. Now I am trying to give the rest of the user a categorie_bit value of 0, if I click on that button.
So basically:
Give 2 Random Users categorie_bit value 1 & Give Rest of the User in DB categorie_bit value of 0.
(Again giving 2 random Users value of 1 works and saves in db.)
I tried this so far to give the rest a value of 0:
$member = $staff->getStaffData($id);
$queryCheck = $database->query('SELECT id,firstname,lastname,categorie_bit FROM `staff` WHERE categorie_bit = 1');
$queryCheckResult = $queryCheck->fetch();
$queryMaximum = $queryCheckResult > 1;
var_dump($queryMaximum); // true
// MY SQL TO GIVE REST A VALUE OF 0
$taste = $database->query('SELECT id,firstname,lastname FROM `staff` WHERE categorie_bit = 1 SET categorie_bit = 0');
if($queryMaximum) {
echo "first step works";
if(is_array($taste) || is_object($taste)) {
echo "second step works";
foreach ($taste as $testx) {
var_dump($testx);
}
} else {
echo "second does not work";
}
}
This did not work. I always get the echo of second does not work.
Can anyone help me:)
The request you perform is wrong.
Either you make a select to retrieve user or you make an update to update user.
If you replace
'SELECT id,firstname,lastname FROM `staff` WHERE categorie_bit = 1 SET categorie_bit = 0'
With :
"UPDATE staff SET categorie_bit = 0 where categorie_bit = 1"
Your data should be updated ;)
you have used SET in SELECT statement which can be used in UPDATE query. Be specific on what you want to query from DB. If you want to update all records with category_bit = 0 then
$taste = $database->query('UPDATE `staff` SET categorie_bit = 0 where categorie_bit = 1');

Categories