Array help

Hey,
Ihave just written the following extract and am getting a Parse Error. I know the code I ahve writen is not the best way to go about what I am doing but it will be fine for what I need for the time being.

	$questionnaire_users = $_POST['questionnaire_users'];
	
	$users = explode('|', $questionnaire_users);
	$validusers = NULL;
	
	foreach($users as $user) {
		$sql = "SELECT * FROM users WHERE email = '$user'";
		$result = mysql_query($sql) or die(mysql_error());
		$row = mysql_fetch_row($result);
		$bits = explode('|', $user);
		$pw = $bits[0];
		if (mysql_num_rows($result) == 0) {
			//insert temporary user
			$sql = "INSERT INTO users VALUES('$user', '$pw')";
			$result = mysql_query($sql) or die(mysql_error());
		} else {
			$validuser = $row['id'];
			$validusers .= $validuser.',';
		}
	}
	$bits2 = explode(',' $validusers);
	$valid = implode('-', $bits2);

	$sql = "INSERT INTO questionnaires (name, description, userid) VALUES ('$questionnaire_name', '$questionnaire_desc', '$user_id', '$valid')";

The error is occuring on the line beginning with $bits2

I guess it’s occuring because it’s trying to use $validusers but $validusers is within the foreach loop. How can I get around this?

What is the exact error?

Start using echo $somevar; and echo var_dump($somevar); statements to get the contents/output of variable to verify you’re getting what you’re expecting.

Here is the full code of that page:

<?php
session_start();
$user_id = $_SESSION['user_id'];
echo $user_id;
require_once('db.php');

if (isset($_POST['submitted']) == 1) {

	$questionnaire_name = $_POST['questionnaire_name'];
	$questionnaire_desc = $_POST['questionnaire_desc'];
	$questionstouse = $_POST['questionstouse'];
	$questionnaire_users = $_POST['questionnaire_users'];
	
	$users = explode('|', $questionnaire_users);
	$validusers = NULL;
	
	foreach($users as $user) {
		$sql = "SELECT * FROM users WHERE email = '$user'";
		$result = mysql_query($sql) or die(mysql_error());
		$row = mysql_fetch_row($result);
		$bits = explode('|', $user);
		$pw = $bits[0];
		if (mysql_num_rows($result) == 0) {
			//insert temporary user
			$sql = "INSERT INTO users VALUES('$user', '$pw')";
			$result = mysql_query($sql) or die(mysql_error());
		} else {
			$validuser = $row['id'];
			$validusers .= $validuser.',';
		}
	}
	$bits2 = explode(',' $validusers);
	$valid = implode('-', $bits2);

	$sql = "INSERT INTO questionnaires (name, description, userid) VALUES ('$questionnaire_name', '$questionnaire_desc', '$user_id', '$valid')";
	
	
	
	
	$result = mysql_query($sql) or die(mysql_error().'SQL:'.$sql);
	
	$questionnaire_id = mysql_insert_id();
	
	foreach($questionstouse as $question_id) {
		$sql2 = "INSERT INTO question_questionnaire (question_id, questionnaire_id) VALUES ('$question_id', '$questionnaire_id')";
		$result2 = mysql_query($sql2) or die(mysql_error());
	}
	

	
	
	if ($result){
		echo '<h2>Thanks!</h2><p>Your questionnaire has been created</p><br /><br /><a href="index.php">Click here to go back to the homepage</a>';
	}
	
} else {

	require_once("create_questionnaire_form.php");

}

?>

You will notice that a few lines from the top of the code I ahve added a echo to print out the $user_id variable. It is not even printing out that echo so I ahve no idea what is wrong. The error just states a parse error in this file on line 32 which is the one which starts with $bits2

Just printing out the value of a single variable won’t cut it. When debugging, you have to look at any variable that might be involved.

I’d suggest starting with $validusers before you try to explode it.

(aside): A var_dump shows the contents of a variable, which is especially handy when dealing with non-primitive datatypes (such as arrays or objects).

I understand I need to echo several variables and do those checks but why won;t even that first simple echo even print anything out?

Thats whats bugging me!

use var_dump to see if anything is even being set. I suspect $_SERVER[‘user_id’] might be empty.

I just updated the first few lines of the page to this:

<?php
session_start();
if (isset($_SESSION['user_id'])) {
	$user_id = $_SESSION['user_id'];
} else {
	echo 'Error with session';
}

echo var_dump($user_id);
require_once('db.php');

if (isset($_POST['submitted']) == 1) {

It now has a check to see if the session is set and if not then echos out an error. I don;t get shown the error so i assume the session is set fine. I tried altering the echo to var_dump as you can see and I still get nothing outputted except the error on the $bits2 variable line I posted in my previous post

You’ll need to initialize the $user_id variable outside of the if-statement. As it stands, the only place the variable exists is within that if-statement.

In any case, for now, I’d remove the if-statements you put in until you’ve finished troubleshooting. Go back to your first version then do a var_dump with $user_id

Conditionals do not create their own scope in PHP, $user_id exists in the global scope

var_dump() directly prints the dump, it returns a void, you don’t need to echo it

yeh, i thought they were global. i have removed the echo for the var dump but still get no output!

If you’re just getting blank pages where you expect output, there’s probably a fatal error occurring (when an error happens during parsing then execution stops before any instructions are actually run, so what you write can’t produce output) and display_errors is set to Off. Reload the webpage then check the end of PHP’s error log.

whats bugging me is that the error is happening at line 30 odd but it won’t echo or var dump a variable at line 4 or 5!

All i see when i refresh the page is the parse error saying it is on line 30 odd

Like I said, a parse error means the interpreter stops before it gets to executing any code.

Your entire file is parsed before execution begins. It doesn’t just start running line 1 then line 2 then line 3… or you wouldn’t be able to call a function defined in a different file or later in the file.

Which line is 30 and what is the exact error?

It’s here:

    $bits2 = explode(',' $validusers); 

Missing comma between arguments.

Never knew that. Must have been something I carried over from my Java days.

OMG!!! - Why can’t I spot these things?

I’m now getting a “column count does not match value count at line 1” error. What is this referring to?

  $sql = "INSERT INTO users VALUES('$user', '$pw')"; 

Your users table probably has more than two columns.

You really need to start calling mysql_real_escape_string() on all user input before you put it into a query, too, or start using prepared statements. Right now I can easily break your database even after you fix the syntax errors.