Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/280725-parse-error-syntax-error/
Share on other sites

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'; ...

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 by KubeR

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;
}

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 ?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.