For loop only loop one time - php

i have this code, suppose loop 5 time but it only loop one time only. How to make sure it will loop 5 time?.
for ($i = 0; $i < 5;)
{
$sql6 = "SELECT * FROM at_agent_management WHERE piam_reg = '$checkedValuesarr[$i]'";
$result6 = mysql_query($sql6);
while($row6 = mysql_fetch_array($result6)){
$reg = $row6['piam_reg'];
$date = $row6['piam_date'];
$agency = $row6['agency_name'];
$branch = $row6['branch'];
$category = $row6['agent_category'];
//$template_id = $reg_count;
$sql7 = "INSERT INTO at_agent_view_receipant(piam_reg, piam_date, agency_name, branch, agent_category, template_id) VALUES ('$reg', '$date', '$agency', '$branch', '$category', '$reg_count')";
$result7 = mysql_query($sql7);
}
$i++;
}

From logic you have written,
There is checkbox values array and you want to use that value in Query.
You can try with Foreach Loop as below,
<?
$checkedValuesarr=array("1","2","3","4","5");
foreach ($checkedValuesarr as $checkValue)
{
$sql6 = "SELECT * FROM at_agent_management WHERE piam_reg = '".$checkValue."'";
$result6 = mysql_query($sql6);
while($row6 = mysql_fetch_array($result6)){
$reg = $row6['piam_reg'];
$date = $row6['piam_date'];
$agency = $row6['agency_name'];
$branch = $row6['branch'];
$category = $row6['agent_category'];
//$template_id = $reg_count;
$sql7 = "INSERT INTO at_agent_view_receipant(piam_reg, piam_date, agency_name, branch, agent_category, template_id) VALUES ('$reg', '$date', '$agency', '$branch', '$category', '$reg_count')";
$result7 = mysql_query($sql7);
}
}?>

Related

Inserting values into another table using a conditional statement

I'm trying to insert values to another table using conditional statement but the values does not insert inside the table.
<?php
if($_POST['save'])
{
$pr_no = $_POST['pr_no'];
$s = mysqli_query($connect,"SELECT purchase_no FROM `sms_request` WHERE purchase_no = '".$pr_no."'");
if(mysqli_num_rows($s) > 0)
{
echo"<script type='text/javascript'>
alert('Purchase No. Existed!');
window.location.href = 'sms_supply management.php';
</script> ";
}
else
{
$fcluster = $_POST['fund_cluster'];
$osection = $_POST['office_section'];
$pr_no = $_POST['pr_no'];
$rcode = $_POST['responsibility_code'];
$desig = $_POST['desig'];
$requester = $_POST['requester'];
$loc = $_POST['loc'];
$purpose = $_POST['prpose'];
$ename = $_POST['entity_name'];
$date = $_POST['date'];
$dateA = date("Y-m-d",strtotime($date));
$radioo = $_POST['supply_type'];
$ins = mysqli_query($connect, "INSERT INTO sms_purchaserecord(purchase_no, supply_type) VALUES ('".$pr_no."', '".$radioo."')");
$insS = mysqli_query($connect, "INSERT INTO sms_ris(purchase_no, ris_num) VALUES ('".$pr_no."', '".$pr_no."')");
$insert = mysqli_query($connect, "INSERT INTO sms_request(purchase_no,sms_request.date, entity_name, fund_cluster, office_section, responsibility_code, purpose, stat) VALUES ('".$pr_no."','".$dateA."','".$ename."', '".$fcluster."', '".$osection."', '".$rcode."','".$purpose."', '1')");
$inS = mysqli_query($connect, "INSERT INTO sms_iar(iar_num, purchase_no) VALUES ('".$pr_no."', '".$pr_no."')");
$select = mysqli_query($connect, "SELECT request_IDnum FROM sms_request WHERE purchase_no = '".$pr_no."'");
while ($row = mysqli_fetch_array($select)){
$rnum = $row['request_IDnum'];
}
$select2 = mysqli_query($connect, "SELECT * FROM sms_branchloc WHERE loc_ID_no = '".$loc."'");
while ($row1 = mysqli_fetch_array($select2)){
$loc_num = $row1['loc_ID_no'];
}
if ($rnum != NULL AND $loc_num != NULL){
$insert2 = mysqli_query($connect, "INSERT INTO sms_requester(request_IDnum, name, loc_ID_no, position) VALUES ('".$rnum."', '".$requester."', '".$loc_num."', '".$desig."')");
}
if ($radioo == 'expandable'){
$insertI = mysqli_query($connect, "INSERT INTO inventory_status(stock_prop_num, description, unit, quantity, price) VALUES ('".$pr_num."','".$unit."','".$desc."','".$qty."','".$cost."')");
}
// If I choose expandable on the radio button it will insert into the inventory_status table
foreach ($_POST['sp_num'] as $row=>$sp_numm) {
$sp_num = $sp_numm;
$unit = $_POST['unt'][$row];
$desc = $_POST['sdesc'][$row];
$qty = $_POST['sqty'][$row];
$cost = $_POST['cost'][$row];
$query = mysqli_query($connect,"INSERT INTO sms_supply (supply_qty,purchase_no,unit_cost,supply_unit,supply_desc,stockproperty_num) VALUES ('".$qty."','".$pr_no."', '".$cost."', '".$unit."','".$desc."', '".$sp_num."')");
}
echo"<script type='text/javascript'>
alert('Purchase Request Save.');
window.location.href = 'sms_supply management.php';
</script>";
}
}
What I wanted to do is if I choose expandable on the radio button it will insert the values into the inventory_status table. The block of code that didn't work is
if ($radioo == 'expandable')
{
$insertI = mysqli_query($connect, "INSERT INTO
inventory_status(stock_prop_num, description, unit, quantity, price) VALUES('".$pr_num."', '".$unit."','".$desc."', '".$qty."', '".$cost."')");
Do I need to connect the 2 tables with a primary key and foreign key?

Data not inserting to database table

When I choose the expandable radio button and press the save button it should save its data to inventory_status table but the data wont insert into the inventory_status table. I've seen similar questions but still couldn't figure out the cause of the problem.
<?php
if($_POST['save']){
if ($_POST['supply_type'] == "expandable"){
$insertI = (mysqli_query($connect, "INSERT INTO inventory_status(stock_prop_no, unit, description, quantity, price) VALUES ('".$sp_num."','".$unit."','".$desc."','".$qty."','".$cost."')"));
}
$pr_no = $_POST['pr_no'];
$s = mysqli_query($connect,"SELECT purchase_no FROM `sms_request` WHERE purchase_no = '".$pr_no."'");
if(mysqli_num_rows($s) > 0)
{
echo"
<script type='text/javascript'>
alert('Purchase No. Existed!');
window.location.href = 'sms_supply management.php';
</script>
";
}
else{
$fcluster = $_POST['fund_cluster'];
$osection = $_POST['office_section'];
$pr_no = $_POST['pr_no'];
$rcode = $_POST['responsibility_code'];
$desig = $_POST['desig'];
$requester = $_POST['requester'];
$loc = $_POST['loc'];
$purpose = $_POST['prpose'];
$ename = $_POST['entity_name'];
$date = $_POST['date'];
$dateA = date("Y-m-d",strtotime($date));
$radioo = $_POST['supply_type'];
$ins = mysqli_query($connect, "INSERT INTO sms_purchaserecord(purchase_no, supply_type) VALUES ('".$pr_no."', '".$radioo."')");
$insS = mysqli_query($connect, "INSERT INTO sms_ris(purchase_no, ris_num) VALUES ('".$pr_no."', '".$pr_no."')");
$insert = mysqli_query($connect, "INSERT INTO sms_request(purchase_no,sms_request.date, entity_name, fund_cluster, office_section, responsibility_code, purpose, stat) VALUES ('".$pr_no."','".$dateA."','".$ename."', '".$fcluster."', '".$osection."', '".$rcode."','".$purpose."', '1')");
$inS = mysqli_query($connect, "INSERT INTO sms_iar(iar_num, purchase_no) VALUES ('".$pr_no."', '".$pr_no."')");
$select = mysqli_query($connect, "SELECT request_IDnum FROM sms_request WHERE purchase_no = '".$pr_no."'");
while ($row = mysqli_fetch_array($select)){
$rnum = $row['request_IDnum'];
}
$select2 = mysqli_query($connect, "SELECT * FROM sms_branchloc WHERE loc_ID_no = '".$loc."'");
while ($row1 = mysqli_fetch_array($select2)){
$loc_num = $row1['loc_ID_no'];
}
if ($rnum != NULL AND $loc_num != NULL){
$insert2 = mysqli_query($connect, "INSERT INTO sms_requester(request_IDnum, name, loc_ID_no, position) VALUES ('".$rnum."', '".$requester."', '".$loc_num."', '".$desig."')");
}
foreach ($_POST['sp_num'] as $row=>$sp_numm) {
$sp_num = $sp_numm;
$unit = $_POST['unt'][$row];
$desc = $_POST['sdesc'][$row];
$qty = $_POST['sqty'][$row];
$cost = $_POST['cost'][$row];
$query = mysqli_query($connect,"INSERT INTO sms_supply (supply_qty,purchase_no,unit_cost,supply_unit,supply_desc,stockproperty_num) VALUES ('".$qty."','".$pr_no."', '".$cost."', '".$unit."','".$desc."', '".$sp_num."')");
}
echo"
<script type='text/javascript'>
alert('Purchase Request Save.');
window.location.href = 'sms_supply management.php';
</script>
";
}
}
This is the table structure of our inventory_status table:
I found one issue on your following query. sms_request.date should be sms_request_date
$insert = mysqli_query($connect, "INSERT INTO sms_request(purchase_no,sms_request.date, entity_name, fund_cluster, office_section, responsibility_code, purpose, stat) VALUES ('".$pr_no."','".$dateA."','".$ename."', '".$fcluster."', '".$osection."', '".$rcode."','".$purpose."', '1')");
Other thing
All these '".$sp_num."','".$unit."','".$desc."','".$qty."','".$cost."')" variables are not defined. so you have to take this condition in loop.
For i.e.
foreach ($_POST['sp_num'] as $row=>$sp_numm) {
$sp_num = $sp_numm;
$unit = $_POST['unt'][$row];
$desc = $_POST['sdesc'][$row];
$qty = $_POST['sqty'][$row];
$cost = $_POST['cost'][$row];
if ($_POST['supply_type'] == "expandable"){
$insertI = (mysqli_query($connect, "INSERT INTO inventory_status(stock_prop_no, unit, description, quantity, price) VALUES ('".$sp_num."','".$unit."','".$desc."','".$qty."','".$cost."')"));
}
$query = mysqli_query($connect,"INSERT INTO sms_supply (supply_qty,purchase_no,unit_cost,supply_unit,supply_desc,stockproperty_num) VALUES ('".$qty."','".$pr_no."', '".$cost."', '".$unit."','".$desc."', '".$sp_num."')");
}

How to fetch record to sum upto a value and then add into two new tables?

I am trying to sum (add the values) from the database. My application checks the values from each row, adds up the value from each row up to 2000. And once it reaches up to 2000, it saves in the database (insert query) and continues the same till last record fetched. The total value summed (or totaled) up by each rows should not exceed over 2000.
There are two insert queries, One for inserting the total( from each row between 1800 and 2000) with the ID (like Primary key) generated and the second table add each row inserted with ID (the ID generated becomes now foreign key)
Please refer to the screenshot.
Please find the code below:
$i = 1;
do {
$id = $row_FetchRecordRS['ID'];
$dateissued = $row_FetchRecordRS['DateIssued'];
$rundateCarrierRun = $row_FetchRecordRS['RundateCarrierRunID'];
$timegenerated = $row_FetchRecordRS['TimeGenerated'];
$carrierID = $row_FetchRecordRS['CarrierRunID'] ;
$areaID = $row_FetchRecordRS['CarrierAreaID'];
$address = $row_FetchRecordRS['DeliveryAddress'];
$potzone = $row_FetchRecordRS['Postzone'];
$carr_ID = $row_FetchRecordRS['CarrierID'];
$instruction = $row_FetchRecordRS['DeliveryAddress'];
$areaRep = $row_FetchRecordRS['AreaRepDetails'];
// $vendor = $row_FetchRecordRS['VendorDetails'];
$quantity = $row_FetchRecordRS['Quantity'];
$direct = $row_FetchRecordRS['Direct'];
$jobID = $row_FetchRecordRS['JobID'];
$jobName = $row_FetchRecordRS['JobName'];
$bundlesize = $row_FetchRecordRS['Bundlesize'];
$bundle = $row_FetchRecordRS['Bundles'];
$items = $row_FetchRecordRS['Items'];
$weight = $row_FetchRecordRS['WeightKgs'];
$totalWeightCol = $row_FetchRecordRS['TotalWeightKgs'];
$date = date("D M d, Y G:i");
$total_weight = $row_FetchRecordRS['FinalWeight'] + $total_weight ;
echo "Row: " .$row_FetchRecordRS['FinalWeight']. "<br>";
echo "Total is______ $i : $total_weight <br><br>";
$sqlquerytest = "INSERT INTO `GenerateRun`
(`DateIssued`, `RundateCarrierRunID`, `TimeGenerated`,
`CarrierRunID`, `CarrierAreaID`, `DeliveryAddress`, `Postzone`,
`CarrierID`, `DeliveryInstruction`, `AreaRepDetails`,
`Quantity`, `Direct`, `JobID`, `JobName`, `Bundlesize`,
`Bundles`, `Items`, `WeightKgs`, `TotalWeightKgs`,
`LodingZoneID`)
VALUES
('$dateissued', '$rundateCarrierRun', '$timegenerated',
'$carrierID', '$areaID', '$address', '$potzone', '$carr_ID',
'$instruction', '$areaRep', '$quantity', '$direct', '$jobID',
'$jobName', '$bundlesize', '$bundle', '$items', '$weight',
'$totalWeightCol','$i')";
mysql_select_db($database_callmtlc_SalmatDB, $callmtlc_SalmatDB);
$ResultUpd1 = mysql_query($sqlquerytest, $callmtlc_SalmatDB) or die(mysql_error());
if ($total_weight >= 1800) {
$sqltransitlist = " INSERT INTO `TransitList`(`genID`, `total`) Values ('$i','$total_weight')";
mysql_select_db($database_callmtlc_SalmatDB, $callmtlc_SalmatDB);
$ResultUpd3 = mysql_query($sqltransitlist, $callmtlc_SalmatDB) or die(mysql_error());
$i = $i+1;
$total_weight = 0;
}
} while($row_FetchRecordRS = mysql_fetch_assoc($FetchRecordRS));
Some of your line of codes are irrelevant to your problem. I will simplify it for you.
$i = 1;
$total = 0;
$arr = array(); // for storing a list of data provides that the total doesn't exceed 2000
while ($row = mysql_fetch_assoc($record)) {
$id = $row['id'];
$name = $row['name'];
$num = $row['num'];
$arr[] = array('id' => $id, 'name' => $name, 'num' => $num);
if ($num + $total > 2000) {
$sql = "INSERT INTO Table1(genID, total) Values ('$i','$total')";
mysql_query($sql) or die(mysql_error());
foreach ($arr as $data) {
$sql = "INSERT INTO Table2(ID, name, genID, total) Values ('$data[id]','$data[name]','$i','$data[num]')";
mysql_query($sql) or die(mysql_error());
}
$arr = array(); // empty the array as the data has been stored to database
$i++;
$total = 0;
} else { // if the total doesn't exceed 2000, add it to total
$total += $num;
}
}
$sql = "INSERT INTO Table1(genID, total) Values ('$i','$total')";
mysql_query($sql) or die(mysql_error());
foreach ($arr as $data) {
$sql = "INSERT INTO Table2(ID, name, genID, total) Values ('$data[id]','$data[name]','$i','$data[num]')";
mysql_query($sql) or die(mysql_error());
}
Note: this code is just a sample, not your actual code. You can implement my code and match it to your code.

inserting record if count of column is less than two else not inserting into mysql

just want to add record if count of column is less than 2 for today's date and if count is more than two it should not get insert into the db.It's keep getting added after two records.
$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$img = $_POST['img'];
$amount = 5;
$sql = "SELECT COUNT(*) as totalupload FROM `daily_uploads` WHERE DATE_FORMAT(`date`, '%Y-%m-%d') = CURDATE()";
$row = mysqli_fetch_assoc($sql);
$sum = $row['totalupload'];
if ($sum < 2 ) {
$sql = "INSERT INTO `daily_uploads` (img, geoplugin_city, geoplugin_regionName, amount)
VALUES ('$img', '$city', '$region','$amount')";
if ($conn->query($sql)) {
echo ('success');
} else {
echo ('error');
}
} else {
echo"already exist";
make the connection after count query like this,
$result = mysqli_query($con,$sql);
Try this one hope it will help you.
$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$img = $_POST['img'];
$amount = 5;
$sql = "SELECT COUNT(*) as totalupload FROM `daily_uploads` WHERE DATE_FORMAT(`date`, '%Y-%m-%d') = CURDATE()";
$qry= mysql_query($sql);
$row = mysql_fetch_assoc($qry);
$count = $row['totalupload'];
if ($count < 2 ) {
$sql = "INSERT INTO `daily_uploads` (img, geoplugin_city, geoplugin_regionName, amount)
VALUES ('$img', '$city', '$region','$amount')";
if ($conn->query($sql)) {
echo ('success');
} else {
echo ('error');
}
} else {
echo"already exist";

How To Loop SQL Insert for many data On PHP

I have many data on array, in this case I must insert on SQL one by one... can you help me to solve this? I want it to sort insert data... I use manual insert... can you give me solution to foreach or looping SQL?
$user_id = getUserId();
$current_dttm = getSQLDate();
$org_id = $_SESSION["ehr_org_id"];
$id = $this->getInosId();
list($patient_id,$inf_id,$num,$tindakan_id,$denum)=$args;
$inf_id = explode('%7C',$inf_id);
$inf_id_1 = $inf_id[0];
$inf_id_2 = $inf_id[1];
$inf_id_3 = $inf_id[2];
$inf_id_4 = $inf_id[3];
$inf_id_5 = $inf_id[4];
$inf_id_6 = $inf_id[5];
$inf_id_7 = $inf_id[5];
$num = explode('%7C',$num);
$num_1 = $num[0];
$num_2 = $num[1];
$num_3 = $num[2];
$num_4 = $num[3];
$num_5 = $num[4];
$num_6 = $num[5];
$num_7 = $num[6];
$tindakan_id = explode('%7C',$tindakan_id);
$tindakan_id_1 = $tindakan_id[0];
$tindakan_id_2 = $tindakan_id[1];
$tindakan_id_3 = $tindakan_id[2];
$tindakan_id_4 = $tindakan_id[3];
$tindakan_id_5 = $tindakan_id[4];
$tindakan_id_6 = $tindakan_id[5];
$tindakan_id_7 = $tindakan_id[6];
$denum = explode('%7C',$denum);
$denum_1 = $denum[0];
$denum_2 = $denum[1];
$denum_3 = $denum[2];
$denum_4 = $denum[3];
$denum_5 = $denum[4];
$denum_6 = $denum[5];
$denum_7 = $denum[6];
$sql_inf_1 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_1','$num_1','$tindakan_id_1','$denum_1','','$current_dttm','$current_dttm','$user_id','normal')";
$sql_1 = $db->query($sql_inf_1);
$sql_inf_2 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_2','$num_2','$tindakan_id_2','$denum_2','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_2);
$sql_inf_3 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_3','$num_3','$tindakan_id_3','$denum_3','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_3);
$sql_inf_4 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_4','$num_4','$tindakan_id_4','$denum_4','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_4);
$sql_inf_5 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_5','$num_5','$tindakan_id_5','$denum_5','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_5);
$sql_inf_6 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_6','$num_6','$tindakan_id_6','$denum_6','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_6);
$sql_inf_7 = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd)"
." values('$id','$patient_id','$org_id','$inf_id_7','$num_7','$tindakan_id_7','$denum_7','','$current_dttm','$current_dttm','$user_id','normal')";
$db->query($sql_inf_7);
I want to insert all variable 1 - 7 on sort
You can put multiple sets of values in a single INSERT query. So concatenate all the values that you're inserting.
$values = array();
for ($i = 0; $i < count($inf_id); $i++) {
$values[] = "('$id', '$patient_id', '$org_id', '{$inf_id[$i]}','{$num[$i]}','{$tindakan_id[$i]}','{$denum[$i]}','','$current_dttm','$current_dttm','$user_id','normal')"
}
$values_string = implode(',', $values);
$sql = "insert into xocp_ehr_pencatatanInos (id,patient_id,org_id,inf_id,num,tindakan,denum,pengali,tgl_kejadian,created_dttm,created_user,status_cd) VALUES $values_string";
$db->query($sql);

Categories