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.
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.
<?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
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
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.
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.
$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.