1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

help w/php input

Discussion in 'PHP' started by ataloss, Feb 5, 2014.

  1. #1
    Hi, I'm hoping for a little help with php coding. Following will be my code (skeletonized.)
    Don't laugh, you probably can't spell it either LOL My project is to code up a report with
    the a dark background, I've got this(body with background) using values in input boxes so
    it can be seen ok. I can't get the code right for the values from the database. I was given
    examples but they don't work. I think I've got it if some one can give me a sample(simple LOL).
    I commented a line to show what I'm talking about. I'll be ever so grateful for help.
    <html><body>
    <?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  * FROM oocust WHERE payrec = 'R' && pd = ''";
    $result=mysql_query($query);
    if(mysql_num_rows($result))
      {
    while(list($acctno,$bname)= mysql_fetch_row($result))
      {
      ?>
    <input type=text size=25 value="Bill To:"><br>
    // ** 
      <input type=text size=25 value="<?php echo $bname;?>" /><br>
    // **
      <input type=text size=25 value="Date:<?php echo $mm/dd/yyyy;?>" /><br>
      <input type=text size=25 value="Invoice:<?php echo $acctno;?>" /><br>
    <TABLE BORDER=1 CELLPADDING=20 CELLSPACING=20>
      <TD>
    <input type=text size=25 value="Ship To:"><br> 
    <input type=text size=25 value="<?php echo $sname";
    "</td>"; 
    "</tr>";
    "</table";
      }
      }
    ?>
    </body></HTML>
    PHP:

     
    Last edited by a moderator: Feb 5, 2014
    ataloss, Feb 5, 2014 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    first up you need to update the way you connect to the database. It's deprecated code and won't work for too much longer. I'll leave you to decide how you want to manage that.

    There are a couple of odd things in your code. I'll make some suggestions but you'll need to test them and come back to us with anything that isn't right. FWIW I used php as the bbcode.

    Because there could be multiple lines returned from the query you have to capture the changes in an array. I've assumed some column names.
    
    <html><body><form>
    <?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  * FROM `oocust` WHERE `payrec` = 'R' AND `pd` = '' ";
    $result=mysql_query($query);
    if(mysql_num_rows($result)):
    while($row= mysql_fetch_row($result)):
     ?>
    <label>Name: <input name='bname[<?php echo $row['id'];?>]' type='text' size='25' value="<?php echo $row['bname'];?>" /></label><br>
    
    <label>Date <small>mm/dd/YYYY</small>:  <input name='date[<?php echo $row['id'];?>]' type='text' size='10' value="<?php echo date('m/d/Y); ?>" /></label><br>
    <label>Invoice: <input name='acctno[<?php echo $row['id'];?>]' type='text' size='25' value="<?php echo $row['acctno'];?>" /></label><br>
    <!-- cellpadding and spacing at 20! -->
    <TABLE BORDER=1 CELLPADDING=20 CELLSPACING=20>
      <TD>
    <label>Ship To:<input type=text size=25 value=""></label><br>
    <input type=text size=25 value="<?php echo $sname"; ?>
    </td>
    </tr>
    </table>
    <?php
     endwhile; 
     endif;
    ?>
    </form></body></HTML>
    PHP:
     
    sarahk, Feb 5, 2014 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    @sarahk has it right in that you should stop using mysql_ functions -- we've been told for NINE YEARS to stop using them, they added GIANT RED WARNING BOXES to the manual two years now... yet people are STILL writing new code using them?!? :/

    Your markup is just plain gibberish... I've no idea why you seem to have a 'table for nothing' in there, much less using INPUT to do (also as Sarahk changed in her example) LABEL's job...

    Since you have multiple elements in the same form, they all need a unique name -- I'm going to assume that the invoice / acctno is unique and doesn't occur more than once in the database. If it does not, you should have a separate field in the database for that. I'm also going to assume that the fields are named acctno and bname since you're using 'list' to make extra variables for nothing other than wasting memory. You don't even have NAME attributes on your form...

    Actually, even making those assumptions I can't make heads nor tails out of what the devil you're trying to do there... since you're pulling the current date; have no NAME attributes, are plugging values in with a most nonsensical manner...

    Could you better explain it, including the table structure of 'oocust' (talk about uselessly gibberish and vague table names) and what you expect this form to even DO? Code for the form handler when you submit wouldn't hurt either. As it sits, there's not enough here to even try cleaning it up. You didn't even include where $sname is set...

    Much less some of those values, like the account number, probably dont' even belong in an INPUT... Or even a FORM with an ACTION attribute!!!
     
    deathshadow, Feb 6, 2014 IP
  4. WebDeveloperSahil

    WebDeveloperSahil Active Member

    Messages:
    331
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Use PDO instead of mysql_ functions. That's way better and Object oriented
     
    WebDeveloperSahil, Feb 6, 2014 IP
  5. ataloss

    ataloss Active Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #5
    Wow! I wonder if I need to get and use a manual...I think I will. Just an observation-there I just do this as a pastime, was away having fun for those 9 years, and if assumptions are made in lieu of perusing the post....Ok, I know the code is antiquated and for sure with errors. I thank you for your candor however tart. And thanks to Sarak! I did state that the had been stripped down , not being verbose myself. I didn't think you'd need an explanation
    re. names. I was puzzled re. php values in the html code. The table, actually 3 tables was an exercise to try to accomplish what I wanted to eventually see. and friend I really don't think my "uselessly gibberish and vague table names" are as nonsensical as "deathshadow"?
    Just my opinion, following are what I humbly consider constructive criticism :


    The rest of your post is unwarranted (gibberish)
    Again, thanks
     
    ataloss, Feb 6, 2014 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #6
    You don't need to "get" a manual - just go to www.php.net
     
    sarahk, Feb 6, 2014 IP
  7. ataloss

    ataloss Active Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #7
    thanks a bunch
     
    ataloss, Feb 6, 2014 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    Deathshadow is terse and unfriendly in his demeanor, but he's right on everything he says. Coding "for fun" is the best way to learn, by all means, but at the same time it's also one of the least effective ways to learn how to code well - since one often only tries to accomplish the bare minimum, and rarely bothers to do some research and find better ways to do the things one is trying to do.
    The first thing you should do is read up on HTML and CSS. The presentational bit of what you wanna accomplish. When you've got that pinned down (more or less), you should start adding processing code to do the actions you want on the input you want, using PHP.
    And, when reading on (for instance) php.net, do pay attention to anything saying "Deprecated" - don't use those functions, but look up those mentioned as replacements instead. That will automatically make your code less prone to attacks and "hacks", even only by a tiny, little fraction.
     
    PoPSiCLe, Feb 7, 2014 IP
  9. ataloss

    ataloss Active Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #9
    Thanks PoPSiCLe:
    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 critism:
    And not to worry; nothing is thrown at me that doesn't back.
    <html><body background="oldorchard.jpg"><center>
    HTML:
    
    <?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
      }
      }
    ?>
    PHP:
    </form></body></HTML>
    HTML:
     
    ataloss, Feb 7, 2014 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    Uhm... define "works" -- do you REALLY intend for those static names like:
    value="Terms - <?php echo $terms;?>"

    to ACTUALLY be part of the value when you submit that form? Do you even HAVE a submit handler? (I don't see an action on your forms) -- should that even BE a form?

    I'm starting to wonder that -- if you're using form elements on non-form data as you have no action handler and aren't building those inputs to, well... do what inputs DO.

    Also, does it make any sense whatsoever to be passing $city to your query string? What's that set to and is that intentional? (seems unlikely). Are you cutting down that markup from a larger sample because you've got some real nonsense in there like opening a paragraph for nothing and a bold tag for nothing, which you never close.

    You REALLY should spend some time learning more HTML and modern techniques before diving into trying to do things in PHP. I'm getting the feeling you don't even know what an INPUT is for given what you're plugging into them.

    Oh, and when I said 'name on form' I meant form ELEMENTS (input, textarea, select, button)... Every form element should have a NAME so you can submit it with ACTION attribute on the FORM. NAME doesn't belong on the FORM tag itself unless you're using 1990's style javascript. Really what you are doing is starting to strike me as NOT being what FORMs and INPUT are even FOR!

    You do know that FORM/INPUT are for users to input data to be sent to the server, and are NOT for just showing data results, right? I think that's the logic disconnect here as what you're plugging into the inputs is just... bizarre. I mean are you planning on even having an ACTION handler for that?
     
    Last edited: Feb 7, 2014
    deathshadow, Feb 7, 2014 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #11
    Oh, and your 'date' not working?

    <?php echo $mm/dd/yyyy;?>

    Do we see a problem here? $mm isn't defined, and /dd/yyyy is invalid, it should be bombing right there unless it's magically assuming that's a string or something. Since you're formatting your date, did you mean that to be:

    <?php echo $date; ?>

    Instead?

    Though really, STOP making extra variables for nothing.

    <? echo date('D M d Y g:ia', time(); ?>

    functionally identical, letting you axe two "vars for nothing" in $format and $date.

    Same goes for that bizarro-land extra variables made by that LIST statement.

    Oh, and your not getting errors? You turn error reporting on full blast, but then on the very next line disable it? Which do you want?!?
     
    deathshadow, Feb 7, 2014 IP
  12. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #12
    Listen to @deathshadow, he knows his stuff.

    I'd like to see a tidier way of handling

    while(list( $acctno,$pd,$payrec,$orderno,
    $bname,$bstreet,$bcity,$bstate,$bzip,
    $sname,$sstreet,$scity,$sstate,$szip,
    $terms,$duedate)= mysql_fetch_row($result))
    Code (markup):
    The moment you add a new column to the query you have to update that statement as well as everything in the form.

    while( $row = mysql_fetch_row($result))
    Code (markup):
    is cleaner, easier to read and maintain and you can refer to the contents as $row['acctno']

    <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">
    Code (markup):
    these lines will be displaying that text in fields on the form but they won't be saved because they are missing the "name" attribute, they should, perhaps be disabled so that users don't think that if they change them the change will be saved.
    What purpose are they meant to be serving?
     
    sarahk, Feb 7, 2014 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #13
    Yeah, I just looked deeper at your latest -- what makes these two:
    <input type="text" size="25" value="Bill To"><br />
    and
    <input type="text" size= 25 value="Ship To">

    Form inputs the user is going to EDIT and send back to the server changed?!? I think you don't know what inputs are and/or are for. (those look more like they should be LEGEND inside a FIELDSET)... the more I look at it the more I think you're building a form and using input without it being... what belongs in form and inputs. Is this just output, not an actual edit form the user is supposed to change values and send back to the server? If so that's why nobody here understands what your trying to d!
     
    deathshadow, Feb 7, 2014 IP
  14. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #14
    That's what I'm wondering -- about ALL the input; if they don't have names, and the form doesn't have ACTION, and there's no SUBMIT... why is this even a FORM filled with INPUT?
     
    deathshadow, Feb 7, 2014 IP
  15. ataloss

    ataloss Active Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #15
    I see now where some (most) of the confusion is - the only I'm using "input" is to make the text & values visable on the background.
    If you run the file using the two dave pic.jpg oldorchard.jpg images here you'll see the page and see what I'm up to.
    My son-in-law is Dave, a plumber doing business as Oldorchard. I know this is basic and you may see it as weird and I wish I had
    explained my objective better at the same time I think you have been into my reasoning at least as much as the reason for my post.
    I appreciate more than you know your interest in good correct but I really need to know what my error not that the error is present .
    The date ...that was an offering from an "expert" on another forum. I have been juggling between 4 offering trying to come up with
    the most reasonable. I'm asking you to do this - go back to your first reply to my first post and let it sink in - how much can you say
    could be helpful to me?
     
    ataloss, Feb 7, 2014 IP