SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Feb 2009
    Location
    israel
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php code doesn't update MySql table

    Hi Everyone,
    This is the HTML page I created for input:
    HTML Code:
    <html>
    <body>
     <form action = "insert_persons.php" method="GET">
    	startdate: <input type ="text" name="startDate">
    	enddate: <input type ="text" name="endDate">
    	<input type="submit">
    </form>
    </body>
    </html>
    This is insert_persons.php file that contains the code to insert data from HTML page into MySql table
    PHP Code:
    <?php
     
    echo $_GET['startDate'];
     echo 
    $_GET['endDate'];
     
      
    $con=mysqli_connect("localhost","root","pass","db");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
      }

     
    $sql="INSERT INTO persons (startDate, endDate, intervalTime)
    VALUES
    (
     '
    $_GET[startDate]',
     '
    $_GET[endDate]',
     TIMEDIFF('
    $_GET[endDate]','$_GET[startDate]')
     )"
    ;

    if (!
    mysqli_query($con,$sql))
      {
      die(
    'Error: ' mysqli_error($con));
      }
    echo 
    "1 record added";
    ?>
    Here is the data I assigned in the HTML page:
    startdate- 2013-07-02 11:00:00
    enddate- 2013-07-02 14:45:00
    When I press the "Submit" button something strange happens: On the screen I get the code of php file shown above.
    The code doesn't update the MySql table. It just appears on the screen.
    Any idea what goes wrong?
    Thanks

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deotpit View Post
    When I press the "Submit" button something strange happens: On the screen I get the code of php file shown above.
    Elaborate on that sentence for me please. Are you seeing the actual code on the page? If so, then your web server isn't setup properly and it isn't executing your PHP script, it is just serving it up as a text file.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    666
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Assuming you have php 5.3, this should work.
    PHP Code:
    <?php
        $con
    =mysqli_connect("localhost","root","pass","db");
        
    // Check connection
        
    if (mysqli_connect_errno())
        {
            echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
        }
        if(isset(
    $_GET['startDate'])){
            
    $intervalTime TIMEDIFF("{$_GET['endDate']}","{$_GET['startDate']}");
            
    $sql="INSERT INTO persons (startDate, endDate, intervalTime)
            VALUES
            (
            '
    {$_GET['startDate']}',
            '
    {$_GET['endDate']}',
            '
    $intervalTime')
            )"
    ;
            
            if (!
    mysqli_query($con,$sql))
            {
            die(
    'Error: ' mysqli_error($con));
            }
            echo 
    "1 record added";    
        }
    ?>
    If less than php5.3 you'll need to use strtotime() to get minutes.
    PHP Code:
    <?php
        $con
    =mysqli_connect("localhost","root","pass","db");
        
    // Check connection
        
    if (mysqli_connect_errno())
        {
            echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
        }
        
        if(isset(
    $_GET['startDate'])){
            
    $datetime1 strtotime($_GET['startDate']);
            
    $datetime2 strtotime($_GET['endDate']);
            
    $interval  abs($datetime2 $datetime1);
            
    $intervalTime   round($interval 60); 
            
    $sql="INSERT INTO persons (startDate, endDate, intervalTime)
            VALUES
            (
            '
    {$_GET['startDate']}',
            '
    {$_GET['endDate']}',
            '
    $intervalTime')
            )"
    ;
            
            if (!
    mysqli_query($con,$sql))
            {
            die(
    'Error: ' mysqli_error($con));
            }
            echo 
    "1 record added";    
        }
    ?>
    At least I think so. Not tested

  4. #4
    SitePoint Addict
    Join Date
    Feb 2009
    Location
    israel
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Elaborate on that sentence for me please. Are you seeing the actual code on the page? If so, then your web server isn't setup properly and it isn't executing your PHP script, it is just serving it up as a text file.
    Hi
    Attached is screenshots of my servers.to_forum.jpg

  5. #5
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    666
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Not sure what else you've got going on the page but it looks like you've already got a connection established. Try removing those lines.

  6. #6
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    359
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    As a minor aside, and not saying it will make any difference, but is there a reason you're using GET instead of POST here?
    http://www.firenza.net - my homage to a car from the 1970s

  7. #7
    SitePoint Addict
    Join Date
    Feb 2009
    Location
    israel
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Drummin View Post
    Not sure what else you've got going on the page but it looks like you've already got a connection established. Try removing those lines.
    Thanks,
    Could the problem lay on Google chrome?
    Here is a more simple code:
    Code:
    <?php
     echo "Hello world";
    ?>
    From right click menu on "test.php" which is the above file's name, I select "open with" and from windows explorer I select:
    "Program files, Google, Application, Chrome.exe".
    What I get is the code :-(

  8. #8
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,386
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by deotpit View Post
    Thanks,
    Could the problem lay on Google chrome?
    Here is a more simple code:
    Code:
    <?php
     echo "Hello world";
    ?>
    From right click menu on "test.php" which is the above file's name, I select "open with" and from windows explorer I select:
    "Program files, Google, Application, Chrome.exe".
    What I get is the code :-(
    This sounds like the root of your problem.. PHP won't execute if you load it into the browser directly from the file system, it has to be served by a webserver with PHP installed.

    To run PHP on a windows machine you'll need to install something like WampServer or XAMPP. Then you open your browser and goto the special url http://localhost/ which is usually the address of the server running on your machine.

  9. #9
    SitePoint Addict
    Join Date
    Feb 2009
    Location
    israel
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    This sounds like the root of your problem.. PHP won't execute if you load it into the browser directly from the file system, it has to be served by a webserver with PHP installed.

    To run PHP on a windows machine you'll need to install something like WampServer or XAMPP. Then you open your browser and goto the special url http://localhost/ which is usually the address of the server running on your machine.
    And the root of my problem it was !
    Inside the address bar of my Crome browser I typed: "http://localhost/addhours.php" and the php code initiated by the "Submit" button worked just fine !
    Thanks to all of you !


Tags for this Thread

Bookmarks

Posting Permissions

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