The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> How to Update multiple columns in PHP, where the input is not empty.
masonh928
post Jan 20 2016, 03:22 PM
Post #1


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



EditProfile

CODE

<?php
session_start();

require_once("Scripts/PHP/Class/Update.class.php");

$DataUpdate = array_filter($_POST);
$Columns = array_keys($DataUpdate);

$ColumnsToUpdate = implode($Columns, ", ");

//$Update = new Update($ColumnsToUpdate, $DataUpdate, $_SESSION['ID']);

?>

DataUpdate is $_POST removing all the empty fields.

$Columns are the columns which are to be updated.

$ColumnsToUpdate should be something like… About, Phone, Gender, etc.

However I want $ColumnsToUpdate to be something like…

About = Hello I was born in *, Phone = 8125633578, Gender = Male, etc.

So I can put that into an Update Query.

Please help thanks…

This post has been edited by masonh928: Jan 20 2016, 03:22 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Jan 20 2016, 03:26 PM
Post #2


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



Please move this to ServerSide Please…
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Jan 20 2016, 03:33 PM
Post #3


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



See if this link helps.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Jan 20 2016, 03:37 PM
Post #4


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



Thanks looking into it!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Jan 20 2016, 03:41 PM
Post #5


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



The problem with that is it's Insert not Update and Update has a very different setup or method of executing.

Insert…
CODE
INSERT INTO Table (col1, col2) VALUES (val1, val2)


Update…
CODE

UPDATE Users SET col1=val1, col2=val2 WHERE ID = 5
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Jan 20 2016, 03:57 PM
Post #6


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(masonh928 @ Jan 20 2016, 02:41 PM) *

The problem with that is it's Insert not Update and Update has a very different setup or method of executing.

Insert…
CODE
INSERT INTO Table (col1, col2) VALUES (val1, val2)


Update…
CODE

UPDATE Users SET col1=val1, col2=val2 WHERE ID = 5


I don't use PDO but from what I understand using arrays should work regardless of INSET of UPDATE. Build the column array with the '=?' placeholders and then use bindValue/bindParam using an array of values in the same order as the column names.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Jan 20 2016, 04:03 PM
Post #7


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



Not in this case, but I will update you later… thanks
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Jan 20 2016, 08:21 PM
Post #8


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



There is another option, just build the query itself in a loop. Sample, not actual code:
CODE
$sql = "UPDATE Users SET"
foreach($_POST as $key => $value)
{
  if($value != '')
  {
   $sql .= ' ' . $key . '=' . $value
  }
  $sql .= " WHERE ID = 5"
}
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Jan 20 2016, 09:19 PM
Post #9


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



You know I just saw this and I had the same idea!

I have this
Class
CODE

<?php
Class Update
{
    private $UpdateColumn = [];
    private $UpdatedData = [];
    private $ID;
    protected $Connect;
    protected $Data = [];
    public $UpdatedQuery;

    public function __construct($UpdateColumn, $UpdatedData, $ID)
    {
        try {
            $this->Connect = new pdo(**PRIVATE**);
            $this->Connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }

        catch(PDOException $ex) {
            die(json_encode(array(
                'outcome' => false,
                'message' => 'Unable to connect'
            )));
        }

        $this->UpdateColumn = $UpdateColumn;
        $this->ID = $ID;
        $this->UpdatedData = $UpdatedData;
    }

    public function bindData()
    {

        // $this->UpdatedData =  strip_tags($this->UpdatedData);

        $this->Data = [$this->UpdateColumn, $this->ID, $this->UpdatedData];
        return $this->Data;
    }

    public function checkForErrors()
    {
        foreach($this->Data as $Data) {
            $Error = false;
            if (empty($Data)) {
                $Error = true;
            }

            if (!is_Numeric($Data[1])) {
                $Error = true;
            }
        } //foreach close
        return $Error;
    } //function close
    
public function bindQuery()
    {
        $this->UpdatedQuery = "";
        foreach($this->Data[2] as $Arr => $Key) {
            $Key = "'" . $Key . "'";
            $this->UpdatedQuery.= $Arr . "=" . $Key . ",";
        }

        $this->UpdatedQuery = rtrim($this->UpdatedQuery, ",");
        $this->UpdatedQuery.= " ";
        return $this->UpdatedQuery;
    }

    public function Update()
    {
        $Query = $this->Connect->prepare("UPDATE Users SET" . $this->UpdatedQuery . "WHERE ID = :ID");
        $Query->bindValue(":ID", $this->Data[1]);
        $Query->execute();
    }

    public function Redirect($MessageToUser)
    {
        $Msg = urlencode($MessageToUser);
        return header("Location: /Profile?ID=$this->Data[1]&&Msg=$Msg");
    }
} //class close

?>


EditProfile
CODE

<?php
session_start();

require_once("Scripts/PHP/Class/Update.class.php");

$DataUpdate = array_filter($_POST);
$Columns = array_keys($DataUpdate);


$Update = new Update($Columns, $DataUpdate, $_SESSION['ID']);

$Update->bindData();

if($Update->CheckForErrors()){
error_reporting(E_ALL);
//$Update->Redirect("Error Could NOT Update");
}

echo($Update->bindQuery());

if($Update->Update()){

$Update->Redirect("Successfully Updated Your Profile");
exit();
}
?>


I don't see where the syntax error lies.

I get:

Weight="165 LBS"

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '='165 LBS' WHERE ID = '5'' at line 1' in /home/u481336533/public_html/Scripts/PHP/Class/Update.class.php:87 Stack trace: #0 /home/u481336533/public_html/Scripts/PHP/Class/Update.class.php(87): PDOStatement->execute() #1 /home/u481336533/public_html/EditProfile.php(21): Update->Update() #2 {main} thrown in /home/u481336533/public_html/Scripts/PHP/Class/Update.class.php on line 87

===============
Weight="165 LBS" is just test content, where's the error?

This post has been edited by masonh928: Jan 20 2016, 09:21 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Jan 20 2016, 09:25 PM
Post #10


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



I used backsticks and it looks pretty good.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Jan 20 2016, 09:27 PM
Post #11


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



Anybody see an error that would cause checkForErrors() return true?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post Jan 20 2016, 09:56 PM
Post #12


Serious Coder
*****

Group: Members
Posts: 253
Joined: 17-August 13
From: Indiana
Member No.: 19,570



Ok I removed the errors function and everything works perfectly, I'll redesign the error method later.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 25th April 2024 - 07:16 AM