/    Sign up×
Community /Pin to ProfileBookmark

How to make this code better

I mean simplifying this PHP code that contains some MySQL script.

[code=php] $query = “UPDATE users SET street = IF(?=”, street, ?), city = IF(?=”, city, ?), province = IF(?=”, province, ?), postal = IF(?=”, postal, ?), country = IF(?=”, country, ?), cpnum = IF(?=”, cpnum, ?), phone2 = IF(?=”, phone2, ?), email = IF(?=”, email, ?), status = IF(?=”, status, ?), salt = IF(?=”, salt, ?), password = IF(?=”, password, ?), info_updated = NOW() WHERE username = ?”;
$stmt = $tubcConnection->prepare($query);
$stmt->bind_param(‘ssssssiisssssssssssssss’, $credentials[‘street’], $credentials[‘street’],
$credentials[‘city’], $credentials[‘city’],
$credentials[‘province’], $credentials[‘province’],
$credentials[‘postal’], $credentials[‘postal’],
$credentials[‘country’], $credentials[‘country’],
$credentials[‘phone’], $credentials[‘phone’],
$credentials[‘phone2’], $credentials[‘phone2′],
$credentials[’email’], $credentials[’email’],
$credentials[‘status’], $credentials[‘status’],
$secured_pass[0], $secured_pass[0], $secured_pass[1],
$secured_pass[1], $user
);[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmDec 30.2015 — #1 - I would remove all the logic from the query statement build and instead create the values you want to use in your query as php vars and then insert THEM in the statement. Your code as it stands is too difficult to follow.

Show us what you get then and perhaps we can see other improvements.


PS - why do you not hard-code the table fieldnames you are updating instead of using a bound parm? In fact, can you even do that with a prepared query?
Copy linkTweet thisAlerts:
@NogDogDec 30.2015 — How adverse would you be to using PDO instead of MySQLi? I think it would be easier to deal with a variable number of arguments when binding parameters, allowing you to create the list of fields to be set on the fly and avoiding all those IF statements in the query itself.
Copy linkTweet thisAlerts:
@tubc2015authorDec 31.2015 — Could you please provide me a code of its PDO version so that I would be shorter for me to read.
Copy linkTweet thisAlerts:
@NogDogDec 31.2015 — Untested, but here's what I'm thinking of:
[code=php]
$fields = array(
':street' => $credentials['street'],
':city' => $credentials['city'],
':province' => $credentials['province'],
':postal' => $credentials['postal'],
':country' => $credentials['country'],
':cpnum' => $credentials['phone'],
':phone2' => $credentials['phone2'],
':email' => $credentials['email'],
':status' => $credentials['status'],
':salt' => $secured_pass[0],
':password' => $secured_pass[1]
);
foreach($fields as $key => $value) {
if(trim($value) === '') {
unset($fields[$key]);
}
}
if(count($fields) == 0) {
// nothing to do, so some sort of error here?
}
else {
$pdo = new PDO($dsn, $dbUser, $dbPass); // probably in a config file?
$sql = "UPDATE users SET";
foreach($fields as $key => $value) {
$sql .= "n".substr($key, 1)." = $key";
}
$sql .= "nWHERE username = :username";
$fields[':username'] = $user;
$stmt = $pdo->prepare($sql);
if($stmt == false) {
throw new Exception(print_r($pdo->errorInfo().PHP_EOL.$sql, 1));
}
if($stmt->execute($fields) == false) {
throw new Exception(print_r($stmt->errorInfo().PHP_EOL.$sql, 1));
}
}
[/code]
×

Success!

Help @tubc2015 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...