Jump to content
Pardon our ads (a necessary update) ×

Help with an update query using a$_SESSION variable


Go to solution Solved by mac_gyver,

Recommended Posts

I'm trying to run the following update query, but I keep getting the error message:

Parse error: syntax error, unexpected string content "", expecting "-" or identifier or variable or number in C:\xampp\htdocs\myreliantservices\samplepage1reworkinclude.php on line 55

$sql1 = "UPDATE cd006ccals SET
ccalcourt    = $_SESSION['vartestccalcourt' . $i]  
WHERE ccalid = ".$key;

I've tried the following without any success:

ccalcourt    = " . $_SESSION['vartestccalcourt' . $i] . "

ccalcourt    = ' . $_SESSION['vartestccalcourt' . $i] . '

ccalcourt    = '$_SESSION["vartestccalcourt" . $i]'

ccalcourt    = '($_SESSION['vartestccalcourt' . $i])'

 

  • Solution

didn't the previous thread show a simple way of doing this?

all you would need to do is go from the example code, that loops over and echos the form data, to code that builds and prepares the query, then executes the query inside the loop with each set of form data -

// build and prepare the sql query statement
$sql = "UPDATE cd006ccals SET ccalcourt=? WHERE ccalid=?";
$stmt = $pdo->prepare($sql);

// example code looping over the form data
foreach(array_keys($_POST['court']) as $index)
{
	// execute the prepared query with each set of form data
	$stmt->execute([ $_POST['court'][$index], $index ]);
}

 

You should follow @mac_gyver's advice.

However, I wanted to weigh in on why your code was failing. When PHP does string interpolation within double quoted strings it will attempt to parse variables. However, when you are dealing with anything but a simple $variable name, you have to take care so they will be interpreted correctly. For example, you can't use and $arrayVariable['foo'] on it's own - you have to leave out the single quotes of the array key OR include it within curly braces, such as:

$string = "Name of user = $user[name]}";
$string = "Name of user = {$user['name']}";

However, you are trying to use a variable within a $variable and have it interpreted within a string:

$sql1 = "UPDATE cd006ccals SET ccalcourt = $_SESSION['vartestccalcourt' . $i] WHERE ccalid = ".$key;

To be honest, I'm not sure if what the format would be to make that work for what you were trying or if it's even possible. I would just create a temp variable and then include that within the full string variable. Again, this is not what you should do with this use case - use prepared statements as noted above.

for ($i=0; $i<5; $i++) {
	$name = $_SESSION['name'.$i];
    $string = "The name as position $i is $name";
    echo $string;
}

 

mac_gyver & gizmola-

Thank you both for all help.  I apologize if I didn't fully understand your responses.  I'm fairly new to php and obviously have a lot to learn.  So, I went back over both threads several times and finally gasped what you were both saying.   I got everything working now.  Again, thank you both for all of your assistance

 

We appreciate your update.   There are many long time contributors like Mac and Psycho who visit the site regularly, and when someone like yourself improves their understanding and manages to build something that solves a problem for them, that makes us really happy. 

One never knows for sure which threads prove to have long term worth, but various topics have been picked up by search engines and the site sees a substantial amount of traffic to some of them, so there could easily be people in the future who you end up helping because their questions are similar to yours.  PHPFreaks is one of a very few remaining sites 100% run by volunteers, which for many of us, is our way of contributing back to the PHP Community that provides the language, libraries, and tools many of us use professionally. 

With AI taking the place for many of what search engines used to provide, along with developer communities like Stackoverflow and phpfreaks, many of the developer specific communities have been closing up, and Reddit has a lot of mindshare especially with younger developers.

PHPFreaks gets a fraction of the number of members and posts we used to see a decade ago.   Since PHPFreaks was always run by the community, we've continued on when many other communities closed.  New PHP developers with questions like yours are the reason the site continues on, and is a nice reminder of why we help keep it running.     

  • Like 1

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.