Php input boxes

I realize that code I used in the past finds disfavor with many
and I’m trying to improve. However if it works… Here’s the code
to my current work. My problem is that only one line generates value
from my database (I’ve confirmed the database field). I’ve commented
the line that works. If I substitute that line in another it works but
editing the relocated line doesn’t work. Also the date doesn’t work.
I get no error messages. I’ll appreciate any constructive criticism:

<html><body background="oldorchard.jpg"><center>
<?php
 error_reporting(E_ALL ^ E_NOTICE);
error_reporting(0);
mysql_connect('localhost','root','');
mysql_select_db('homedb') or die("Unable to select database");
$query="SELECT acctno,pd,payrec,orderno,
bname,bstreet,$city,bstate,bzip,
sname,sstreet,scity,sstate,szip,
terms,duedate FROM oocust WHERE payrec = 'R' && pd = ''";
$result=mysql_query($query);
if(mysql_num_rows($result))
    {
while(list( $acctno,$pd,$payrec,$orderno,
$bname,$bstreet,$bcity,$bstate,$bzip,
$sname,$sstreet,$scity,$sstate,$szip,
$terms,$duedate)= mysql_fetch_row($result))
   {
<!--the below code is for the date-->
	$format = "D M d Y g:ia";
	$date = date($format, time());
<!--the above code is for the date-->
?>
<form name='form'>
<input type=text size=25 value="Old Orchard Plumbing">
<input type=text size=25 value="2210 E. Hogan Hollow Road">
<input type=text size=25 value="Margate, Fl  33063"><p>
<!--?-->
<input type=text size=25 value="Date - <?php echo $mm/dd/yyyy;?>" />
<!--?-->
<!--the below line inserts the database field-->
   <input type=text size=25 value="Acct# - <?php echo $acctno;?>" /><br>
<!--the above line inserts the database field-->

<TABLE BORDER=0 CELLPADDING=10 CELLSPACING=10>
      <TD>
<p><label for="bname"><b>
<input type=text size=25 value="Bill To"><br>
       <input type=text size=25 value="Name<?php echo $bname;?>" /><br>
<input type=text size=25 value="Street - <?php echo $bstreet;?>" /><br>
    <input type=text size=25 value="City - <?php echo $bcity;?>" /><br>
  <input type=text size=25 value="State - <?php echo $bstate;?>" /><br>
      <input type=text size=25 value="Zip - <?php echo $bzip;?>" />
     </TD>
   <td>
<p><label for="sname"><b>
   <input type=text size= 25 value="Ship To"><br>
    <input type=text size=25 value="Name - <?php echo $sname;?>" /><br>
<input type=text size=25 value="Street - <?php echo $sstreet;?>" /><br>
    <input type=text size=25 value="City - <?php echo $scity;?>" /><br>
  <input type=text size=25 value="State - <?php echo $sstate;?>" /><br>
      <input type=text size=25 value="Zip - <?php echo $szip;?>" />
     </TD>
      <TR>
     <TD>
     <input type=text size=25 value="Terms - <?php echo $terms;?>" /><br>
<input type=text size=25 value="Due Date - <?php echo $duedate;?>" /><br>
  <input type=text size=25 value="order# - <?php echo $orderno;?>" />
     </td>
    <TD>
<img src="dave pic.jpg" width=175 height=200>
    </td>
   </tr>
    </table>
  <?php
    }
    }
?>
</form></body></HTML>

Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.

So how’s it going solving your problem with your code? Do you really have a table field for every city that might be called, i.e. $city ?

I have never used list() in a WHILE loop and so I’m not sure if that section even works in this case.
As far as the date section, you need to get rid of those html style comments in the php and use // before the line of text.

// the below code is for the date
    $format = "D M d Y g:ia";
    $date = date($format, time());
// the above code is for the date

Then in your form echo the variable $date where you want the date.

Also, most if not all your form input fields are missing the name attribute, e.g. name=“date” value=“<?php echo $date; ?>”

You are also prefilling your form values with text, e.g. Acct# - , which is also going to cause issues with processing the form to strip out this text.

The </form> tag is also outside the query loop while the start tag is inside the loop. Actually there are so many issues I will stop here.

The reason there are no error messages is because the first line switches errors on and the second line switches errors off.

Try this:


error_reporting(E_ALL ^ E_NOTICE);
// error_reporting(0);
ini_set('display_errors', true); // maybe not required if previously set in phpini.php

// test start
   echo $x / 0; 
   // expected results
   // Notice:  Undefined variable: x in C:\\xampp\\htdocs\\XXX\\index.php on line 5
   // Warning:  Division by zero in C:\\xampp\\htdocs\\XXX\\index.php on line 5
// test finish


For what it’s worth, here’s a quick redo of your project. I don’t usually mix queries in html like this but wanted to keep things close to as you had it. The code uses $_GET[‘city’] to trigger query.

<?php
$host = "localhost";
//Database user name.	
$login = "";
//Database Password.
$dbpass = "";
//Database name.
$dbname = "";
$PDO = new PDO("mysql:host=localhost;dbname=$dbname", "$login", "$dbpass");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body background="oldorchard.jpg"><center>
<form name='form' action="" method="post">
<?php
if(isset($_GET['city'])){
	$city = trim($_GET['city']);
	$R = "R";
	
	try{
		$sql = "SELECT
		acctno,pd,payrec,orderno,
		bname,bstreet,bcity,bstate,bzip,
		sname,sstreet,scity,sstate,szip,
		terms,duedate
		FROM oocust
		WHERE bcity = :bcity AND payrec = :R AND pd = ''";
		$query = $PDO->prepare($sql);
		$query->bindParam(":bcity", $city);
		$query->bindParam(":R", $R);
		$query->execute();
		while($row = $query->fetch(PDO::FETCH_ASSOC)){
			$acctno = $row['acctno'];
			$pd = $row['pd'];
			$payrec = $row['payrec'];
			$orderno = $row['orderno'];
			$bname = $row['bname'];
			$bstreet = $row['bstreet'];
			$bcity = $row['bcity'];
			$bstate = $row['bstate'];
			$bzip = $row['bzip'];
			$sname = $row['sname'];
			$sstreet = $row['sstreet'];
			$scity = $row['scity'];
			$sstate = $row['sstate'];
			$szip = $row['szip'];
			$terms = $row['terms'];
			$duedate = $row['duedate'];

			// the below code is for the date
		    $format = "D M d Y g:ia";
		    $date = date($format, time());
			// the above code is for the date
?>
			<p>
			<input type="text" name="Name[]" size="25" value="Old Orchard Plumbing" />
			<input type="text" name="Address1[]" size="25" value="2210 E. Hogan Hollow Road" />
			<input type="text" name="Address2[]" size="25" value="Margate, Fl  33063" />
			</p>
			<p>
			<input type="text" name="Date[]" size="25" value="Date - <?php echo $date;?>" />
			<input type="text" name="AcctNumber[]" size="25" value="Acct# - <?php echo $acctno;?>" />
			</p>
			
			
			<table border=0 cellpadding=10 cellspacing=10>
				<tr>
					<td>
						<input type="text" name="Bill To[]" size="25" value="Bill To" /><br />
						<input type="text" name="bName[]" size="25" value="Name<?php echo $bname;?>" /><br />
						<input type="text" name="bStreet[]" size="25" value="Street - <?php echo $bstreet;?>" /><br />
						<input type="text" name="bCity[]" size="25" value="City - <?php echo $bcity;?>" /><br />
						<input type="text" name="bState[]" size="25" value="State - <?php echo $bstate;?>" /><br />
						<input type="text" name="bZip[]" size="25" value="Zip - <?php echo $bzip;?>" />
					</td>
					<td>
						<input type="text" name="Ship To[]" size="25" value="Ship To" /><br />
						<input type="text" name="sName[]" size="25" value="Name - <?php echo $sname;?>" /><br />
						<input type="text" name="sStreet[]" size="25" value="Street - <?php echo $sstreet;?>" /><br />
						<input type="text" name="sCity[]" size="25" value="City - <?php echo $scity;?>" /><br />
						<input type="text" name="sState[]" size="25" value="State - <?php echo $sstate;?>" /><br />
						<input type="text" name="sZip[]" size="25" value="Zip - <?php echo $szip;?>" />
					</td>
				<tr>
					<td>
						<input type="text" name="Terms[]" size="25" value="Terms - <?php echo $terms;?>" /><br />
						<input type="text" name="DueDate[]" size="25" value="Due Date - <?php echo $duedate;?>" /><br />
						<input type="text" name="OrderNumber[]" size="25" value="order# - <?php echo $orderno;?>" />
					</td>
					<td>
						<img src="davepic.jpg" width=175 height=200 />
					</td>
				</tr>
			</table>
<?php
		}
	
	}catch (PDOException $e){
		echo "Database error: ".$e->getMessage();
	}
}
?>
</form>
</center>
</body>
</html>

Thanks for your input. Drummin. I like your structured code and am already planning it’s use in further code. I must admit though that as yet I haven’t fully grasped the entirety. Following is my attempt to make it work for me. Thus far I only get the background.

<?php 
$host = "localhost";  
//Database user name.     
$login = "root"; 
//Database Password. 
$dbpass = ""; 
//Database name. 
$dbname = "homedb"; 
$PDO = new PDO("mysql:host=localhost;dbname=$dbname", "$login", "$dbpass"); 

?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<title></title> 
</head> 
<body background="oldorchard.jpg"><center>  
<form name='form' action="" method="post"> 
<?php 
if(isset($_GET['acctno'])){ 
    $acctno = trim($_GET['acctno']); 
    $R = "R"; 
     
    try{ 
        $sql = "SELECT  
        acctno,pd,payrec,orderno, 
        bname,bstreet,bcity,bstate,bzip, 
        sname,sstreet,scity,sstate,szip, 
        terms,duedate 
        FROM oocust  
        WHERE acctno = :acctno AND payrec = :R AND pd = ''"; 
        $query = $PDO->prepare($sql); 
        $query->bindParam acctno", $acctno);  
        $query->bindParam(":R", $R); 
        $query->execute();  
        while($row = $query->fetch(PDO::FETCH_ASSOC)){ 
            $acctno = $row['acctno']; 
            $pd = $row['pd']; 
            $payrec = $row['payrec']; 
            $orderno = $row['orderno'];  
            $bname = $row['bname']; 
            $bstreet = $row['bstreet']; 
            $bcity = $row['bcity']; 
            $bstate = $row['bstate']; 
            $bzip = $row['bzip']; 
            $sname = $row['sname']; 
            $sstreet = $row['sstreet']; 
            $scity = $row['scity']; 
            $sstate = $row['sstate']; 
            $szip = $row['szip']; 
            $terms = $row['terms']; 
            $duedate = $row['duedate']; 

            // the below code is for the date 
            $format = "D M d Y g:ia"; 
            $date = date($format, time()); 
            // the above code is for the date 
?> 
            <p> 
            <input type="text" name="Name[]" size="25" value="Old Orchard Plumbing" /> 
            <input type="text" name="Address1[]" size="25" value="2210 E. Hogan Hollow Road" /> 
            <input type="text" name="Address2[]" size="25" value="Margate, Fl  33063" /> 
            </p> 
            <p> 
            <input type="text" name="Date[]" size="25" value="Date - <?php echo $date;?>" /> 
            <input type="text" name="AcctNumber[]" size="25" value="Acct# - <?php echo $acctno;?>" /> 
            </p> 
             
             
            <table border=0 cellpadding=10 cellspacing=10> 
                <tr> 
                    <td> 
                        <input type="text" name="Bill To[]" size="25" value="Bill To" /><br /> 
                        <input type="text" name="bName[]" size="25" value="Name<?php echo $bname;?>" /><br /> 
                        <input type="text" name="bStreet[]" size="25" value="Street - <?php echo $bstreet;?>" /><br /> 
                        <input type="text" name="bCity[]" size="25" value="City - <?php echo $bcity;?>" /><br /> 
                        <input type="text" name="bState[]" size="25" value="State - <?php echo $bstate;?>" /><br /> 
                        <input type="text" name="bZip[]" size="25" value="Zip - <?php echo $bzip;?>" /> 
                    </td> 
                    <td> 
                        <input type="text" name="Ship To[]" size="25" value="Ship To" /><br />  
                        <input type="text" name="sName[]" size="25" value="Name - <?php echo $sname;?>" /><br /> 
                        <input type="text" name="sStreet[]" size="25" value="Street - <?php echo $sstreet;?>" /><br /> 
                        <input type="text" name="sCity[]" size="25" value="City - <?php echo $scity;?>" /><br /> 
                        <input type="text" name="sState[]" size="25" value="State - <?php echo $sstate;?>" /><br /> 
                        <input type="text" name="sZip[]" size="25" value="Zip - <?php echo $szip;?>" /> 
                    </td> 
                <tr>        
                    <td> 
                        <input type="text" name="Terms[]" size="25" value="Terms - <?php echo $terms;?>" /><br />     
                        <input type="text" name="DueDate[]" size="25" value="Due Date - <?php echo $duedate;?>" /><br /> 
                        <input type="text" name="OrderNumber[]" size="25" value="order# - <?php echo $orderno;?>" /> 
                    </td> 
                    <td> 
                        <img src="davepic.jpg" width=175 height=200 /> 
                    </td>  
                </tr> 
            </table> 
<?php   
        }  
     
    }catch (PDOException $e){ 
        echo "Database error: ".$e->getMessage(); 
    } 
} 
?> 
</form></center></body></html>

What is up with your GET value???

if(isset($_GET['[B]acctno[/B]'])){
    $[B]acctno[/B] = trim($_GET['[B]acctno[/B]']); 

I would think you’d call the account number like so.

pagename.php?acctno=12

So the actual GET would be

if(isset($_GET['acctno'])){
    $acctno = trim($_GET['acctno']);

Then the query

        $sql = "SELECT
        acctno,pd,payrec,orderno,
        bname,bstreet,bcity,bstate,bzip,
        sname,sstreet,scity,sstate,szip,
        terms,duedate
        FROM oocust
        WHERE acctno = :acctno AND payrec = :R AND pd = ''";
        $query = $PDO->prepare($sql);
        $query->bindParam(":acctno", $acctno); 

EDIT: Now it looks like your post was changed…

        $query->bindParam acctno", $acctno); 

Should be

        $query->bindParam(":acctno", $acctno);

What are you planning on doing with the output of this form?

Just noting that fixing this line

$query->bindParam(":acctno", $acctno);

results in successful query.

I’ve had that ? a few times, or I should say that was questioned . My son-in law is a plumber and this is an invoice for his billing. The inputs are only for readability. The document inserts values from a database for relative acct#s. (payrec) is a/r or a/p and (pd) is “P”(paid) or " " (unpaid)… Honestly, if it was simple it wouldn’t interest. me. .I really do thank you for your help. Only when I’m stumped do I seek help. Again thanks

OK, so your going to call up a list of all unpaid accounts, then call up one account using code above and print the page or something. That’s cool.
Then you’d probably wish to add a button to mark account as paid after receiving payment. Simple enough. Let us know how it goes. You did get the query above to work, right?

U got the idea without yelling at me LOL. Actually not selecting an account but printing all due and yes I update the acct by placing a “P” in
the field “pd”. I earnestly would like to learn your manner of coding but I have yet to get more than the background. I’ll keep sluggin’

Really? Still not showing?

All DB table fields match what you have?
acctno, pd, payrec, orderno, bname, bstreet, bcity, bstate, bzip, sname, sstreet, scity, sstate, szip, terms, duedate

Can you POST your current full page?

The document inserts values from a database for relative acct#s. (payrec) is a/r or a/p and (pd) is “P”(paid) or " " (unpaid)…
It’s not a problem with Case is it? In this quote you’re saying accounts are marked with (a or r) but in our example we use uppercase R. Or is it supposed to be literal a/r

No I very seldom use caps except but did this intentionally.
I’m stuck at this: below the code is the table

Parse error: syntax error, unexpected ‘acctno’ (T_STRING) in C:\xampp\htdocs\invoice\billing.php on line 34


<?php 
$host = "localhost";  
//Database user name.     
$login = "root"; 
//Database Password. 
$dbpass = ""; 
//Database name. 
$dbname = "homedb"; 
$PDO = new PDO("mysql:host=localhost;dbname=$dbname", "$login", "$dbpass"); 

?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<title>Invoice</title> 
</head> 
<body background="oldorchard.jpg"><center>  
<form name='form' action="" method="post"> 
<?php 
if(isset($_GET['acctno'])){ 
    $acctno = trim($_GET['acctno']); 
    $R = "R"; 
     
    try{ 
        $sql = "SELECT  
        acctno,pd,payrec,orderno, 
        bname,bstreet,bcity,bstate,bzip, 
        sname,sstreet,scity,sstate,szip, 
        terms,duedate 
        FROM oocust  
        WHERE acctno = :acctno AND payrec = :R AND pd = ''"; 
        $query = $PDO->prepare($sql); 
        $query->bindParam acctno", $acctno);  // line 34
        $query->bindParam(":R", $R); 
        $query->execute();  
        while($row = $query->fetch(PDO::FETCH_ASSOC)){ 
            $acctno = $row['acctno']; 
            $pd = $row['pd']; 
            $payrec = $row['payrec']; 
            $orderno = $row['orderno'];  
            $bname = $row['bname']; 
            $bstreet = $row['bstreet']; 
            $bcity = $row['bcity']; 
            $bstate = $row['bstate']; 
            $bzip = $row['bzip']; 
            $sname = $row['sname']; 
            $sstreet = $row['sstreet']; 
            $scity = $row['scity']; 
            $sstate = $row['sstate']; 
            $szip = $row['szip']; 
            $terms = $row['terms']; 
            $duedate = $row['duedate']; 

            // the below code is for the date 
            $format = "D M d Y g:ia"; 
            $date = date($format, time()); 
            // the above code is for the date 
?> 
            <p> 
            <input type="text" name="Name[]" size="25" value="Old Orchard Plumbing" /> 
            <input type="text" name="Address1[]" size="25" value="2210 E. Hogan Hollow Road" /> 
            <input type="text" name="Address2[]" size="25" value="Margate, Fl  33063" /> 
            </p> 
            <p> 
            <input type="text" name="Date[]" size="25" value="Date - <?php echo $date;?>" /> 
            <input type="text" name="AcctNumber[]" size="25" value="Acct# - <?php echo $acctno;?>" /> 
            </p> 
             
             
            <table border=0 cellpadding=10 cellspacing=10> 
                <tr> 
                    <td> 
                        <input type="text" name="Bill To[]" size="25" value="Bill To" /><br /> 
                        <input type="text" name="bName[]" size="25" value="Name<?php echo $bname;?>" /><br /> 
                        <input type="text" name="bStreet[]" size="25" value="Street - <?php echo $bstreet;?>" /><br /> 
                        <input type="text" name="bCity[]" size="25" value="City - <?php echo $bcity;?>" /><br /> 
                        <input type="text" name="bState[]" size="25" value="State - <?php echo $bstate;?>" /><br /> 
                        <input type="text" name="bZip[]" size="25" value="Zip - <?php echo $bzip;?>" /> 
                    </td> 
                    <td> 
                        <input type="text" name="Ship To[]" size="25" value="Ship To" /><br />  
                        <input type="text" name="sName[]" size="25" value="Name - <?php echo $sname;?>" /><br /> 
                        <input type="text" name="sStreet[]" size="25" value="Street - <?php echo $sstreet;?>" /><br /> 
                        <input type="text" name="sCity[]" size="25" value="City - <?php echo $scity;?>" /><br /> 
                        <input type="text" name="sState[]" size="25" value="State - <?php echo $sstate;?>" /><br /> 
                        <input type="text" name="sZip[]" size="25" value="Zip - <?php echo $szip;?>" /> 
                    </td> 
                <tr>        
                    <td> 
                        <input type="text" name="Terms[]" size="25" value="Terms - <?php echo $terms;?>" /><br />     
                        <input type="text" name="DueDate[]" size="25" value="Due Date - <?php echo $duedate;?>" /><br /> 
                        <input type="text" name="OrderNumber[]" size="25" value="order# - <?php echo $orderno;?>" /> 
                    </td> 
                    <td> 
                        <img src="davepic.jpg" width=175 height=200 /> 
                    </td>  
                </tr> 
            </table> 
<?php   
        }  
     
    }catch (PDOException $e){ 
        echo "Database error: ".$e->getMessage(); 
    } 
} 
?> 
</form></center></body></html>

– phpMyAdmin SQL Dump
– version 4.0.4.1
http://www.phpmyadmin.net

– Host: localhost
– Generation Time: Jan 29, 2014 at 10:16 PM
– Server version: 5.5.32
– PHP Version: 5.4.16

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: homedb



– Table structure for table oocust

CREATE TABLE IF NOT EXISTS oocust
(
id int(5) NOT NULL AUTO_INCREMENT,

acctno varchar(15) NOT NULL,

status varchar(1) NOT NULL,

pd varchar(1) NOT NULL,

payrec varchar(1) NOT NULL,

orderno int(5) NOT NULL,

bname varchar(25) NOT NULL,

bstreet varchar(25) NOT NULL,

bcity varchar(15) NOT NULL,

bstate varchar(15) NOT NULL,

bzip varchar(12) NOT NULL,

bemail varchar(12) NOT NULL,

phone varchar(12) NOT NULL,

contact varchar(15) NOT NULL,

sname varchar(25) NOT NULL,

sstreet varchar(25) NOT NULL,

scity varchar(15) NOT NULL,

sstate varchar(15) NOT NULL,

szip varchar(12) NOT NULL,

semail varchar(12) NOT NULL,

terms varchar(5) NOT NULL,

fob varchar(11) NOT NULL,

shipdate date NOT NULL,

shipamt decimal(5,2) NOT NULL,

dateord date NOT NULL,

datecomp date NOT NULL,

duedate date NOT NULL,

qty int(5) NOT NULL,

descr varchar(25) NOT NULL,

payable decimal(5,2) NOT NULL,

tax decimal(4,2) NOT NULL,

paidamt decimal(5,2) NOT NULL,

datepaid date NOT NULL,

dayslate int(4) NOT NULL,

checkno varchar(5) NOT NULL,

amtdue decimal(5,2) NOT NULL,

prevbal decimal(5,2) NOT NULL,

balance decimal(5,2) NOT NULL,

PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;


– Dumping data for table oocust

INSERT INTO oocust
(id,
acctno,
status,
pd,
payrec,
orderno,
bname,
bstreet,
bcity,
bstate,
bzip,
bemail,
phone,
contact,
sname,
sstreet,
scity,
sstate,
szip,
semail,
terms,
fob,
shipdate,
shipamt,
dateord,
datecomp,
duedate,
qty,
descr,
payable,
tax,
paidamt,
datepaid,
dayslate,
checkno,
amtdue,
prevbal,
balance) VALUES

Should be

        $query->bindParam(":acctno", $acctno);  // line 34