My issue is to display the data. It repeats the same id in it several times.
<?php
$check_shared_section = mysqli_query($MYSQLi,"select * from `vp_wall_post` where `type` = 'section' and `username` = '".mysqli_real_escape_string($MYSQLi,$poster_username)."' ");
$get_section = mysqli_fetch_array($check_shared_section);
$section_post = trim(strip_tags($get_section["post"]));
$section_page = trim(strip_tags($get_section["page_id"]));
$section_id = trim(strip_tags($get_section["pid"]));
echo $section_id;
?>
These are the issues that I am facing. Please help me to solve this problem.
It shows the output id
e4zDFOL3jBgcH8YRfkzJ
e4zDFOL3jBgcH8YRfkzJ
e4zDFOL3jBgcH8YRfkzJ
I want to show different id
e4zDFOL3jBgcH8YRfkzJ
er556gdfg4asffgfgfgg
So2cLYtCTTMYD0fCNFjq
JGH63vAqIAnt5jNCH6OL
<?php
$check_shared_section = mysqli_query($MYSQLi,"select * from `vp_wall_post` where `type` = 'section' and `username` = '".mysqli_real_escape_string($MYSQLi,$poster_username)."' ");
$get_section = mysqli_fetch_array($check_shared_section);
$section_post = trim(strip_tags($get_section["post"]));
$section_page = trim(strip_tags($get_section["page_id"]));
$section_id = trim(strip_tags($get_section["pid"]));
echo $section_id;
?>
I'm not sure why you are seeing the same pid value multiple times as you don't have any looping in your code at all. However the reason you are not getting all the different pid values is that you're not looping the results of the query. You need to use a while loop on the result of mysqli_fetch_array e.g.
while ($get_section = mysqli_fetch_array($check_shared_section)) {
$section_post = trim(strip_tags($get_section["post"]));
$section_page = trim(strip_tags($get_section["page_id"]));
$section_id = trim(strip_tags($get_section["pid"]));
echo $section_id;
// do other stuff with the values
}
Note that if you want to further process the results outside the while loop, you will need to save the values into arrays e.g.
$section_posts = array();
$section_pages = array();
$section_ids = array();
while ($get_section = mysqli_fetch_array($check_shared_section)) {
$section_posts[] = $section_post = trim(strip_tags($get_section["post"]));
$section_pages[] = $section_page = trim(strip_tags($get_section["page_id"]));
$section_ids[] = $section_id = trim(strip_tags($get_section["pid"]));
echo $section_id;
// do other stuff with the values
}
// you can use other loops to further process the values here
// e.g. foreach ($section_posts as $index => $section_post) {
// you can use $index to access the corresponding values from
// the $section_pages and $section_ids arrays e.g.
// $section_page = $section_pages[$index]
I am on point where I have to usk on forum.
So, I have an array that is my return from join table sql query.
i am displaying it correctly without the problem.
but some of those values I want to put in different table of mysql database.
$array = joint_table();
$array_value = array['key'];
I can echo array_value and it's displaying correctly, also checked variable type and it returns STRING.
however when I am inserting it into the table, it's empty cell.
I am inserting other stuff like date() and such and that is inserted correctly.
So my sql query works fine, besides I am using same query in other places without problem.
Only values I have from that array are not inserting, but still can echo them.
<?php
$page_title = 'Complete Task';
require_once('includes/load.php');
// Checkin What level user has permission to view this page
page_require_level(2);
$task = join_task_table((int)$_GET['id']);
?>
<?php
if(isset($_POST['complete_task'])){
$area = $task['area'] ;
$jig = $task['jig'];
$desc = $task['description'];
$freq = $task['freq'];
$date = make_date();
$user = current_user();
$user_done = remove_junk(ucfirst($user['name']));
$comment = remove_junk($db->escape($_POST['comment']));
if(empty($errors)){
$sql = "INSERT INTO tpm_history (area_name,jig_name,description,frequency,date_done,done_by_user,comment)";
$sql .= " VALUES ('{$area}','{$jig}','{$desc}','{$freq}','{$date}','{$user_done}','{$comment}')";
$result = $db->query($sql);
if($result && $db->affected_rows() === 1){
$session->msg('s',"Job Completed");
redirect('home.php', false);
} else {
$session->msg('d',' Sorry failed to complete the task!');
redirect('task_complete.php?id='.$task['id'], false);
}
} else{
$session->msg("d", $errors);
redirect('task_complete.php?id='.$task['id'],false);
}
}
?>
I am lost. Help.
I am new to php and I want to insert data into using insert query where 2 fields that is size and numberofitems are array and other are normal fields and id is autoincrement in my database.It is giving failure when i run this code.Any help would be appreciated.
<?php
define('HOST','localhost');
define('USER','user');
define('PASS','pass');
define('DB','db');
$con = mysqli_connect(HOST,USER,PASS,DB);
include 'filename.php';
include 'filename1.php';
$item=$_POST['url'];
$color=$_POST['color'];
$date1=date("Y-m-d");
echo 'working3';
$s=array($_POST['size']);
$s1=array($_POST['numberofitems']);
if(is_array($s)){
$sql="INSERT INTO placeorder VALUES ";
$valuesArr = array();
for($i=0;$i<count($s); $i++){
echo 'working4';
$size = mysqli_real_escape_string( $s[$i] );
$n = mysqli_real_escape_string( $s1[$i] );
$valuesArr[] = "('$contact','$name','$item','$color','$size','$n','$date1')";
echo $valuesArr[$i];
}
$sql .= implode(',', $valuesArr);
if(mysqli_query($con,$sql)){
echo"success";
}else
echo"failure";
}
mysqli_close($con);
?>
Here I'm trying to insert the datas again into database new table (with quantity & customer details). $grocery_id and $grocery_item values are fetch from database. $customername, $customermobile, $groqty values are user will enter the details in that appropriate textfield.
When I execute this code ($groceryid, $groceryitem) -> These two column always stored the last row values. Because I've put the query outside of foreach loop. Here is my problem. If I put the query inside the foreach it works fine. But, quantity values doesn't work properly. So, How can I execute the query properly (outside of foreach loop)?
<?php
if(isset($_POST['submit']))
{
$grocery_id = $rowid;
$grocery_item = $rowsitem;
$customername = $_POST['customername'];
$customermobile = $_POST['customermobile'];
$groqty = $_POST['groceryquantity'];
for($i = 0; $i < sizeof($groqty); $i++)
{
$groqtys = $groqty[$i];
foreach($grocery_id as $key => $index_id )
{
}
$sql = "INSERT INTO ".customer_order." SET grocery_id = '$index_id' , grocery_item = '$grocery_item[$key]', customername = '$customername', customermobile = '$customermobile', quantity = '$groqtys' ";
mysql_query($sql,$CN);
$response = asort_form_ok("Your order successfully submitted. We will deliver you soon.");
}
}
?>
You could simply use one foreach loop considering the index values of $grocery_id and $groqty are the same.
Try:
<?php
if (isset($_POST['submit']))
{
$grocery_id = $rowid;
$grocery_item = $rowsitem;
// sanitizing your values
$customername = mysql_real_escape_string($_POST['customername']);
$customermobile = mysql_real_escape_string($_POST['customermobile']);
$groqty = array_map('mysql_real_escape_string', $_POST['groceryquantity']);
foreach($grocery_id as $key => $index_id)
{
$sql = "INSERT INTO " . customer_order . " SET grocery_id = '$index_id' , grocery_item = '$grocery_item[$key]', customername = '$customername', customermobile = '$customermobile', quantity = '$groqty[$key]' ";
mysql_query($sql, $CN);
$response = asort_form_ok("Your order successfully submitted. We will deliver you soon.");
}
}
?>
Also note:
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
I have an invoice form which posts multiple rows to an insert page for insertion to multiple rows.My stumbling block is that when I insert the multiple order items with a foreach statement I also insert the invoice gross for each row. How would I go about only inserting for example the "invoice gross amount"on the last line of the insert in a column of my choosing.
I have enclosed the code I am using to insert the form, and before it is mentioned, I have stripped out calls to data cleansing functions for clarity.
<?php
//Lets connect to the database
if(mysqli_connect_errno()){
exit("Failed to connect to the database".mysqli_connect_error());}
//We first insert the header line of the purchase invoice, then we get the insert id and switch to the details table and insert
//the purchased items details
$query ="INSERT INTO acc_posting_headers (company_id,supplier_id,header_ref,header_date,header_type,posting_ref,due_date)
values
('$company_id','$supplier_id','$receipt_ref','$invoice_date','$header_type','$syspost_ref','$due_date')";
//Execute the $sql query on the server to insert the values
if ($conn->query($query) === TRUE) {
$last_post_header_id = $conn->insert_id;
$post_header_id = $last_post_header_id;
/* We have now switched to the details table, so we got the last insert id, we have set the variable
$post header and now insert each purchased item */
foreach ($_POST['lst_nominal_code'] as $row=>$id){
$nominal_code = $_POST['hidden_nominal_code'][$row];
$quantity = $_POST['txt_quantity_'][$row];
$item_cost = $_POST['txt_item_cost'][$row];
$line_total = $_POST['txt_line_total'][$row];
$description = $_POST['txt_description'][$row];
$trade_creditors = 2109;
$invoice_gross = $_POST['txt_gross'];
$vat_line = $_POST['txt_line_vat_'][$row];
$nominal_id = $id;
$subtotal = $_POST['txt_subtotal'];
$vat_total = $_POST['txt_vat_total'];
$vat_control = 2200;
$query = ("INSERT INTO acc_posting_details
(post_header_id,nominal_acc_no,quantity,price,line_total,line_vat,vat_total,description,subtotal,debit,credit)
VALUES
('$post_header_id','$nominal_code','$quantity','$item_cost','$line_total','$vat_line','0','$description','$subtotal','$line_total','0'),
('$post_header_id','$vat_control','0','0','0','$vat_total','total VAT','vat content','0','$vat_total','0'),
('$post_header_id','$trade_creditors','0','0','0','0','0','trade Creditors','0','0','$invoice_gross')");
if ($conn->query($query) === TRUE) {
Header("Location:../add_purchase_invoice.php");
} else {
echo 'Error: '. $conn->error;
}
}
}
$conn->close();
?>
<?php
// echo "<pre>";
// print_r($_POST);
// echo "<pre>";
//echo "vat control".$vat_control;
//echo "vat total is:".$vat_total
?>
Solved, it was quiet easy really once I had a good look,by changing the order of the insert and removing from the "foreach" I achieved what I wanted with the following code. Thanks to all that helped.
<?php session_start();?>
<?php require_once('../Connections/connpay.php'); ?>
<?php require_once('../functions/global_functions.php'); ?>
<?php require_once('../functions/account_functions.php'); ?>
<?php
$supplier_id = clean_data($_POST['lst_supplier']);
$receipt_ref = clean_data($_POST['txt_receipt_ref']);
$invoice_date = clean_data($_POST['txt_receipt_date']);
$due_date = clean_data ($_POST['txt_receipt_due']);
$due_date = clean_data($_POST['txt_receipt_due']);
$company_id = clean_data($_POST['hidden_company_id']);
$syspost_ref = clean_data($_POST['txt_post_ref']);
$header_type = "puchase_invoice";
$nominal_id = clean_data($_POST['lst_nominal_code']);
?>
<?php
//Lets connect to the database
if(mysqli_connect_errno()){
exit("Failed to connect to the database".mysqli_connect_error());}
//We first insert the header line of the purchase invoice, then we get the insert id and switch to the details table and insert
//the purchased items details
$query ="INSERT INTO acc_posting_headers (company_id,supplier_id,header_ref,header_date,header_type,posting_ref,due_date)
values
('$company_id','$supplier_id','$receipt_ref','$invoice_date','$header_type','$syspost_ref','$due_date')";
//Execute the $sql query on the server to insert the values
if ($conn->query($query) === TRUE) {
// we now get the last insert id, and set it to a variable, we also declare the vat & gross amount variables
$last_post_header_id = $conn->insert_id;
$post_header_id = $last_post_header_id;
$vat_control = 2200;
$vat_total = clean_data($_POST['txt_vat_total']);
$trade_creditors = 2109;
$invoice_gross = clean_data($_POST['txt_gross']);
//We are now at the posting details table, we insert the trade creditors and total vat cr amounts first.
$query = ("INSERT INTO acc_posting_details
(post_header_id, nominal_acc_no ,quantity,price,line_total,line_vat,vat_total,description,subtotal,debit,credit)
VALUES
('$post_header_id','$trade_creditors','0','0','0','0','0','trade Creditors','0','0','$invoice_gross'),
('$post_header_id','$vat_control','0','0','0','$vat_total','0','vat content','0','$vat_total','0')");
$conn->query($query);
// Now that we have inserted the two lines to show the trade creditors and vat CR and DR amounts we
//carry on and insert each invoice line from the posted array.
foreach ($_POST['lst_nominal_code'] as $row=>$id){
$nominal_code = $_POST['hidden_nominal_code'][$row];
$quantity = clean_data($_POST['txt_quantity_'][$row]);
$item_cost = clean_data($_POST['txt_item_cost'][$row]);
$line_total = clean_data($_POST['txt_line_total'][$row]);
$description = clean_data($_POST['txt_description'][$row]);
$vat_line = clean_data($_POST['txt_line_vat_'][$row]);
$nominal_id = $id;
$subtotal = clean_data($_POST['txt_subtotal']);
$query = ("INSERT INTO acc_posting_details
(post_header_id, nominal_acc_no ,quantity,price,line_total,line_vat,vat_total,description,subtotal,debit,credit)
VALUES
('$post_header_id','$nominal_code','$quantity','$item_cost','$line_total','$vat_line','0','$description','0','$line_total','0')");
// execute the insert query and push on to the purchase invoice page.
if ($conn->query($query) === TRUE) {
Header("Location:../add_purchase_invoice.php");
} else {
echo 'Error: '. $conn->error;
}
}
}
$conn->close();
?>
<?php
//echo "<pre>";
// print_r($_POST);
// echo "<pre>";
?>
It sounds like your core challenge is determining when you are in the last iteration of your foreach loop, so that you can then modify the query. Is that right?
You could use a counter to help. Something along these lines:
$i = 0;
$len = count($_POST['lst_nominal_code']);
foreach ($_POST['lst_nominal_code'] as $row=>$id) {
// Assign (don't forget to sanitize!) all the POST variables
<snip>
if ($i == $len - 1) {
// This is the last line! We want a different query, with $invoice_gross included
$query = "...";
} else {
// This is NOT the last line, use the default query without $invoice_gross
$query = "...";
}
$conn->query($query);
$i++;
}