KubeR Posted August 1, 2013 Share Posted August 1, 2013 Hello,I am sitting on it for about an hour,two three and I just don't understand why I get this error ...I am trying to create a new file with PHP and it gives me this error : Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in admin.php on line 24 now,the problem is that this is line 24 : \n$text[] = $row[0]; Around it/code : function addPage() { var page_name=prompt("Enter name"); if (page_name!=null) { <?php $file = fopen($_GET["page_name"].".php","w"); $fcontent = "\n$query = 'SELECT title FROM home'; \nif($result = mysqli_query($link,$query)) { \n$pages = mysqli_num_rows($result); \nwhile($row=mysqli_fetch_row($result)) { \n$text[] = $row[0]; \n} \nmysqli_free_result($result); \n}"; fwrite($file,$fcontent); fclose($file); ?> } } Will appericiate if somebody will tell me what's wrong in here. Greetings,KubeR. Quote Link to comment https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/ Share on other sites More sharing options...
DavidAM Posted August 1, 2013 Share Posted August 1, 2013 Building a string using double-quotes causes variables to be interpreted. So the code is trying interpret $text[] = $row[0] However, to my knowledge, you can not do an assignment inside a double-quoted string. So, the PHP engine expectes $text[] to have an array index inside the square-brackets so it can interpret the variable (and insert the value). Try using single-quotes: $fcontent = '.... Of course then, the new-lines are not going to be interpreted as newlines, they will instead be literal \n (backslash "n"). Since you don't close the quotes between lines, you don't really need them anyway (unless you want the code double-spaced). Same goes for the single-quotes inside the string: they need to be escaped or changed to double-quotes: $fcontent = '$query = \'SELECT title FROM home\'; if($result = mysqli_query($link,$query)) { $pages = mysqli_num_rows($result); while($row=mysqli_fetch_row($result)) { $text[] = $row[0]; } mysqli_free_result($result); }'; If you need (or want) the double-quotes around the string, then escape all of the dollar-signs with a backslash so PHP will not interpret it as a variable name:$fcontent = "\n\$query = 'SELECT title FROM home'; ... Quote Link to comment https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/#findComment-1443001 Share on other sites More sharing options...
KubeR Posted August 1, 2013 Author Share Posted August 1, 2013 (edited) Oh thank you ! Never figured out what these slashes('\') means. But now I left with only one problem,it shows me an error that variable isn't defined. I readed in StackFlow that you can get JavaScript variables into PHP using $_GET["var_name"] but it shows me an "undefined array" and another error "mysqli_query() [<a href='function.mysqli-query'>function.mysqli-query</a>]: Couldn't fetch mysqli in" Never saw these errors and not an expert in PHP,this is the code : $query = 'insert into home values('.$_GET[page_name].','.$_GET[page_name].');'; if($result = mysqli_query($link,$query)) { errors comes from the last line. Edited August 1, 2013 by KubeR Quote Link to comment https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/#findComment-1443019 Share on other sites More sharing options...
lemmin Posted August 1, 2013 Share Posted August 1, 2013 To get the Javascript variables, you would have to pass them to the PHP file either directly through the URL or using a form: http://localhost/index.php?page_name=home OR <form action="index.php"> <input type="text" name="page_name"/> <input type="submit"/> </form> In your case, where you want the page from a prompt, this should work: function addPage() { var page_name=prompt("Enter name"); if (page_name!=null) top.location = '?page_name=' + page_name; } Quote Link to comment https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/#findComment-1443021 Share on other sites More sharing options...
KubeR Posted August 1, 2013 Author Share Posted August 1, 2013 Hm,I looked at the source in the browser and it's just don't parse the PHP code. it says that the php prefix ( < ) is 'unexpected token'. Uncought SyntexError:Unexpected token < Does it mean I'll need to find another way to create a file (with JS or something) or there is somekind of fix for this ? Quote Link to comment https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/#findComment-1443025 Share on other sites More sharing options...
lemmin Posted August 1, 2013 Share Posted August 1, 2013 Do you have PHP installed on your server? Is your file a .PHP file? Quote Link to comment https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/#findComment-1443031 Share on other sites More sharing options...
KubeR Posted August 1, 2013 Author Share Posted August 1, 2013 (edited) Yes,the latest and indeed,it has an .php extension. Edited August 1, 2013 by KubeR Quote Link to comment https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/#findComment-1443035 Share on other sites More sharing options...
KubeR Posted August 2, 2013 Author Share Posted August 2, 2013 So,as I understood this is unsolveable ?+ Quote Link to comment https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/#findComment-1443129 Share on other sites More sharing options...
lemmin Posted August 2, 2013 Share Posted August 2, 2013 Post the new code that throws the Javascript error Quote Link to comment https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/#findComment-1443183 Share on other sites More sharing options...
Pawan_Agarwal Posted August 2, 2013 Share Posted August 2, 2013 do not add brackets [] with $text[] and provide a column name with $row[0] in place of zero like $row["Address"] Quote Link to comment https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/#findComment-1443189 Share on other sites More sharing options...
KubeR Posted August 3, 2013 Author Share Posted August 3, 2013 Post the new code that throws the Javascript error Same code in the first post. <?php is the error cause. Quote Link to comment https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/#findComment-1443311 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.