I have the exact question as How do I prevent MySQL from updating column with an empty string
However, the only twist is that I am applying this to PDO instead of the conventional MySQL.
I have followed the top answer on that question regarding adding IF statements to check if the length is 0. This does not seem to work for PDO however since no update occurs into the table and the webpage outputs: Array ( [0] => HY093 [1] => [2] => ) 1. Note I am using prepared statements via the bind function.
My PHP code with the IF statement included:
<?php
$pdo=new PDO("mysql:dbname=createyo_TestDatabase;host=localhost","createyo_james","password");
$statement=$pdo->prepare(
"UPDATE `Users`
SET `EditIDs` = IF(LENGTH(':EditID')=0, EditIDs, ':EditID'),
`ArticleIDs` = IF(LENGTH(':ArticleID')=0, ArticleIDs, ':ArticleID'),
`Reputation` = IF(LENGTH(':Reputation')=0, Reputation, ':Reputation'),
`VotedUpArticleIDs` = IF(LENGTH(':VotedUpArticleID')=0, VotedUpArticleIDs, ':VotedUpArticleID'),
`VotedUpEditIDs` = IF(LENGTH(':VotedUpEditID')=0, VotedUpEditIDs, ':VotedUpEditID'),
`VotedDownArticleIDs` = IF(LENGTH(':VotedDownArticleID')=0, VotedDownArticleIDs, ':VotedDownArticleID'),
`VotedDownEditIDs` = IF(LENGTH(':VotedDownEditID')=0, VotedDownEditIDs, ':VotedDownEditID'),
WHERE `UserID` = :UserID");
$statement->bindValue(':EditID', (string) trim($_GET['EditID']), PDO::PARAM_STR);
$statement->bindValue(':ArticleID', (string) trim($_GET['ArticleID']), PDO::PARAM_STR);
$statement->bindValue(':Reputation', (string) trim($_GET['Reputation']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpArticleID', (string) trim($_GET['VotedUpArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpEditID', (string) trim($_GET['VotedUpEditID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownArticleID', (string) trim($_GET['VotedDownArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownEditID', (string) trim($_GET['VotedDownEditID']), PDO::PARAM_STR);
$statement->bindValue(':UserID', (string) trim($_GET['UserID']), PDO::PARAM_STR);
$statement->execute() or die(print_r($statement->errorInfo()));
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);
print $json;
?>
Edit
I am now receiving a syntax error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE UserID = '1234'' at line 9. I have not been able to understand this error.
New PHP code:
$statement=$pdo->prepare(
"UPDATE `Users`
SET `EditIDs` = IF(LENGTH(:EditID)=0, EditIDs, :EditID1),
`ArticleIDs` = IF(LENGTH(:ArticleID)=0, ArticleIDs, :ArticleID1),
`Reputation` = IF(LENGTH(:Reputation)=0, Reputation, :Reputation1),
`VotedUpArticleIDs` = IF(LENGTH(:VotedUpArticleID)=0, VotedUpArticleIDs, :VotedUpArticleID1),
`VotedUpEditIDs` = IF(LENGTH(:VotedUpEditID)=0, VotedUpEditIDs, :VotedUpEditID1),
`VotedDownArticleIDs` = IF(LENGTH(:VotedDownArticleID)=0, VotedDownArticleIDs, :VotedDownArticleID1),
`VotedDownEditIDs` = IF(LENGTH(:VotedDownEditID)=0, VotedDownEditIDs, :VotedDownEditID1),
WHERE `UserID` = :UserID");
$statement->bindValue(':EditID', (string) trim($_GET['EditID']), PDO::PARAM_STR);
$statement->bindValue(':ArticleID', (string) trim($_GET['ArticleID']), PDO::PARAM_STR);
$statement->bindValue(':Reputation', (string) trim($_GET['Reputation']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpArticleID', (string) trim($_GET['VotedUpArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpEditID', (string) trim($_GET['VotedUpEditID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownArticleID', (string) trim($_GET['VotedDownArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownEditID', (string) trim($_GET['VotedDownEditID']), PDO::PARAM_STR);
$statement->bindValue(':EditID1', (string) trim($_GET['EditID']), PDO::PARAM_STR);
$statement->bindValue(':ArticleID1', (string) trim($_GET['ArticleID']), PDO::PARAM_STR);
$statement->bindValue(':Reputation1', (string) trim($_GET['Reputation']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpArticleID1', (string) trim($_GET['VotedUpArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpEditID1', (string) trim($_GET['VotedUpEditID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownArticleID1', (string) trim($_GET['VotedDownArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownEditID1', (string) trim($_GET['VotedDownEditID']), PDO::PARAM_STR);
WHEREclause.