What Is Wrong With This Particular PDO (SQL) Statement? - php

Very sorry to be asking one of these questions, but I have been trying for the past hour to figure this one out and I am just plain stuck.
I am trying to update a lot of columns at the same time. I am using PDO.
Some PHP code:
$db=db_connect();
$wid=$_POST['wid'];
$time=time();
$username=$_SESSION['s_username'];
$arr=explode("|", $_POST['idata']);
array_push($arr, $time,$username,$wid );
$statement = $db->prepare("UPDATE widgets SET file_id=?,delay=?,title=?,instructions=?,width=?,height=?,bg_color=?,bg_image=?,border-radius=?,offer_table_border_width=?,offer_table_border_color=?,offer_table_link_color=?,offer_table_link_hover_color=?,offer_table_bg1=?,offer_table_bg2=?,head_font_size=?,head_font_color=?,instructions_font_size=?,instructions_font_color=?,time=? WHERE username=? AND wid=?");
$statement->execute($arr);
var_dump($arr);
The code comes from an AJAX request in the format "var1|var2|var3..." etc. This information is split by the "|" delimiter and then a few variables are added, then the table should be updated.
Here is the result of the var_dump (this is what $arr equates to):
array(22) { [0]=> string(2) "40" [1]=> string(1) "0" [2]=> string(9) "My Title!" [3]=> string(16) "My Instructions!" [4]=> string(3) "700" [5]=> string(3) "500" [6]=> string(4) "#FFF" [7]=> string(27) "http://background-image.png" [8]=> string(1) "0" [9]=> string(1) "1" [10]=> string(7) "#81ABD6" [11]=> string(7) "#0588C6" [12]=> string(7) "#81ABD6" [13]=> string(4) "#FFF" [14]=> string(7) "#DDEEFF" [15]=> string(2) "24" [16]=> string(7) "#2779AA" [17]=> string(2) "18" [18]=> string(7) "#2779AA" [19]=> int(1349486490) [20]=> string(15) "myuser" [21]=> string(6) "2LvS4c" }
And once again, the PDO statement:
$statement = $db->prepare("UPDATE widgets SET file_id=?,delay=?,title=?,instructions=?,width=?,height=?,bg_color=?,bg_image=?,border-radius=?,offer_table_border_width=?,offer_table_border_color=?,offer_table_link_color=?,offer_table_link_hover_color=?,offer_table_bg1=?,offer_table_bg2=?,head_font_size=?,head_font_color=?,instructions_font_size=?,instructions_font_color=?,time=? WHERE username=? AND wid=?");
The result from this: There are NO ERRORS returned from errorInfo(). There IS a row where username="myuser" and wid="2LvS4c", but it is simply not updated. There must be some sort of syntax error that I can't see, and is not being returned with errorInfo().
I tried with a simpler query:
$statement = $db->prepare("UPDATE widgets SET file_id=? WHERE username=? AND wid=?");
$statement->execute(array($arr[0], $username, $wid));
And it works like a charm! So what is wrong with the big query? Thanks!

This page lists the allowable characters in identifiers for MySQL, and - (minus) doesn't appear to be one of them.
First, are you certain that border-radius is the correct column name? It uses a minus sign whereas all the others use the underscore, _. At a bare minimum, you should maintain some consistency in your naming.
Other than that, start removing one column at a time from the update statement until it works. If it's a problem with a specific column (or possibly command length, however unlikely), this sort of "change one thing and retry" strategy can pinpoint the problem.

Related

How do I get all GET values from a URL? (Missing Values)

I am looping through GET values and only getting 13 values, not matter what URL I submit. Also, it is not getting the values in sequential order...
When I loop through I only get 13 values, as well as when I use a var_dump on $_GET itself; even though there are many more values to retrieve.
Here is the URL:
website.com/Questionaire.php?SurveyName=TV%20Quiz&SurveyType=MultipleChoice&Q1=Choose%20a%20character%20off%20of%20Happy%20Days?&A1=Benny%20the%20bull&A2=The%20Fonz&A3=Jack%20Cracker&Q3=Favorite%20Friends%20character?&A1=Ross&A2=Monica&A4=Joey&A5=Rachel&A6=Chandler&A7=Phoebe&Q8=Favorite%20Nickelodeon%20show?&A1=Hey%20Arnold!&A2=Rugrats&A8=Ahhhh!%20Real%20Montsters
Here are my results:
SurveyName: TV Quiz
SurveyType: MultipleChoice
Q1: Choose a character off of Happy Days?
A1: Hey Arnold!
A2: Rugrats
A3: Jack Cracker
Q3: Favorite Friends character?
A4: Joey
A5: Rachel
A6: Chandler
A7: Phoebe
Q8: Favorite Nickelodeon show?
A8: Ahhhh! Real Montsters
As you can see the results are not in sequential order and some values are even missing.
var_dump($_GET);
foreach ($_GET as $key => $value) {
echo $key.": ".$value."<br/>\n";
}
I expect these results:
SurveyName=TV Quiz
SurveyType=MultipleChoice
Q1=Choose a character off of Happy Days?
A1=Benny the bull //<- missed
A2=The Fonz //<- missed
A3=Jack Cracker
Q3=Favorite Friends character?
A1=Ross //<- missed
A2=Monica //<- missed
A4=Joey
A5=Rachel
A6=Chandler
A7=Phoebe
Q8=Favorite Nickelodeon show?
A1=Hey Arnold!
A2=Rugrats
A8=Ahhhh! Real Montsters
You cannot have identical parameter names in your query string, otherwise the last value will overwrite the previous ones. You need to have unique answer names or you will lose data. You can imagine PHP adding the parameters to $_GET with the following pseudo-code:
foreach($param as $key=>$val) {
$_GET[$key] = $val;
}
Because of this, parameters appear in the order in which they first show up in the request. So the query string ?A=1&B=2&A=3&C=4 will have A appear first, then B, and finally C. The last value for an identical parameter is the one used, so we get the following $_GET result:
array(
'A'=>3,
'B'=>2,
'C'=>4
);
Consider adding the question identifier as a prefix for each answer. For example, instead of A1 do Q1A1 and Q2A1. This will ensure that your data is not overwritten.
I would suggest using array notation for query string parameter names so that order is maintained. Something like:
?SurveyName=TV Quiz
&SurveyType=MultipleChoice
&Q[1]=Choose a character off of Happy Days?
&A[1][1]=Benny the bull
&A[1][2]=The Fonz
&A[1][3]=Jack Cracker
&Q[3]=Favorite Friends character?
&A[3][1]=Ross
&A[3][2]=Monica
&A[3][4]=Joey
&A[3][5]=Rachel
&A[3][6]=Chandler
&A[3][7]=Phoebe
&Q[8]=Favorite Nickelodeon show?
&A[8][1]=Hey Arnold!
&A[8][2]=Rugrats
&A[8][8]=Ahhhh! Real Montsters
When you name query string parameter like that, PHP will parse them into arrays:
array(4) {
["SurveyName"]=>
string(7) "TV Quiz"
["SurveyType"]=>
string(14) "MultipleChoice"
["Q"]=>
array(3) {
[1]=>
string(37) "Choose a character off of Happy Days?"
[3]=>
string(27) "Favorite Friends character?"
[8]=>
string(26) "Favorite Nickelodeon show?"
}
["A"]=>
array(3) {
[1]=>
array(3) {
[1]=>
string(14) "Benny the bull"
[2]=>
string(8) "The Fonz"
[3]=>
string(12) "Jack Cracker"
}
[3]=>
array(6) {
[1]=>
string(4) "Ross"
[2]=>
string(6) "Monica"
[4]=>
string(4) "Joey"
[5]=>
string(6) "Rachel"
[6]=>
string(8) "Chandler"
[7]=>
string(6) "Phoebe"
}
[8]=>
array(3) {
[1]=>
string(11) "Hey Arnold!"
[2]=>
string(7) "Rugrats"
[8]=>
string(21) "Ahhhh! Real Montsters"
}
}
}

PHP: Incorrect value after assignment

I have really no idea what's happening. The story:
I use PDO for a SELECT statement on a database.
$sql = "SELECT a,b,c,performance,points,compare
FROM normvalues
WHERE x=:x AND y=1 AND z=:z";
$stmt = $GLOBALS['PDO']->prepare($sql);
$stmt->bindParam(":x",$x);
$stmt->bindParam(":z",$z);
$stmt->execute();
$res=$stmt->fetchAll(PDO::FETCH_ASSOC);
So that's fine and it is working. When I var_dump the $res variable I get something like:
array(6) {
["a"]=> string(2) "44"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "50.1"
["points"]=> string(1) "1"
["compare"]=> string(2) "-1"
}
[1]=>
array(6) {
["a"]=> string(2) "57"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "47.7"
["points"]=> string(1) "2"
["compare"]=> string(2) "-1"
}
[2]=>
array(6) {
["a"]=> string(2) "70"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "44.7"
["points"]=> string(1) "3"
["compare"]=> string(2) "-1"
}
...
That's also okay. But I have to sort the result another way. So I am doing:
foreach($res as $e){
$this->normTable[$e['a']][$e['points']]=$e['performance'];
$this->normTable[$e['a']]['compare']=$e['compare'];
}
And now I am completely lost. By assigning $e['performance'] I get wrong values. Actually this should be the performance values.
[1176]=>
array(4) {
[1]=> string(2) "50"
["compare"]=> string(2) "-1"
[2]=> string(2) "48"
[3]=> string(2) "45"
}
I already checked the values in the database and they are correct. By doing doubleval() I'd get the right values, but the problem is that not every value is a double but also integer or string. I also tried to typecast with (string) but it's the same result. I have no explanation.
Update:
It's a very big project and I just tried to minimize it as possible and to make my problem as clear as possible. But now I have figured out something new:
I do an 'echo()' of my first variable in the normTable during the loop:
foreach($res as $e){
$this->normTable[$e['a']][$e['points']]=$e['performance'];
echo "a:".$e['a']." pt: ".$e['points']." perf: ".$e['performance']."-".$this->normTable[1176][1]."\n";
$this->normTable[$e['a']]['compare']=$e['compare'];
}
and the value is changing from '50.1' to '50'. Still can't figure out the reason. Is there a size limitaion of arrays in PHP?
UPDATE 2 and a big SORRY!
As I said, it is a big project. So the table I read out, has some values for some attributes twice or more. Actually such a case should not happen. That's why the answer is simple: It became 50 because 50 was assigned. I'm so sorry for having waisted your time. But I totally excluded this case and since I am also coding in C, my first thought was: memory leak - clear case!
Thanks for your help.
Meh, I don't have 50 reputation; can't comment only answer.
If you replace a with be in your loop, you should get your expected result (given your data sample). I.e.:
foreach($res as $e){
$this->normTable[$e['b']][$e['points']]=$e['performance'];
$this->normTable[$e['b']]['compare']=$e['compare'];
}
But I'm not sure this really solves your problem. YMMV.

Syntax for using an array with IN as parameter

Probably a no-brainer but google didn't help me the last 30 minutes so maybe someone has done this already and can nudge me to the right syntax;
I have a typical php-array like
$idArray = array(7) { [0]=> string(13) "55b753312986c" [1]=> string(13) "55b751f83d9f3" [2]=> string(13) "55b752b6ab7fd" [3]=> string(13) "55b7531fbb5ad" [4]=> string(13) "55b75346baae8" [5]=> string(13) "55b752c9aea0a" [6]=> string(13) "55b753417d577" }
I want to ask in the query if a property is in this array. I tried to put this into a parameter like
$queryParam = array("selectedID" => $idArray);
Next I wanted to query my DB like
MATCH (u:User) --> (n:Node)
WHERE u.id IN {selectedID}
RETURN u
Somehow the syntax is incorrect and I would appreciate a hint how to type the query accurately. Thanks very much.

How do I get the data out of this array? Single sign on

I have implemented a Single Sign on function using Janrain. It outputs this data (amongst other data). Is it possible to break this down and extract only the displayName for example so that I may place it in a variable?
auth_info:
array(3) {
["stat"]=>
string(2) "ok"
["profile"]=>
array(10) {
["providerName"]=>
string(8) "Facebook"
["identifier"]=>
string(48) "removed"
["preferredUsername"]=>
string(10) "OllieJones"
["displayName"]=>
string(11) "Ollie Jones"
["name"]=>
array(3) {
["formatted"]=>
string(11) "Ollie Jones"
["givenName"]=>
string(5) "Ollie"
["familyName"]=>
string(5) "Jones"
}
Here is the script that creates it and where I would like to define the variable https://github.com/janrain/Janrain-Sample-Code/blob/master/php/rpx-token-url.php
Many Thanks
Try $auth_info['profile']['displayName']

PHP return object variables inside of an array

I'm writing a PHP script to work with some JSON data. Below is an (abridged) var_dump($data). I want to return the value associated with ["[question(13), option(0)]"] which is 20. I can't figure out how to do it. I've tried $data->[question(13), option(0)] and $data->question(13). (I tried to look this up but I'm not sure what the notation means, so I'm not sure what I'm looking for)
object(stdClass)#133 (36) {
["id"]=>
string(1) "1"
["contact_id"]=>
string(0) ""
["status"]=>
string(8) "Complete"
["is_test_data"]=>
string(1) "0"
["datesubmitted"]=>
string(19) "2012-04-19 17:11:00"
["[question(5)]"]=>
string(11) "C. 40%, 40%"
["[question(9)]"]=>
string(47) "D. EBITDA and Free cash flow are the same thing"
["[question(10)]"]=>
string(48) "A. Accounts Payable as % of sales would increase"
["[question(11)]"]=>
string(20) "E. None of the above"
["[question(12)]"]=>
string(97) "A. A larger portion of initial investment is equity which can increase exit return potential."
["[question(13), option(0)]"]=>
string(2) "20"
["[url("embed")]"]=>
string(0) ""
["[variable("STANDARD_IP")]"]=>
string(13) "38.107.74.230"
["[variable("STANDARD_LONG")]"]=>
string(10) "-73.976303"
["[variable("STANDARD_LAT")]"]=>
string(9) "40.761902"
}
Either use extended object access notation:
$data->{'[question(13), option(0)]'}
Or just ask for normal array and use it as normal array.
json_decode($json, true);
try this
echo $data->{'[question(13), option(0)]'};

Categories