Solved

What is wrong with this update statement?

Posted on 2016-07-26
6
50 Views
Last Modified: 2016-07-28
Here's my code:

$jorja="update features set featurename='$_POST[feature_name_'.$vivian_row[id]]' where id=$vivian_row['id']";

The error I get is "Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting ']' in C:\wamp\www\kitchen\adm\features_edit.php on line 20"

I don't see what the problem is...
0
Comment
Question by:brucegust
6 Comments
 

Author Comment

by:brucegust
ID: 41730218
I made this change:

$jorja="update features set featurename='$_POST[feature_name_'.$vivian_row['id'].']' where id='$vivian_row[id]'";

Same error.

What?
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 83 total points
ID: 41730219
$jorja="update features set featurename='" . $_POST['feature_name_'.$vivian_row[id]] ."' where id=$vivian_row['id']";
0
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 83 total points
ID: 41730252
NO POINTS

While I am sure Guy's code is good and meets your needs for the purpose of this question, you should really sanitize your data before updating.    Updating or adding raw posted and concatenated data to your database is not very safe.

At the very least, sanitize http://php.net/manual/en/filter.filters.sanitize.php and use an abstraction if you are not already http://php.net/manual/en/intro.pdo.php


$feature_name = $_POST[feature_name];
$new_feature_name = filter_var($feature_name , FILTER_SANITIZE_STRING).$vivian_row[id];
$jorja="update features set featurename='" . $new_feature_name  ."' where id=$vivian_row['id']";
1
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 251 total points
ID: 41730259
There are several problems here, but nothing that can't be simplified and corrected easily.

If you're making reference to $_POST in a query string it almost certainly means your script is vulnerable to external attacks.  You probably want to use MySQLI::real_escape_string() or a similar "minimum-level" method to sanitize the external data.

If you're using an associative array index without quotation marks, you're at risk that a defined constant could cause a name collision, and in any case, your code will raise an unwanted Notice message.  Learn more about how PHP quotes and apostrophes work here.
https://www.experts-exchange.com/articles/12241/Quotation-Marks-in-PHP.html

If you're using compound statements, you're writing code that is brittle and hard to debug.  This is probably the source of the parse error -- it's difficult to see where the quote marks are supposed to go in compound statements and arguments.  So just don't do that!  You can make this easier on yourself by writing simple, unit-level statements.  Please see AntiPractices 9 and 9a.

Here is how I might do it.  You can create different variables, and each of these variables can be fed to var_dump() so you can see what the variable(s) contain.  Much easier than guessing whether your syntax is correct!  For links to the PHP var_dump() man page, please refer back to your recent questions.
// ISOLATE vivian_row id 
$vrid    = $vivian_row['id'];
$ok_vrid = $mysqli->real_escape_String($vrid);

// ISOLATE POST-REQUEST feature_name_
$pfid    = 'feature_name_' . $vrid;
$ok_pfid = $mysqli->real_escape_String($pfid);

// CONSTRUCT THE QUERY -- DO YOU WANT A TABLE SCAN, OR SHOULD YOU CONSIDER USING A "LIMIT 1" CLAUSE??
$jorja   = "UPDATE features SET featurename='$ok_pfid' WHERE id='$ok_vrid'";

Open in new window

0
 

Assisted Solution

by:Vatsal Shah
Vatsal Shah earned 83 total points
ID: 41731197
Please Try Below Code.
$jorja="update features set featurename={$_POST['feature_name_'.$vivian_row['id']]} where id=".$vivian_row['id'];

Open in new window


Regards,
Vatsal
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41733069
To anyone coming across this in the future, the "assisted solution" from Vatsal Shah perpetuates one of the many dangerous practices that novice PHP programmers often follow without understanding the risks.  

Do not use unfiltered values from any external variable (in this case $_POST) in a query string.
1

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now