Help me troubleshoot/tweak my PHP code

Discussion in 'PHP' started by Cookied89, Oct 6, 2008.

  1. #1
    Inserting Multiple Rows - MySQL PHP Page.

    Sorry for all the questions. I'm having trouble inserting multiple rows into the MySQL DB via a PHP/HTML form.
    I've got a table setup like so -
    Table name - timesheet
    Columns - id (auto increment)
    uname -
    entrydate
    state
    hours.

    And I'm trying to get a HTML form working with PHP so that they can insert into the database.
    Right now I've got it sort of working with the code below (At the moemnt I've got both the PHP code and the HTML form on one page, but I will change this)

    
    <?include_once ("auth.php");
    	include_once ("authconfig.php");
    	include_once ("check.php"); ?>
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("test-auth", $con);
    
    $sql="INSERT INTO timesheet (uname, entrydate, state, hours)
    VALUES
    ('$_POST[uname]','$_POST[entrydate]','$_POST[state]','$_POST[hours]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "meh";
    
    mysql_close($con)
    
    ?>
    
    
    
    
    
    <html>
    <body>
    
    VICTORIA:
    <form action="test1.php" method="post">
    <input type="hidden" name="uname" value="<?echo $check["uname"]?>" />
    Entry Date: <input type="date" name="entrydate" />
    Hours: <input type="text" name="hours" />
    State:<input type="text" name="state" />
    <input type="submit" />
    
    </form>
    
    
    </body>
    </html>
    Code (markup):
    The problems I have with this form is that -
    a) The STATE input box is just for text, what i'm after is a drop down list so that users can only select from VIC, NSW, TAS, QLD, WA, etc. If somebody goes and types in for example 'VIC." with a fullstop or a space or something like VICTORIA, it's going to fuck up all my other queries later on.

    b) This form only works for putting in one row, what I want is to be able to have a form that can insert multiple rows with a single click. Eg -

    $uname sent to the 'uname' column (Hidden) (I have a login/authentication system in place so once they're logged in it just ads their name in)
    At the Top - [Datebox] [Clickable Calander to set date]
    Then below -

    STATE HOURS

    VIC x.x
    WA x.x
    TAS x.x
    QLD x.x
    TAS x.x

    [SUBMIT]

    So this way it'd put in the amount of hours they do for each state.
    uname=test entrydate- 2008-08-10 state=VIC hours=2.4
    uname=test entrydate- 2008-08-10 state=WA hours=3.8

    Is this possible?
    Have been looking up arrays and loops but all the examples I've found are too techncial and I have a hard time adjusting it to work for me.
     
    Cookied89, Oct 6, 2008 IP
  2. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hit my messengers up, I might be of some assistance. Don't PM me, use a messenger.
     
    NatalicWolf, Oct 6, 2008 IP
  3. caffeinefree

    caffeinefree Guest

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If I'm understanding you correctly, you want a state combo box with static values. If so, try this:

    <select name='statesel'>
    <option value='VIC'>VIC</option>
    <option value='WA'>WA</option>
    <option value='TAS'>TAS</option>
    <option value='QLD'>QLD</option>
    </select>

    As for part 2 of your question:
    There are a number of ways to tackle the problem.
    I assume the number of time sheet entries will vary.
    You could put a checkbox or similar asking if they'd like to add
    another entry. So when the form is 1st submitted, your script would..
    1. Insert submitted record
    2. display form again if user ticked checkbox
    This would be the easiest route from coding perspective.

    Alternatively, you could mess with loops and submitting arrays.
    But somehow you'll need the user to submit how many entries he'd
    like to make. Then you would generate the form with requested number
    of rows. You can submit arrays using the following syntax:

    echo "<input type='text' name='timeentry[$arrkey][hours]'>";

    Then after submission, your script can access $timeentry[1]['hours'], $timeentry[2]['hours'], etc...

    The key here (no pun intended) is to supply a unique id for $arrkey which,
    in your case, could just be a number generated through loop.

    Hope that helps some.
     
    caffeinefree, Oct 6, 2008 IP