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.
    PHP:
    1. <html><body>
    2. <?php
    3. // error_reporting(E_ALL ^ E_NOTICE);
    4. mysql_connect('localhost','root','');
    5. mysql_select_db('homedb') or die("Unable to select database");
    6. $query="SELECT  * FROM oocust WHERE payrec = 'R' && pd = ''";
    7. $result=mysql_query($query);
    8. if(mysql_num_rows($result))
    9.   {
    10. while(list($acctno,$bname)= mysql_fetch_row($result))
    11.   {
    12.   ?>
    13. <input type=text size=25 value="Bill To:"><br>
    14. // **
    15.   <input type=text size=25 value="<?php echo $bname;?>" /><br>
    16. // **
    17.   <input type=text size=25 value="Date:<?php echo $mm/dd/yyyy;?>" /><br>
    18.   <input type=text size=25 value="Invoice:<?php echo $acctno;?>" /><br>
    19. <TABLE BORDER=1 CELLPADDING=20 CELLSPACING=20>
    20.   <TD>
    21. <input type=text size=25 value="Ship To:"><br>
    22. <input type=text size=25 value="<?php echo $sname";
    23. "</td>";
    24. "</tr>";
    25. "</table";
    26.  }
    27.  }
    28. ?>
    29. </body></HTML>

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

    sarahk iTamer Staff

    Messages:
    17,405
    Likes Received:
    1,740
    Best Answers:
    37
    Trophy Points:
    510
    #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.
    PHP:
    1.  
    2. <html><body><form>
    3. <?php
    4. // error_reporting(E_ALL ^ E_NOTICE);
    5. mysql_connect('localhost','root','');
    6. mysql_select_db('homedb') or die("Unable to select database");
    7. $query="SELECT  * FROM `oocust` WHERE `payrec` = 'R' AND `pd` = '' ";
    8. $result=mysql_query($query);
    9. if(mysql_num_rows($result)):
    10. while($row= mysql_fetch_row($result)):
    11.  ?>
    12. <label>Name: <input name='bname[<?php echo $row['id'];?>]' type='text' size='25' value="<?php echo $row['bname'];?>" /></label><br>
    13.  
    14. <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>
    15. <label>Invoice: <input name='acctno[<?php echo $row['id'];?>]' type='text' size='25' value="<?php echo $row['acctno'];?>" /></label><br>
    16. <!-- cellpadding and spacing at 20! -->
    17. <TABLE BORDER=1 CELLPADDING=20 CELLSPACING=20>
    18.   <TD>
    19. <label>Ship To:<input type=text size=25 value=""></label><br>
    20. <input type=text size=25 value="<?php echo $sname"; ?>
    21. </td>
    22. </tr>
    23. </table>
    24. <?php
    25.  endwhile;
    26.  endif;
    27. ?>
    28. </form></body></HTML>
    sarahk, Feb 5, 2014 IP
  3. deathshadow

    deathshadow Prominent Member

    Messages:
    5,975
    Likes Received:
    824
    Best Answers:
    144
    Trophy Points:
    395
    #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 Member

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

    ataloss Greenhorn

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #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:
    17,405
    Likes Received:
    1,740
    Best Answers:
    37
    Trophy Points:
    510
    #6
    You don't need to "get" a manual - just go to www.php.net
    sarahk, Feb 6, 2014 IP
  7. ataloss

    ataloss Greenhorn

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

    PoPSiCLe Well-Known Member

    Messages:
    1,212
    Likes Received:
    108
    Best Answers:
    51
    Trophy Points:
    160
    #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 Greenhorn

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #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:
    1. <html><body background="oldorchard.jpg"><center>
    PHP:
    1.  
    2. <?php
    3.  error_reporting(E_ALL ^ E_NOTICE);
    4. mysql_connect('localhost','root','');
    5. mysql_select_db('homedb') or die("Unable to select database");
    6. $query="SELECT acctno,pd,payrec,orderno,
    7. bname,bstreet,$city,bstate,bzip,
    8. sname,sstreet,scity,sstate,szip,
    9. terms,duedate FROM oocust WHERE payrec = 'R' && pd = ''";
    10. $result=mysql_query($query);
    11. if(mysql_num_rows($result))
    12.   {
    13. while(list( $acctno,$pd,$payrec,$orderno,
    14. $bname,$bstreet,$bcity,$bstate,$bzip,
    15. $sname,$sstreet,$scity,$sstate,$szip,
    16. $terms,$duedate)= mysql_fetch_row($result))
    17.   {
    18. <!--the below code is for the date-->
    19.    $format = "D M d Y g:ia";
    20.    $date = date($format, time());
    21. <!--the above code is for the date-->
    22. ?>
    23. <form name='form'>
    24. <input type=text size=25 value="Old Orchard Plumbing">
    25. <input type=text size=25 value="2210 E. Hogan Hollow Road">
    26. <input type=text size=25 value="Margate, Fl  33063"><p>
    27. <!--?-->
    28. <input type=text size=25 value="Date - <?php echo $mm/dd/yyyy;?>" />
    29. <!--?-->
    30. <!--the below line inserts the database field-->
    31.   <input type=text size=25 value="Acct# - <?php echo $acctno;?>" /><br>
    32. <!--the above line inserts the database field-->
    33.  
    34. <TABLE BORDER=0 CELLPADDING=10 CELLSPACING=10>
    35.   <TD>
    36. <p><label for="bname"><b>
    37. <input type=text size=25 value="Bill To"><br>
    38.   <input type=text size=25 value="Name<?php echo $bname;?>" /><br>
    39. <input type=text size=25 value="Street - <?php echo $bstreet;?>" /><br>
    40.   <input type=text size=25 value="City - <?php echo $bcity;?>" /><br>
    41.   <input type=text size=25 value="State - <?php echo $bstate;?>" /><br>
    42.   <input type=text size=25 value="Zip - <?php echo $bzip;?>" />
    43.   </TD>
    44.   <td>
    45. <p><label for="sname"><b>
    46.   <input type=text size= 25 value="Ship To"><br>
    47.   <input type=text size=25 value="Name - <?php echo $sname;?>" /><br>
    48. <input type=text size=25 value="Street - <?php echo $sstreet;?>" /><br>
    49.   <input type=text size=25 value="City - <?php echo $scity;?>" /><br>
    50.   <input type=text size=25 value="State - <?php echo $sstate;?>" /><br>
    51.   <input type=text size=25 value="Zip - <?php echo $szip;?>" />
    52.   </TD>
    53.   <TR>  
    54.   <TD>
    55.   <input type=text size=25 value="Terms - <?php echo $terms;?>" /><br>  
    56. <input type=text size=25 value="Due Date - <?php echo $duedate;?>" /><br>
    57.   <input type=text size=25 value="order# - <?php echo $orderno;?>" />
    58.   </td>
    59.   <TD>
    60. <img src="dave pic.jpg" width=175 height=200>
    61.   </td>
    62.   </tr>
    63.   </table>
    64.   <?php
    65.   }
    66.   }
    67. ?>
    HTML:
    1. </form></body></HTML>
    ataloss, Feb 7, 2014 IP
  10. deathshadow

    deathshadow Prominent Member

    Messages:
    5,975
    Likes Received:
    824
    Best Answers:
    144
    Trophy Points:
    395
    #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 Prominent Member

    Messages:
    5,975
    Likes Received:
    824
    Best Answers:
    144
    Trophy Points:
    395
    #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:
    17,405
    Likes Received:
    1,740
    Best Answers:
    37
    Trophy Points:
    510
    #12
    Listen to @deathshadow, he knows his stuff.

    I'd like to see a tidier way of handling

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

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

    Code (Text):
    1. <input type=text size=25 value="Old Orchard Plumbing">
    2. <input type=text size=25 value="2210 E. Hogan Hollow Road">
    3. <input type=text size=25 value="Margate, Fl  33063">
    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 Prominent Member

    Messages:
    5,975
    Likes Received:
    824
    Best Answers:
    144
    Trophy Points:
    395
    #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 Prominent Member

    Messages:
    5,975
    Likes Received:
    824
    Best Answers:
    144
    Trophy Points:
    395
    #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 Greenhorn

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #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