Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP update statement with arrays

Hi so I have an insert statement which works well, but need to create a separate update function which uses array keys and array values, which would be quite like the insert function but updates.

I have this for my insert

$sql = "INSERT INTO $tablename (".implode(",", array_keys($DATA).")" . " DATA ('".implode("','",array_values($DATA))."')";
connect()->query($sql); 

This is what I have for my update so far but am stuck with it,

<?php
    function updatethis (array $id, array $values, $tablename)
    {
        $sql = "UPDATE $tablename SET (".implode(",", array_keys($DATA)).")" . " DATA ('".implode("','",array_values($DATA))."')";
        dbconnect()->query($sql); 
    } 
?>

Therefore I would like help on the update feature please .

So I am getting an error with the UPDATE syntax

This is the part i am struggling with, i cna give further explanation, but i must have put in the wrong syntax to update the database after i click edit on the index page it calls the function just the syntax is incorrect.

also its php to mySQL

index page for PHP updatee fucntion

 {
     $values = array();
     $idValues = array($idColumn => $id);
     foreach($_POST as $key => $value)
     {
         if(!empty($value) && ($value != "Submit"))
         {
             $values[$key] = $value;
         }
     } 
     $result = update($idValues, $values, $tableName);
 }

Edit: Error I am getting edit has not been successfull from below

  if(isset($_POST['Submit']))
                {
                    if($result>0)
                    {
                        echo 'Edit has been successful.  Return to index page';
                    }
                    else
                    {
                        echo 'Edit has not been successful.';
                    }
                }

My code

function updateAll(array $id, array $values, $tablename)
{
    $sIDColumn  = key($id);
    $sIDValue   = current($id);
    $arrayValues = $values;
    array_walk($values, function(&$value, $key){
        $value = "{$key} = '{$value}'";
    });
    $sUpdate = implode(", ", array_values($values));
    $sql        = "UPDATE {$tablename} SET {$sUpdate} WHERE {$sIDColumn} = '{$sIDValue}'";


    connect()->query($sql);
}

My aim: takes the input of the unique identifier of the row to be edited as an array of 1 then the value plus the name of the column representing the primary key, an array containing the values indexed by the column names as well as a string representing the table name useing array_keys and array_vaules like th insert but to update

like image 808
blueboy123 Avatar asked Jan 08 '23 01:01

blueboy123


1 Answers

You cannot UPDATE in the same way of INSERT. It should be like this :

$valueSets = array();
foreach($values as $key => $value) {
   $valueSets[] = $key . " = '" . $value . "'";
}

$conditionSets = array();
foreach($id as $key => $value) {
   $conditionSets[] = $key . " = '" . $value . "'";
}

$sql = "UPDATE $tablename SET ". join(",",$valueSets) . " WHERE " . join(" AND ", $conditionSets);

See details here http://dev.mysql.com/doc/refman/5.7/en/update.html

like image 75
Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Avatar answered Jan 16 '23 21:01

Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ