Forum Moderators: coopster

Message Too Old, No Replies

incorrect echo and parse error t_constant_encapsed_string

Error message from server side script

         

rogerjh

7:22 pm on Jun 17, 2005 (gmt 0)

10+ Year Member



I am new to PHP/SQL and am hoping (optimistic?) that someone can help me! It might prove a real challenge!

I am using selectdatatest.php to have the user check boxes that will return data from the database table (called islanddata) to the page viewdatatest.php.

I am having two problems. One is that in selectdatatest a validate message is echoed when it shouldn't be:

} else { // if no checkboxes were selected
$name = NULL;
echo 'Please choose which fields you are interested in.';
}

The other problem is a parse error when the viewdatatest script is called: unexpected t_constant_encapsed_string (It does not say what is expected!)

Here is the selectdatatest.php code

<body>
<form name="myform" method="post" action="viewdatatest.php">

Select name that you want to know the data for:<br>

<input type="checkbox" name="name[]" value="James">James<br>
<input type="checkbox" name="name[]" value="Charles">Charles<br>

Select data that you want for the names checked above:<br>

<input type="checkbox" name="dataset[]" value="size_km">Size<br>
<input type="checkbox" name="dataset[]" value="elev_max">Elevation<br>

<?php
// Check wether or not any name has been chosen
if (isset($_POST['name'])) {

// Start building the name selections with name being null
$name = NULL;

// Continually builds the string for each selected value
foreach($_POST['name'] as $key => $value) {

// Assigns a string to $nam, this continually prints the string for so that
// it can be used in a mysql_query. (e.g. if you select both names it prints
// (name = 'James') OR (name = 'Charles') OR
$nam .= "(name = \'" . $value . "\') OR ";

} // ends the foreach

// For the last one it prints all but the last 4 characters, this way it ends
// like this: (name = 'James') OR (name = 'Charles')
// not like this: (name = 'James') OR (name = 'Charles') OR
$nam = substr($nam, 0, -4);
$name = TRUE;

} else { // if no checkboxes were selected
$name = NULL;
echo 'Please choose which fields you are interested in.';
}

$nam = stripslashes($nam);

// Check wether or not any dataset has been chosen
if (isset($_POST['dataset'])) {

// Start building the dataset selections with name being null
$dataset = NULL;

// Continually builds the string for each selected value
foreach ($_POST['dataset'] as $key => $value) {

// Assigns a string to $data, this continually prints the string for so that
// it can be used in a mysql_query. (e.g. if you select both names it prints
// (dataset = 'size_km') OR (dataset = 'elev_max') OR

$data .= "(dataset = \'" . $value . "\') OR ";
} // ends the foreach

// For the last one it prints all but the last 4 characters, this way it ends
// like this: (dataset = 'size_km') OR (dataset = 'elev_max')
// not like this: (dataset = 'size_km') OR (dataset = 'elev_max') OR
$data = substr($data, 0, -4);
$dataset = TRUE;

} else { // if no checkboxes were selected
$dataset = NULL;
echo '<p>Please choose which fields you are interested in.</p>';
}

?>

<input type="submit" name="Submit" value="Submit">
</form>

</body>

And here is the viewdatatest.php code:

<body>

<?php

require_once ('../../mysql_connect.php'); // Connect to the db.

SELECT * FROM 'islanddata' WHERE (dataset = 'size_km') OR (dataset = 'elev_max') OR (name = 'James') OR (name = 'Charles');
$query = "SELECT * FROM 'islanddata' WHERE $nam OR $data"; // Query to return only requested data from selectislanddata.php

$result = @mysql_query($query); // Run the query.

// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$name1 = $row['James'];
$name2 = $row['Charles'];
$data1 = $row['size_km'];
$data2 = $row['elev_max'];

echo "$name1, $name2, $data1, $data2";
}

mysql_free_result ($result); // Free up the resources.

} else { // If it did not run OK.
echo '<p>The island data could not be displayed due to a system error. We apologize for any inconvenience.</p><p>' . mysql_error() . '</p>';
}

mysql_close(); // Close the database connection.

?>
</body>

Please can someone help? Thank you!

sned

7:56 pm on Jun 17, 2005 (gmt 0)

10+ Year Member



Here's one thing that I see:

In your viewdatatest.php:

require_once ('../../mysql_connect.php'); // Connect to the db.

SELECT * FROM 'islanddata' WHERE (dataset = 'size_km') OR (dataset = 'elev_max') OR (name = 'James') OR (name = 'Charles');
$query = "SELECT * FROM 'islanddata' WHERE $nam OR $data"; // Query to return only requested data from selectislanddata.php

Is the line above the $query supposed to be commented out? or is that just a typo?

More to come .. I'm still looking :)
-sned

rogerjh

2:48 am on Jun 18, 2005 (gmt 0)

10+ Year Member



Hi Sned,

Awesome! You are totally correct. That line should have been commented out. Sorry! Now I get a parse error for an unexpected curly brace on line 28 which is:

} else { // If it did not run OK.

It looks okay to me! Is there something above that is wrong? Then if I take out all the lines from //Free the resources to // Close the database connection, this error goes away. (Which are optional anyway right!?) But I get something more worrying which is

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Oops! Does this mean the variables in the script are not being recognized by the database? I really appreciate your help!

grandpa

3:11 am on Jun 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

It means that there are no results from this line:
$result = @mysql_query($query);

Looking back a bit further, we may discover the reason.
$query = "SELECT * FROM 'islanddata' WHERE $nam OR $data";

Specifically, your SELECT statement is still invalid. What fields should $nam and $data be compared with?

This is what the query should look like. Replace the fields names that I used (name,data) with the proper field names from your table.
$query = "SELECT * FROM 'islanddata' WHERE name = '$nam' OR data = '$data' ";

rogerjh

1:19 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Hi grandpa,

Thank you for your help with this, but now I am really stuck.

When I run the script I get an error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''islanddata' WHERE name= OR dataset=' at line 1

Someone else who is helping me says that the code you suggested won't work because if you select more then one name (in selectdata.php) you end up with something of this sort:

$query = "SELECT * FROM 'islanddata' WHERE name = 'James Charles' OR data = 'dataset1 dataset2";

which won't work. Please can you help me some more? Can I give you the URL of the index where these pages are so you can try them out? Maybe that will help you figure out where I am going wrong. I'd really appreciate your help trying to solve this problem!

jatar_k

5:01 pm on Jun 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



there are 2 problems in that

$query = "SELECT * FROM 'islanddata' WHERE name = 'James Charles' OR data = 'dataset1 dataset2";

missing closing quote around dataset1 dataset2 and single quotes around the tablename so this would be better

$query = "SELECT * FROM islanddata WHERE name = 'James Charles' OR data = 'dataset1 dataset2'";