php update to mysql with array not updating

ajankuv

I have a function that should be updating a row, no errors what so ever in the logs. From what I see it should be working. I took the function from a update user function and tried to mimic it for the new feature.

Here is the data array posting to it.

$data = array('id' =>  $vid, 'name' => $vname, 'logo' => $vlogo, 'info' => $vinfo, 'site' => $vsite, 'est' => $vest);

The post works, I am doing a dump on the updatecompany page. So they do get set. I think it may be with the function. Any insight would be wonderful!

public static function updateCompany($toUpdate = array(), $company = null){
  self::construct();
   if( is_array($toUpdate) && !isset($toUpdate['id']) ){
    if($company == null){
    echo "No company ID set!";
  }
  $columns = "";
  foreach($toUpdate as $k => $v){
    $columns .= "`$k` = :$k, ";
  }

  $sql = self::$dbh->prepare("UPDATE companys SET {$columns} WHERE `id` = :id");
  $sql->bindValue(":id", $company);
  foreach($toUpdate as $key => $value){
    $value = htmlspecialchars($value);
    $sql->bindValue(":$key", $value);
  }
  $sql->execute();

  }else{
  return false;
 }
}



$vid = $_POST["idnum"];
$vname = $_POST["name"];
$vlogo = $_POST["logo"];
$vinfo = $_POST["info"];
$vsite = $_POST["site"];
$vest = $_POST["est"];
Codemole

Your update SQL could not work because you have comma in update value set.

See, you attached comma without any consideration:

  $columns = "";
  foreach($toUpdate as $k => $v){
    $columns .= "`$k` = :$k, ";
  }

Then the final SQL will look something like this:

UPDATE companys SET `name`=:name, `logo`=:logo, WHERE `id`=:id

Do you notice the comma just before WHERE? it should not be there!

So you should update the code like following:

$columns = "";
foreach($toUpdate as $k => $v){
  if ($columns != "") $columns .= ",";
  $columns .= "`$k` = :$k ";
}

This should be working.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Database - mySQL and PHP updating conflicting information on update

From Dev

PHP MySql Database Array update

From Dev

Update MySQL with an Array using PHP

From Dev

update mysql table where rows are not in php array

From Dev

PHP Mysql Update Serialize Array in escape apostrophe

From Dev

JS array to PHP and update table in MYSQL with PDO

From Dev

PDO UPDATE array using php mysql

From Dev

JS array to PHP and update table in MYSQL with PDO

From Dev

PHP MySQL UPDATE query with array and IN statement

From Dev

Updating multiple rows in mysql using php for a json array?

From Dev

PHP - Updating MySQL Database

From Dev

Updating row on mysql php

From Dev

Updating post with PHP & MySQL

From Dev

PHP not updating MySQL Table

From Dev

Updating MySQL entries with PHP

From Dev

Updating MySQL using PHP

From Dev

MySql update query not updating data

From Dev

Array updating temporarily, but not updating permanently in PHP

From Dev

mysql update form using php issue keeps updating with old information not new updats

From Dev

updating the mysql table with php and ajax

From Dev

updating a date type in mysql with php

From Dev

Updating MySQL Database with checkboxes and PHP

From Dev

UPDATING mysql data using PHP

From Dev

Updating Multiple Fields (MySQL) with PHP

From Dev

Php & Mysql Image updating and deleting

From Dev

Updating and Sorting a Multidimensional PHP Array

From Dev

PHP Updating Array Values Issue

From Dev

how to update an array from input box in php mysql

From Dev

update Multiple Rows Into A Mysql Table Using Php Array