Optimize PHP coding.

Hello

I have a code here which is working but it seems wrong.

[php]$cust_code=$_POST[‘customer’] ;

$cust_search = mysqli_query($con,“SELECT * FROM customer_info WHERE rec_no=’$cust_code’”);
if(mysqli_num_rows($cust_search) != 0) {

$cust_code2 = mysqli_fetch_array($cust_search);
$cust_company = $cust_code2['inv_code'];

$petsa_taon = date("Y");
mysqli_query($con,"INSERT INTO invoice_number(company, taon) 
		VALUES ('$cust_company', '$petsa_taon') ");
[/php]

I have to use cust_code2 and cust_company which there might be a better solution.
All I want is get the value of column inv_code and put it into $cust_company without using $cust_code2

Thanks in advance.

I suspect you may have some database design issues. Post the SQL dump of your database.

As far as your query, instead of select *, change it to just select the column you want.

SELECT inv_code FROM…

Hello

I am not sure what you mean by “Post the SQL Dump” but please advise if I did something wrong.

[code]-- phpMyAdmin SQL Dump
– version 3.4.10.1deb1
http://www.phpmyadmin.net

– Host: localhost
– Generation Time: Jun 24, 2014 at 04:08 PM
– Server version: 5.5.35
– PHP Version: 5.3.10-1ubuntu3.9

SET SQL_MODE=“NO_AUTO_VALUE_ON_ZERO”;
SET time_zone = “+00:00”;

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;
/
!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;
/
!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;
/
!40101 SET NAMES utf8 */;


– Database: billing



– Table structure for table customer_info

CREATE TABLE IF NOT EXISTS customer_info (
rec_no smallint(11) NOT NULL AUTO_INCREMENT,
inv_code varchar(5) NOT NULL,
company_name text NOT NULL,
company_address text NOT NULL,
attention varchar(30) NOT NULL,
terms varchar(50) NOT NULL,
mode_payment varchar(30) NOT NULL,
account_name varchar(50) NOT NULL,
bank varchar(50) NOT NULL,
bank_address text NOT NULL,
bank_acct_no varchar(20) NOT NULL,
currency varchar(5) NOT NULL,
swift_code varchar(10) NOT NULL,
PRIMARY KEY (rec_no)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;


– Dumping data for table customer_info

(Deleted for security purpose)



– Table structure for table invoice_data

CREATE TABLE IF NOT EXISTS invoice_data (
rec_no int(11) NOT NULL AUTO_INCREMENT,
cust_rec smallint(6) NOT NULL,
inv_no varchar(15) NOT NULL,
description text NOT NULL,
part_number varchar(15) NOT NULL,
fob_price smallint(6) NOT NULL,
qty decimal(6,3) NOT NULL,
total_fob int(11) NOT NULL,
trading_price decimal(11,3) NOT NULL,
amount decimal(11,3) NOT NULL,
lock_inv tinyint(1) NOT NULL DEFAULT ‘1’,
company_code varchar(5) NOT NULL,
taon varchar(4) NOT NULL,
buwan varchar(2) NOT NULL,
serye varchar(4) NOT NULL,
PRIMARY KEY (rec_no)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


– Dumping data for table invoice_data

INSERT INTO invoice_data (rec_no, cust_rec, inv_no, description, part_number, fob_price, qty, total_fob, trading_price, amount, lock_inv, company_code, taon, buwan, serye) VALUES
(2, 14, ‘’, ‘PO#949100’, ‘101316729’, 0, 0.600, 0, 8.990, 5.390, 1, ‘LOJ’, ‘2014’, ‘06’, ‘’);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/
!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/
!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
[/code]

I did what you said modifying the code. however the result will be “array” instead of a code.

[php]
$cust_code=$_POST[‘customer’] ;

$cust_search = mysqli_query($con,“SELECT inv_code FROM customer_info WHERE rec_no=’$cust_code’”);
if(mysqli_num_rows($cust_search) != 0) {

//$cust_code2 = mysqli_fetch_array($cust_search);
$cust_company = mysqli_fetch_array($cust_search);
//$cust_company = $cust_code2['inv_code'];
[/php]

The SQL dump is exactly what you posted in the previous post.

Array is exactly where you should be at. Now you just need to use the value in the array after your fetch array .

$cust_company [‘inv_code’]

Rewriting the program it should be like this then?

Please check the green fonts. These are the modified portion however i am not sure if this mysqli_query will work.

$cust_code=$_POST[‘customer’] ;

$cust_search = mysqli_query($con,“SELECT * FROM customer_info WHERE rec_no=’$cust_code’”);
if(mysqli_num_rows($cust_search) != 0) {

$cust_company = mysqli_fetch_array($cust_search);
  	
$petsa_taon = date("Y");
mysqli_query($con,"INSERT INTO invoice_number(company, taon) 
		VALUES ('$cust_company['inv_code']', '$petsa_taon') ");

Run it and see what happens.

It did not work. I got a blank page upon clicking create button. That means there is an error on the coding.

Make sure you turn on error reporting and check your error logs. PHP is more than happy to tell you what’s wrong.

Thank you for this suggestion. This really help me a lot and ease my error debugging.

Here is the error
Parse error: syntax error, unexpected ‘"’, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/p1_add_inv.php on line 39

Here is whats in line 39
‘$cust_company[“inv_code”]’,

Ok good, now I will give you the answer. I thought it was better for you to learn about error reporting before I gave you the answer.

‘{$cust_company[‘inv_code’]}’

Thanks so much. This is the one I was looking for.

Thanks Kevin.

May I ask if you can recommend a resources that I can study PHP?
My only resources was w3schools.com

Thanks a lot.

Just do Google searches. There’s plenty out there. I am also available for hire as a personal tutor.

Sponsor our Newsletter | Privacy Policy | Terms of Service