www.webdeveloper.com
Results 1 to 12 of 12

Thread: pdo code

  1. #1
    Join Date
    Apr 2013
    Posts
    39

    pdo code

    Hi all, I need to replace much of my fossilized code. can someone suggest correct pdo code for the below?
    PHP Code:
    <?php
    include("getnumbers.php");
    error_reporting(0);
    mysql_connect('localhost','root','');
    mysql_select_db('homedb') or die("Unable to select database");
    $query=" SELECT * FROM oocust WHERE id = '$id'";
    $result=mysql_query($query);
    $num=mysql_numrows($result);

  2. #2
    Join Date
    Jul 2013
    Posts
    351
    I could, but if you need help with the connect and the select in addition to just plain querying, I suggest you read up on PDO first.

    The manual has some good stuff on this.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    You'll also want to decide if you are going to use OOP or procedural style coding. (All the cool developers use the OOP form, of course.)

    Here's a brief example of the basics you'd need to execute a query (using a prepared statement): http://www.phpeveryday.com/articles/...ment-P550.html (although I'd personally name the PDO object $pdo instead of $conn, just to add readability).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Apr 2013
    Posts
    39
    Thanks NogDog & ginerjm. Would u please lok at this code. I'm past the error messages now but get a blank page ?
    <?php
    include ('getnumbers.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

    $tax = $payable * $taxrate;
    $amtdue = $payable + $tax + $shipamt;
    $currdate = date('Y-m-d');
    $date1 = $duedate;
    $date2 = $currdate;
    $diff = abs(strtotime($date2) - strtotime($date1));
    $years = floor($diff / (365*60*60*24));
    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
    $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
    // echo "Difference is $months months, $days days.";
    $dayslate = $months *30;
    $dayslate = $dayslate + $days;
    $query = "
    INSERT INTO oocust (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 ('$acctno',$status','$pd','$payrec','$orderno','$bname','$bstreet','$bcity',
    '$bstate','$bzip','$bemail','$phone','$contact','$sname','$sstreet','$sscity','$sstate',
    '$szip','$semail','$terms','$fob','$shipdate','$shipamt','$dateord','$datecomp',
    '$duedate','$qty','$descr','$payable','$tax','$paidamt','$datepaid','$dayslate',
    '$checkno,'$amtdue','$prevbal,','$balance')";

    }

    }catch (PDOException $e){
    echo "Database error: ".$e->getMessage();
    }
    }
    ?>

    </form></center></body></html>
    Last edited by 12Strings; 02-26-2014 at 02:06 PM.

  5. #5
    Join Date
    Jul 2013
    Posts
    351
    Can we see the new code now?

  6. #6
    Join Date
    Jul 2013
    Posts
    351
    Ok - the first time I came to see your message, the code was not yet there.

    BTW - CAN YOU FOLLOW THE RULES AND USE THE CODE/PHP TAGS TO HELP MAKE YOUR FORMATTED CODE LOOK MORE PRESENTABLE?

    So now that I can read your code in my own editor, tell me what error to look for other than the query you never execute.
    BTW - if you turn on error checking you may see some errors that are the cause of a blank page.

  7. #7
    Join Date
    Jul 2013
    Posts
    351
    ACTUALLY - your script is probably working perfectly! You don't output anything from the script as it sits right now, so success at least means you won't see anything.

  8. #8
    Join Date
    Apr 2013
    Posts
    39
    hi guys, take a break from the tough stuff.
    I could use some help here. I don't understand this one. Below is the 'fetch_taxrate'
    code.

    PHP Code:
    <?php
    $taxrate
    =$_POST['search_term'];
    $stat mysql_connect('localhost','root','');
    $stat mysql_select_db('homedb');
    $query "SELECT taxrate FROM numbers Where taxrate='$taxrate'";
    $stat = @mysql_fetch_assoc(mysql_query($query));
    echo 
    $stat["taxrate"];
    ?>
    Below is the 'numbers' sql

    id receiptno orderno taxrate bank

    1 154 150 0.575 999.99
    Here is the message:
    Warning: require(includes/fetch_taxrate.php): failed to open stream:
    No such file or directory in C:\xampp\htdocs\invoice\invoiceinsert.php on line 101

    Fatal error: require(): Failed opening required 'includes/fetch_taxrate.php'
    (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\invoice\invoiceinsert.php
    on line 101
    PHP Code:
     </script>
    </head>
    <body bgcolor="#ccffff"><center>  
    <input type="text" name="Date[]" size="25" onFocus="this.value=''" value="Date - <?php echo $date;?>  
    <input type="text" name="acctno[]" size="25" STYLE="color: #000000; background-color: #72A4D2;" onFocus="this.value=''" value="Acct#<?php echo $acctno;?><br> 

    <!--Copies "Bill To" info to "Ship To" info.-->
    <form name='form' action="" method="post">
    <?php 
    require "includes/fetch_taxrate.php"
    if(isset(
    $_GET['acctno'])) 
        try
       {  
            
    $sql "SELECT 
    acctno,status,pd,payrec,orderno,bname,bstreet,bcity,bstate,bzip,
    phone,contact,sname,sstreet,scity,sstate,szip,terms,fob,shipdate,
    shipamt,dateord,datecomp,duedate,qty,descr,payable,tax,paidamt,
    datepaid,dayslate,checkno,amtdue,prevbal,balance 
            FROM oocust  WHERE payrec = 'R' && pd = ''"
    ;  
            
    $query $PDO->prepare($sql); 
            
    $query->execute(); 
            while(
    $row $query->fetch(PDO::FETCH_ASSOC)) 
        {  
    $acctno=$_POST['acctno'];
    $status=$_POST['status'];
    $pd=$_POST['pd'];
    $payrec=$_POST['payrec'];
    $orderno=$_POST['orderno'];
    $bname=$_POST['bname'];
    $bstreet=$_POST['bstreet'];
    $bcity=$_POST['bcity'];
    $bstate=$_POST['bstate'];
    $bzip=$_POST['bzip'];
    $bemail=$_POST['bemail'];
    $phone=$_POST['phone'];
    $contact=$_POST['contact'];
    $sname=$_POST['sname'];
    $sstreet=$_POST['sstreet'];
    $scity=$_POST['scity'];
    $sstate=$_POST['sstate'];
    $szip=$_POST['szip'];
    $semail=$_POST['semail'];
    $terms=$_POST['terms'];
    $fob=$_POST['fob'];
    $shipdate=$_POST['shipdate'];
    $shipamt=$_POST['shipamt']; 
    $dateord=$_POST['dateord'];
    $datecomp=$_POST['datecomp'];
    $duedate=$_POST['duedate'];
    $qty=$POST['qty'];
    $descr=$_POST['descr'];
    $payable=$_POST['payable'];
    $tax=$_POST['tax'];
    $paidamt=$_POST['paidamt'];
    $datepaid=$_POST['datepaid'];
    $dayslate=$_POST['dayslate'];
    $checkno=$_POST['checkno'];
    $amtdue=$_POST['amtdue'];
    $prevbal=$_POST['prevbal'];
    $balance=$_POST['balance']; 
        
    $currdate date('Y-m-d'); 
        
    $date1 $duedate
        
    $date2 $currdate
        
    $diff abs(strtotime($date2) - strtotime($date1)); 
        
    $years floor($diff / (365*60*60*24)); 
        
    $months floor(($diff $years 365*60*60*24) / (30*60*60*24));
     
    $days floor(($diff $years 365*60*60*24 $months*30*60*60*24)/ (60*60*24));
     
    // echo "Difference is $months months, $days days 
    $dayslate $months *30// ?
    $dayslate $dayslate $days;  // ?
    $sql2 
    INSERT INTO oocust (acctno,status,pd,payrec,orderno,bname,bstreet,bcity,bstate,bzip,
    phone,contact,sname,sstreet,scity,sstate,szip,terms,fob,shipdate,shipamt,
    dateord,datecomp,duedate,qty,descr,payable,tax,paidamt,datepaid,dayslate,checkno, 
    amtdue,prevbal,balance)
    VALUES ('
    $acctno',$status','$pd','$payrec','$orderno','$bname','$bstreet','$bcity', 
    '
    $bstate','$bzip','$phone','$contact','$sname','$sstreet','$sscity','$sstate', 
    '
    $szip','$terms','$fob','$shipdate','$shipamt','$dateord','$datecomp', 
    '
    $duedate','$qty','$descr','$payable','$tax','$paidamt','$datepaid','$dayslate', 
    '
    $checkno,'$amtdue','$prevbal,','$balance')"
    ?>
    HTML Code:
    function copyAddress() {
       document.Form.sname.value=document.Form.bname.value
       document.Form.sstreet.value=document.Form.bstreet.value
       document.Form.scity.value=document.Form.bcity.value
       document.Form.sstate.value=document.Form.bstate.value
       document.Form.szip.value=document.Form.bzip.value
       document.Form.semail.value=document.Form.bemail.value
    }
    </script>
    <!-- Start of Bill To table -->
    <TABLE><tr>  
    <td> 
    <input type="text" readonly name="BillTo[]" size="25" value="Bill To" STYLE="color: #000000; background-color: #72A4D2;" onFocus="this.value=''"><br> 
    <input type="text" name="bName[]" size="25" onFocus="this.value='' value="Name - <?php echo $bname;?><br>  
    <input type="text" name="bStreet[]" size="25" onFocus="this.value='' value="Street - <?php echo $bstreet;?><br> 
    <input type="text" name="bCity[]" size="25" onFocus="this.value='' value="City - <?php echo $bcity;?><br>  
    <input type="text" name="bState[]" size="25" onFocus="this.value='' value="State - <?php echo $bstate;?><br>  
       <input type="text" name="bZip[]" size="25" onFocus="this.value='' value="Zip - <?php echo $bzip;?><br>  
    <input type="text" name="bemail[]" size="25" onFocus="this.value='' value="Email - <?php echo $bemail;?><br> 
                        </td> 
    <!--End of Bill To table -->
    <!-- Start of Ship To table -->
                        <td> 
    					
    <input type="text" readonly name="ShipTo[]" size="25" value="Ship To" STYLE="color: #000000; background-color: #72A4D2;" onFocus="this.value=''"><br>  
    <input type="text" name="sName[]" size="25" onFocus="this.value='' value="Name - <?php echo $sname;?><br> 
    <input type="text" name="sStreet[]" size="25" onFocus="this.value='' value="Street - <?php echo $sstreet;?><br> 
    <input type="text" name="sCity[]" size="25" onFocus="this.value='' value="City - <?php echo $scity;?><br>  
    <input type="text" name="sState[]" size="25" onFocus="this.value='' value="State - <?php echo $sstate;?><br>  
    <input type="text" name="sZip[]" size="25" onFocus="this.value='' value="Zip - <?php echo $szip;?><br> 
    <input type="text" name="sEmail[]" size="25" onFocus="this.value='' value="Email - <?php echo $semail;?><br>  
                        </td> </td>    
    <!--End of Ship To table -->
    <tr><td>
    <INPUT onclick=copyAddress() type="button" size=35 value="Click if Ship To Address is Same as Bill To">
    </td>
    </TR></TBODY></TABLE><br>
    <hr align=center width=48%>
     <table border=0 cellpadding=10 cellspacing=10>  
    <tr>
    <th>Taxable?</th>
    <th>Recurring?</th> 
    <th>Paid?</th> 
    <th>Account type</th>
    <th>Terms:</th>
    <th>FOB:</th>
    
    <tr>
    <hr align=center width=48%> 
    <td>
    <SELECT name=taxable>
    <OPTION value=""> 
    <OPTION class=highlight value="N" selected>No 
    <OPTION class=highlight value="Y">Yes
    </OPTION></SELECT>
    
    <SELECT name=recur>
    <OPTION value=""> 
    <OPTION class=highlight value="Y" selected>Yes
    <OPTION class=highlight value="N">No
    </OPTION></SELECT>
    
    <SELECT name=pd>
    <OPTION value=""> 
    <OPTION class=highlight value="p" selected>Yes
    <OPTION class=highlight value=" "> No
    </OPTION></SELECT>
    
    <SELECT name=payrec>
    <OPTION value=""> 
    <OPTION class=highlight value=R selected>receivable
    <OPTION class=highlight value=P>payable
    </OPTION></SELECT>
    
     <SELECT name=terms>
    <OPTION value=""> 
    <OPTION class=highlight value=net_10 selected>Net 10
    <OPTION class=highlight value=1_10_30>1 10 Net 30
    <OPTION class=highlight value=2_10_30>2 10 Net 30
    <OPTION class=highlight value=cash>Cash
    
    </OPTION></SELECT>
     <SELECT name=fob>
    <OPTION value=""> 
    <OPTION class=highlight value=Destination selected>Destination
    <OPTION class=highlight value=Origin>Origin
    
    </OPTION></SELECT></td></tr></table><br>
    <hr align=center width=48%>
    DATES <font color="blue">(yyyy-mm-dd)</font><br>
    <input type="text" name="shipdate[]" size="25" onFocus="this.value=''" value="Ship Date - <?php echo $shipdate;?> 
    <input type="text" name="dateord[]" size="25" onFocus="this.value=''" value="Date Ordered - <?php echo $dateord;?>
    <input type="text" name="datecomp[]" size="25" onFocus="this.value=''" value="Date Completed - <?php echo $datecomp;?>
    <input type="text" name="datepaid[]" size="25" onFocus="this.value=''" value="Date Paid - <?php echo $datepaid;?> 
    <input type="text" name="DueDate[]" size="25" onFocus="this.value=''" value="Due Date - <?php echo $duedate;?>
    <hr align=center width=48%>
    <input type="text" name="Orderno[]" size="10" onFocus="this.value=''" value="Order# - <?php echo $orderno;?> 
    <input type="text" name="qty[]" size="5" onFocus="this.value=''" value="qty - <?php echo $qty;?> 
    <input type="text" name="payable[]" size="7" onFocus="this.value=''" value="Charges - <?php echo $payable;?> 
    <input type="text" name="tax[]" size="6" onFocus="this.value=''" value="Tax - <?php echo $tax;?><br> 
    <hr align=center width=48%>
    <input type="text" name="paidamt[]" size=7 onFocus="this.value=''" value="Amt Paid# - <?php echo $paidamt;?> 
    <input type="text" name="checkno[]" size=15 onFocus="this.value=''" value="Check# - <?php echo $checkno;?>  
    <input type="text" name="shipamt[]" size=7 onFocus="this.value=''" value="Ship Amt - <?php echo $shipamt;?><br> 
    <INPUT type="submit" name="submit" value="submit data">
    </tr></table>
    PHP Code:
    <?php
    $query2 
    $PDO->prepare($sql2); 
    $query2->execute(); 
      }  
       } 
    catch (
    PDOException $e)
       {  
            echo 
    "Database error: ".$e->getMessage();  
        }  
    ?>
    HTML Code:
    </form></center></body></html> 

  9. #9
    Join Date
    Jul 2013
    Posts
    351
    Your code is a confusing mass of pieces that we cannot make much sense of.

    As for the error you showed us, you obviously have a problem locating a file that you are trying to include in your script. Fix it.

  10. #10
    Join Date
    Apr 2013
    Posts
    39
    Thanks for the response ginerjim. Yeah I wouldn't expect anyone to follow that load of code, especially from me LOL. All I
    need is someone smarter than me to explain how my statment "require "includes/fetch_taxrate.php"; " failed.

  11. #11
    Join Date
    Jul 2013
    Posts
    351
    Can't tell you why you are getting the message other than it is telling you exactly what it sees as wrong. On a specific line no. in a specific script you have a statement that can not find the path/filename that you asked for. Check your server folders for the filename and then change that path/filename in that require line to point correctly.

  12. #12
    Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles