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...
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... sorry for confusion...
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
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
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 - i told you that i'm a beginner ( noob ) in PHP so i work and learn...
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:
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());
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 )
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:
Again problems... 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?
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:
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:
Problem solved with if(!function_exists("myFuncName")) { function myFuncName() { //what to do } } PHP: Thank you, rohan!