PHP help

Discussion in 'PHP' started by mekerica, Jul 15, 2008.

  1. #1
    I'm still new in PHP so I opened this Tread because I need help when I get stuck.

    Now I'm working on script which will put News in the database.

    News is written by form, but date and time are automatic with date function. When I finished, I have a problem:

    Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\Stranica\cms\dodaj-vijest.php on line 10

    so, here is the code of script:

    
    <?php
    $vijest = mysql_real_escape_string($_POST['vijest']);
    $submit = $_POST['submit'];
    
    string date ( string $format );
    
    if(isset($submit)) {
    
    if(($vijest) == "") {
    echo "Niste upisali vijest.<br></b></font>";
    $reg="true";
    }
    
        if($reg != "true") {
        mysql_query("INSERT INTO vijesti (datum,vijest) VALUES ('$format','$vijest')") or die(mysql_error());
        echo "Vijest je dodana!";
    
    }
    }
    
    ?>
    
    PHP:
    Where is a mistake?

    Keep in mind that I'm new in PHP and I learn by the way while I'm working, so noob and stupid mistakes are possible... :D
     
    mekerica, Jul 15, 2008 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    if(($vijest) == "") {

    Should be
    if($vijest == "") {
     
    shallowink, Jul 15, 2008 IP
  3. mekerica

    mekerica Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you, I do this, but this is not a problem. The problem stays.

    Line 10 is:
    
    string date ( string $format );
    
    PHP:
    in this line is a problem.

    now i see, in the Error writes line 10, but there is line 5 because this is only a part of the Code. i delete code for connecting on database because there are my username and pass of database... :D

    sorry for confusion...
     
    mekerica, Jul 15, 2008 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    totally skipped over that.... take it you want the date?
    replace :

    string date ( string $format );
    with :
    echo date("m.d.y");

    or push it to a variable...

    $today = date("m.d.y");

    Will give you 07.15.08

    More examples can be found here:
    http://us2.php.net/date
     
    shallowink, Jul 15, 2008 IP
  5. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #5
    
    string date ( string $format );
    
    PHP:
    should be
    
    date ($format);
    //Eg date('d M y, H:i',time());
    
    PHP:
    Looks like you copied from the PHP manual blindly!
    First learn to use the date() function :)
     
    rohan_shenoy, Jul 15, 2008 IP
  6. mekerica

    mekerica Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thank you on answers! I do what rohan_shenoy say and now it works, but when upload, something is not working - logical mistake! now i go to take a bath, and later i will take a look on mistakes and if i can't solve, i will paste code here.

    yes, a copy from manual :D - i told you that i'm a beginner ( noob ) in PHP so i work and learn...
     
    mekerica, Jul 15, 2008 IP
  7. mekerica

    mekerica Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Now, I'm looking at the form but I don't know where is the problem...

    The form is working - problem is logical. when i write Vijest in textbox and hit Submit, it writes that the news is being added, but when i go see in database, there is the news, but date isn't here. type of filed for date is DATETIME and there writes 0000-00-00 00:00:00 which means that the date isn't inserted.

    Here is the code:
    
    <?php
    $vijest = mysql_real_escape_string($_POST['vijest']);
    $submit = $_POST['submit'];
    
    date ($format);
    
    if(isset($submit)) {
    
    if($vijest == "") {
    echo "Niste upisali vijest.<br></b></font>";
    $reg="true";
    }
    
        if($reg != "true") {
        mysql_query("INSERT INTO vijesti (datum,vijest) VALUES ('$format','$vijest')") or die(mysql_error());
        echo "Vijest je dodana!";
    
    }
    }
    
    ?>
    <div id="main">
    <form action="" method="post">
    <center>
    <table>
    <tr>
    <td>Vijest:</td>
    <td><input type="text" name="vijest"></td></tr>
    <tr>
    <td><input type="submit" name="submit" value="Pošalji" ></td></tr>
    </table>
    </center>
    </form>
    
    PHP:
     
    mekerica, Jul 16, 2008 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    replace :
    date ($format);

    with
    $date = date("m.d.y");

    then replace :
    mysql_query("INSERT INTO vijesti (datum,vijest) VALUES ('$format','$vijest')") or die(mysql_error());

    with:
    mysql_query("INSERT INTO vijesti (datum,vijest) VALUES ('$date','$vijest')") or die(mysql_error());
     
    shallowink, Jul 16, 2008 IP
  9. mekerica

    mekerica Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thank you, but I solve this problem.

    Now I have another problem with script who select this from database and echo this.

    Here is the code:
    
    <?php 
    
    $upit = 'SELECT datum,vijest FROM vijesti ORDER BY datum DESC LIMIT 5';
    $rezultat_upita = mysql_query($upit);
    
    while ($redak = @mysql_fetch_array($rezultat_upita));
    
    function convert_date($datum){
    $year = substr($datum, 0,4);
    $month = substr($datum, 5,2); 
    $day = substr($datum, 8,2); 
    $rezultat = $day . "." . $month . "." . $year . ".";
    return $rezultat;
    };
    $result = convert_date($datum);
    echo ("<p>$result - ".$redak['vijest']."</p>");
    ?>
    
    PHP:
    When I start this script, it writes only "-..." , but doesn't write the datum and vijest.

    ( sorry on stupid mistakes, but I'm still new, I'm still learning )
     
    mekerica, Jul 17, 2008 IP
  10. mekerica

    mekerica Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Problem solved!

    
    <?php 
    $upit = 'SELECT datum,vijest FROM vijesti ORDER BY datum DESC LIMIT 5';
    $rezultat_upita = mysql_query($upit);
    
    echo ("");
    while ($redak = @mysql_fetch_array($rezultat_upita)) {
    
    $datum = $redak['datum'];
    
    function convert_date($datum){
    $year = substr($datum, 0,4);
    $month = substr($datum, 5,2); 
    $day = substr($datum, 8,2); 
    $rezultat = $day . "." . $month . "." . $year . ".";
    return $rezultat;
    };
    $result = convert_date($datum);
    echo ("<p>$result - ".$redak['vijest']."</p>");
    }
    ?>
    
    PHP:
     
    mekerica, Jul 17, 2008 IP
  11. mekerica

    mekerica Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Again problems... :confused:

    I upload script and when I add once in database one line, it works, but when I add more news, it writes last added, and then came and error

    Fatal error: Cannot redeclare convert_date() (previously declared in /home/svijetig/public_html/cms/datum.php:11) in /home/svijetig/public_html/cms/datum.php on line 11

    Why?
     
    mekerica, Jul 17, 2008 IP
  12. mekerica

    mekerica Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    anybody knows?
     
    mekerica, Jul 17, 2008 IP
  13. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #13
    That because the function is being declared twice. Do you include() or require() it? If yes, than use include_once() or require_once() functions.

    or you may also use
    
    if(!function_exists("myFuncName"))
    {
       function myFuncName()
       {
       //what to do
       }
    }
    
    PHP:
     
    rohan_shenoy, Jul 17, 2008 IP
  14. mekerica

    mekerica Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    No, I didn't include() or require() it...

    Here is the code:

    
    <?php $upit = 'SELECT datum,vijest FROM vijesti ORDER BY datum DESC LIMIT 5';
    
    $rezultat_upita = mysql_query($upit);
    
    echo ("");
    while ($redak = @mysql_fetch_array($rezultat_upita)) {
    
    $datum = $redak['datum'];
    
    function convert_date($datum){
    $year = substr($datum, 0,4);
    $month = substr($datum, 5,2); 
    $day = substr($datum, 8,2); 
    $rezultat = $day . "." . $month . "." . $year . ".";
    return $rezultat;};
    
    $result = convert_date($datum);
    
    echo ("<p>$result - ".$redak['vijest']."</p>");}?>
    
    PHP:
     
    mekerica, Jul 17, 2008 IP
  15. mekerica

    mekerica Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Problem solved with
    
    if(!function_exists("myFuncName"))
    {
       function myFuncName()
       {
       //what to do
       }
    }
    
    PHP:
    Thank you, rohan!
     
    mekerica, Jul 18, 2008 IP