Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP date script

Options
  • 22-11-2005 7:40pm
    #1
    Registered Users Posts: 673 ✭✭✭


    Hey,

    Im trying to poulate a MySQL database with data from a html form. The script is as follows:
    <?php require_once 'conn.php'; ?>
    <?php require_once 'header.php'; ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Irish Poker Online</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    a:link {
    	color: #0000CC;
    }
    a:visited {
    	color: #0000CC;
    }
    a:hover {
    	color: #CC0000;
    }
    -->
    </style>
    <link href="/poker%20website/CSS/Irish_Poker_Online.css" rel="stylesheet" type="text/css">
    </head>
    
    <?php
    if (isset($_POST['submit'])) {
       $qtmp = array();
       $errs = array();
       foreach ($_POST as $k=>$v) {
           switch ($k) {
                case 'date':
                     if (trim($v) == '') $errs[] = 'Date can not be left blank';
                     else {
                          $td = strtotime('Y-m-d',strtotime($v));
                          if ($td == '0000-00-00') $errs[] = 'Invalid date entered';
                          else $qtmp[] = $k . "='" . $td . "'"; }
                     break;
                  case 'venue':
    
                  case 'address':
    
    				case 'registration':
    
    				case 'game_start':
    
    				case 'finish_time':
    
    				case 'prize':
    
    				case 'buy_in':
    
    				case 're_buys':
    
    				case 'additional_info':
    
    				case 'game':
    
    				case 'rules':
    
    				case 'name':
    
    				case 'phone':
    
    				case 'email':
    
    				case 'website':
    
    				case 'tournament_name':
    
    
    
                     $qtmp[] = $k . "='" . mysql_real_escape_string(stripslashes($v)) . "'";
                     break;
             } // end of switch
        }  // end of foreach
        if (!emtpy($errs)) { // there are errors with the input don't update
            unset($_POST['submit']); }
        else { // no errors found, update database
            $q = 'insert into poker set ' . implode (', ',$qtmp);
            $rs = @msql_query($q) or die('Problem with query: ' . $q . '<br>'. mysql_error());
       }
    }
    ?>
    
    	<?php require_once 'footer.php'; ?>
    
    <body>
    </body>
    </html>
    

    It runs without any errors showing up but it just brings me to a totally blank page and the database does not get updated with the info.

    Can anyone see what im doing wrong in the script?

    Thanks


Comments

  • Registered Users Posts: 2,157 ✭✭✭Serbian


    I put the script into a PHP page here, but the form is not included so I just took a look through the PHP. I'm pretty sure your SQL statement is wrong. You should have something like:

    [php]$q = "INSERT INTO poker VALUES ('" . implode ("', '",$qtmp) . "')";[/php]

    but just to be sure, could you post the form that you are using to submit the date?

    Thanks.


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Hey,

    Thanks for the reply!!

    I've tried it with that new "INSERT INTO" line you posted but its still doing exactly the same thing as before. Bringing me to a blank page and not updating the database. The form code is:
    <form action="cms/add_tournament.php?action=submit&type=poker" method="post">
      <div align="center">
        <table width="700" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="200" class="text_main">Tournament Name:</td>
            <td><input name="tournament_name" type="text" value="Not Specified" size="50" maxlength="255"></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
        </table>
        <table width="700" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="200" class="text_main">Venue:</td>
            <td><input name="venue" type="text" size="50" maxlength="150"> 
              <span class="links">*</span> </td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Address:</td>
            <td><input name="address" type="text" size="70" maxlength="255"> 
              <span class="links">*</span> </td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Date:</td>
            <td><input name="date" type="text" value="yyyy-mm-dd" maxlength="20"> 
              <span class="links">* e.g. yyyy-mm-dd (2005-06-29)</span></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Registration Time: </td>
            <td><input name="registration" type="text" value="00:00"> 
              <span class="links">e.g. hh:mm (20:00)</span> </td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Game Start Time: </td>
            <td><input name="game_start" type="text" value="00:00"> 
              <span class="links">* e.g. hh:mm (20:00)</span></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Game Finish Time:</td>
            <td height="20"><input name="finish_time" type="text" value="00:00"> 
            <span class="links">e.g. hh:mm (20:00)</span> </td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Prize pool: </td>
            <td height="20"><input name="prize" type="text" value="Not Specified"></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Buy-In Cost: </td>
            <td height="20"><input name="buy_in" type="text" value="Not Specified"></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Re-buy cost (If any): </td>
            <td height="20"><input name="re_buys" type="text" value="Not Specified"></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td valign="top" class="text_main">Additional Info: </td>
            <td height="20"><textarea name="additional_info" cols="60" rows="2">Not Specified</textarea></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Game:</td>
            <td height="20"><input name="game" type="text" value="Not Specified" size="80" maxlength="50"></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td valign="top" class="text_main">Rules:</td>
            <td><textarea name="rules" cols="60" rows="2">Normal Rules Apply</textarea></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Contact Name: </td>
            <td height="20"><input name="name" type="text" size="40" maxlength="50"> 
              <span class="links">*</span> </td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Phone Number: </td>
            <td height="20"><input name="phone" type="text" value="Not Specified" size="40" maxlength="15"></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Email Address: </td>
            <td height="20"><input name="email" type="text" value="Not Specified" size="60" maxlength="100"></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main">Website Address: </td>
            <td height="20"><input name="website" type="text" value="Not Specified" size="60" maxlength="100"></td>
          </tr>
          <tr>
            <td class="text_main">&nbsp;</td>
            <td height="20">&nbsp;</td>
          </tr>
          <tr>
            <td class="text_main"><input type="submit" name="Submit" value="Submit"></td>
            <td height="20">&nbsp;</td>
          </tr>
        </table>
      </div>
      <p>&nbsp;</p>
    </form>
    


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    After the line
    $q = 'insert into poker set ' . implode (', ',$qtmp);
    Put in
    die("".$q);

    This will output the generated query to the screen.

    Then try run that query in a MySQL command line or GUI (or post it up here) and you should see where the problem lies.


  • Registered Users Posts: 673 ✭✭✭Bananna man


    seamus wrote:
    After the line
    $q = 'insert into poker set ' . implode (', ',$qtmp);
    Put in
    die("".$q);

    This will output the generated query to the screen.

    Then try run that query in a MySQL command line or GUI (or post it up here) and you should see where the problem lies.

    Just tried that but it is still just bringing me to a blank screen


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    That means it's not even reaching the point at which you do the query.

    There's a spelling mistake on line 75:
    if (!emtpy($errs)) { // there are errors with the input don't update
    (empty) :)

    Also after line 75, stick in some debugging info so you can see exactly what's going on (if there are errors - should it not output them?)


  • Advertisement
  • Registered Users Posts: 673 ✭✭✭Bananna man


    I've corrected the spelling mistake, cheers!! I've tried it now with the two different lines of code:

    $q = "INSERT INTO poker VALUES ('" . implode ("', '",$qtmp) . "')";

    and

    $q = 'insert into poker set ' . implode (', ',$qtmp);

    but its still doing the same thing.

    Im only learning php so i dont have a clue about putting in debug information.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    OK,
    Then after line 75 (!empty....)
    Stick in
    print_r($errs);
    print "<br><br>";
    print_r($_POST)


    And try run it again.


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Ok, i ran it with that in it and it gaev me this error:
    Parse error: parse error, unexpected T_UNSET in D:\webworld\irishpokeronline\irishpokeronline.com\www\poker website\cms\add_tournament.php on line 81
    

    which refers to this line now:

    unset($_POST['submit']); }
    


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    My bad, you're missing the semicolon after
    print_r($_POST)


  • Closed Accounts Posts: 47 Berkel


    Make sure your include files do not have errors either - if you are using the 'header' command make sure it outputs before any html is written - try putting the line "error_reporting(E_ALL) at the top - this will display all the errors, warnings and notices in your script...

    Let us know how you get on...


  • Advertisement
  • Registered Users Posts: 673 ✭✭✭Bananna man


    I've tried it a few ways but i think this is the way you mean
    if (!empty($errs)) { // there are errors with the input don't update
    	unset($_POST['submit']); 
    
    print_r($errs);
    print "<br><br>";
    print_r($_POST);
    } 
    
        else { // no errors found, update database
            $q = 'insert into poker set ' . implode (', ',$qtmp);
    
            $rs = @msql_query($q) or die('Problem with query: ' . $q . '<br>'. mysql_error());
       }
    }
    ?>
    

    Anyways, this way is doing the same thing as before and if i put it anywhere else i am just getting a parse errpr relating to
    else { // no errors found, update database
    

    Jebus, this is taking the piss. All i really need is something that will allow people to input their tournament dates in the form dd/mm/yyyy and then change that around so that MySQL will accept it in the form yyyy/mm/dd. Who would have thought it would cause 2 bloddy days of work!!!!


  • Registered Users Posts: 673 ✭✭✭Bananna man


    The includes files are working fine when i use my old script (which makes people input the date in the form yyyy-mm-dd). This would be fine but im sure this will be too much for them to get right ;)


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Heh. Rookie mistake. So easy to overlook too.

    if (isset($_POST)) {

    Won't work. PHP is case-sensitive.
    try

    if (isset($_POST)) {


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Still the same :mad:


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    ?
    Works for me (well, kind of, I do get errors). This is line 25 or 26 you're changing yeah? Capital 'S' on 'Submit'.


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Yep line 25 and 26. The code has been changed around a few times so this is what im using at the moment:
    <?php require_once 'conn.php'; ?>
    <?php require_once 'header.php'; ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Irish Poker Online</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    a:link {
    	color: #0000CC;
    }
    a:visited {
    	color: #0000CC;
    }
    a:hover {
    	color: #CC0000;
    }
    -->
    </style>
    <link href="/poker%20website/CSS/Irish_Poker_Online.css" rel="stylesheet" type="text/css">
    </head>
    
    <?php
    if (isset($_POST['Submit'])) {
       $qtmp = array();
       $errs = array();
       foreach ($_POST as $k=>$v) {
           switch ($k) {
                case 'date':
                     if (trim($v) == '') $errs[] = 'Date can not be left blank';
                     else {
                          $td = strtotime('Y-m-d',strtotime($v));
                          if ($td == '0000-00-00') $errs[] = 'Invalid date entered';
                          else $qtmp[] = $k . "='" . $td . "'"; }
                     break;
                  case 'venue':
    
                  case 'address':
    
    				case 'registration':
    
    				case 'game_start':
    
    				case 'finish_time':
    
    				case 'prize':
    
    				case 'buy_in':
    
    				case 're_buys':
    
    				case 'additional_info':
    
    				case 'game':
    
    				case 'rules':
    
    				case 'name':
    
    				case 'phone':
    
    				case 'email':
    
    				case 'website':
    
    				case 'tournament_name':
    
    
    
                     $qtmp[] = $k . "='" . mysql_real_escape_string(stripslashes($v)) . "'";
                     break;
             } // end of switch
        }  // end of foreach
        if (!empty($errs)) { // there are errors with the input don't update
            unset($_POST['Submit']); }
        else { // no errors found, update database
            $q = 'insert into poker set ' . implode (', ',$qtmp);
            $rs = @msql_query($q) or die('Problem with query: ' . $q . '<br>'. mysql_error());
       }
    }
    ?>
    
    	<?php require_once 'footer.php'; ?>
    
    <body>
    </body>
    </html>
    


  • Closed Accounts Posts: 47 Berkel


    If you put an 'echo $q' after setting up the query, what do you get?


  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    ok do the following

    use this php script

    [PHP]
    <?php require_once 'conn.php'; ?>
    <?php require_once 'header.php'; ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"&gt;
    <html>
    <head>
    <title>Irish Poker Online</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    a:link {
    color: #0000CC;
    }
    a:visited {
    color: #0000CC;
    }
    a:hover {
    color: #CC0000;
    }
    -->
    </style>
    <link href="/poker%20website/CSS/Irish_Poker_Online.css" rel="stylesheet" type="text/css">
    </head>

    <?php
    if (isset($_POST)) {
    $qtmp = array();
    $errs = array();
    foreach ($_POST as $k=>$v) {
    switch ($k) {
    case 'date':
    if (trim($v) == '') $errs[] = 'Date can not be left blank';
    else {
    $td = strtotime('Y-m-d',strtotime($v));
    if ($td == '0000-00-00') $errs[] = 'Invalid date entered';
    else $qtmp[] = $k . "='" . $td . "'"; }
    break;
    case 'venue':

    case 'address':

    case 'registration':

    case 'game_start':

    case 'finish_time':

    case 'prize':

    case 'buy_in':

    case 're_buys':

    case 'additional_info':

    case 'game':

    case 'rules':

    case 'name':

    case 'phone':

    case 'email':

    case 'website':

    case 'tournament_name':



    $qtmp[] = $k . "='" . mysql_real_escape_string(stripslashes($v)) . "'";
    break;
    } // end of switch
    } // end of foreach
    if (!empty($errs)) { // there are errors with the input don't update
    unset($_POST); }
    else { // no errors found, update database
    $q = "insert into poker set " . implode (', ',$qtmp) . ", event_id=''";
    $rs = mysql_query($q) or die('Problem with query: ' . $q . '<br>'. mysql_error());
    }
    }
    ?>



    <body>
    </body>
    </html>
    [/PHP]


    and also make sure your database is structured as follows
    CREATE TABLE `poker` (
    `tournament_name` VARCHAR( 150 ) NOT NULL ,
    `venue` VARCHAR( 150 ) NOT NULL ,
    `address` VARCHAR( 255 ) NOT NULL ,
    `date` DATE NOT NULL ,
    `registration` TIME NOT NULL ,
    `game_start` TIME NOT NULL ,
    `finish_time` TIME NOT NULL ,
    `prize` INT( 20 ) ,
    `buy_in` INT( 20 ) ,
    `re_buys` INT( 20 ) ,
    `additional_info` VARCHAR( 255 ) ,
    `game` VARCHAR( 50 ) ,
    `rules` VARCHAR( 255 ) ,
    `name` VARCHAR( 50 ) NOT NULL ,
    `phone` INT( 15 ) ,
    `email` VARCHAR( 100 ) ,
    `website` VARCHAR( 100 ) ,
    `event_id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
    PRIMARY KEY ( `event_id` )
    )
    


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Right, I've tried that code. This is still bringing me to a blank screen but it is updating the database but it is not putting in the date. It is just staying as the default of 0000-00-00.


  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    well the page is always going to be blank because you have no output specificed when the form is processed


    as for the date, change your script to this

    $td = date("Y-m-d", strtotime($v));

    think that might work for ya


  • Advertisement
  • Registered Users Posts: 673 ✭✭✭Bananna man


    Thanks,

    Its inputting the date now but i still have to input it yyyy-mm-dd. I tried inputting the date as:

    29-11-2005 and it came up in the database as 2035-04-28

    and 29/11/2005 which came up as 2007-05-11.


  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    see this is where the confusion lies - you need to figure out what way you want the data stored in the database.

    the current way of y,m,d is great for ordering items by year then month, then by date

    as according to your form the date has to be entered

    e.g. yyyy-mm-dd (2005-06-29) - like so

    so why are you trying to enter it in different formats?


  • Registered Users Posts: 673 ✭✭✭Bananna man


    I am getting people to submit when and where their tournaments will be held i.e. venue, address, date, time, etc. I have it set up now that when it appears on my site it is in the format i.e. 29th Jan 2005, but when people are inputting this info into the database they are being asked to input it as 2005-01-29.

    This is ok but it wont be long until someone inputs it wrong and that will mess everything up as i am displaying the tournaments by date 1.e. listing of tournaments coming up in the next 7 days. www.irishpokeronline.com


  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    the problem is your database is set up to accept the dates in 0000-00-00 format

    How the information is stored at the moment is the best way - if you want to display it differently, then you can format the output when you are doing a database query

    To ensure people dont screw up why not use a drop down for the date - saves hassle and worry over something simple


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Thanks for all the help guys!!!

    I've gone with the drop down menu option and it is working fine now. Man, why didnt i do this 2 days ago???? :confused:


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Got it finished at last!!! Thanks for the help. Next an option so that people can modify entried they have made (should be fun ;) ). Anyways, for anyone who's interested the site is www.irishpokeronline.com

    P.S. The html is in a bit of a mess. Its a combe of old tags and CSS. I started the site about a year ago and it was left idle so when i went back to it a couple of weeks ago i started using CSS on it.

    P.P.S. When i do a site validation i only get html 4.0 errors. Is this ok i.e. what kind of browser will have problems with this?


Advertisement