davulga afyon davulga afyon - 5 months ago 11
PHP Question

php make sql query. but table not updated?

I've class that updates table bu passed arguments in

control
function

<?php

class updte extends ayar
{
function control($adi = null, $baslik = null, $acikla = null, $hakki = null, $telifx = null, $logoc = null)
{
$siteadi = trim($adi);
$sitebaslik = trim($baslik);
$aciklama = trim($acikla);
$hakkinda = trim($hakki);
$telif = trim($telifx);
$logo = trim($logoc);

$durumsor = array();
$extsor = array();
$degersor = array();
if (isset($siteadi)) {
$isle = self::siteadi($siteadi);
array_push($durumsor, $isle["sorgu"]);
array_push($extsor, $isle["ext"]);
array_push($degersor, $isle["deger"]);
}
if (isset($sitebaslik)) {
$isle = self::sitebaslik($sitebaslik);
array_push($durumsor, $isle["sorgu"]);
array_push($extsor, $isle["ext"]);
array_push($degersor, $isle["deger"]);
}
if (isset($aciklama)) {
$isle = self::aciklama($aciklama);
array_push($durumsor, $isle["sorgu"]);
array_push($extsor, $isle["ext"]);
array_push($degersor, $isle["deger"]);
}
if (isset($hakkinda)) {
$isle = self::hakkinda($hakkinda);
array_push($durumsor, $isle["sorgu"]);
array_push($extsor, $isle["ext"]);
array_push($degersor, $isle["deger"]);
}
if (isset($telif)) {
$isle = self::telif($telif);
array_push($durumsor, $isle["sorgu"]);
array_push($extsor, $isle["ext"]);
array_push($degersor, $isle["deger"]);
}
if (isset($logo)) {
$isle = self::favicon($logo);
array_push($durumsor, $isle["sorgu"]);
array_push($extsor, $isle["ext"]);
array_push($degersor, $isle["deger"]);
}
if (!isset($durumsor[0])) {
$hata = "K-0002";
$durum = "Null query";
return (array("err" => $hata, "status" => $durum));
} else {
$hata = null;
$sx = COUNT($durumsor);
$sb = $sx - 1;//Dizi değerden geldiği için dizilerde indis 0 dan başladığı için 1 eksik çekiyoruz
for ($a = 0; $a <= $sb; $a++) {
if ($a == 0) {
$sql = "" . $durumsor[$a] . ",";
$ext = "\"" . $extsor[$a] . "\"=>\"" . $degersor[$a] . "\",";
} elseif ($a != $sb) {
$sql .= $durumsor[$a] . ",";
$ext .= "\"" . $extsor[$a] . "\"=>\"" . $degersor[$a] . "\",";
} else {
$sql .= $durumsor[$a] . "";
$ext .= "\"" . $extsor[$a] . "\"=>\"" . $degersor[$a] . "\"";
}
}
//return(array($sql,$ext));
return self::update($sql, $ext);
}


}

private function update($sql, $ext)
{
try {
$ssql = "\"update siteconfig set " . $sql . "\"";
$dizi = array($ext);
$sor = $this->vt()->prepare($ssql);
$sor->execute(array($ext));
$durum = "successful";
} catch (PDOException $i) {
$hata = "K-0003";
$durum = "Error Detail " . $i->getMessage();
}
if (!isset($hata)) {
return (array("durum" => $durum, $dizi, $ssql));
} else {
return (array("hata" => $hata, "durum" => $durum));
}
}

private function siteadi($i)
{
$stn = "siteadi=:siteadial";
$ext = "siteadial";
return (array("sorgu" => $stn, "ext" => $ext, "deger" => $i));
}

private function sitebaslik($i)
{
$stn = "slogan=:sitebaslik";
$ext = "sitebaslik";
return (array("sorgu" => $stn, "ext" => $ext, "deger" => $i));
}

private function aciklama($i)
{
$stn = "description=:acikla";
$ext = "acikla";
return (array("sorgu" => $stn, "ext" => $ext, "deger" => $i));
}

private function hakkinda($i)
{
$stn = "hakkinda=:hakkindalani";
$ext = "hakkindalani";
return (array("sorgu" => $stn, "ext" => $ext, "deger" => $i));

}

private function telif($i)
{
$stn = "copright=:telif";
$ext = "telif";
return (array("sorgu" => $stn, "ext" => $ext, "deger" => $i));
}

private function favicon($i)
{
$stn = "logo=:favicon";
$ext = "favicon";
return (array("sorgu" => $stn, "ext" => $ext, "deger" => $i));
}

}


But my problem is that when I trying to call update table, I get PDO Exception like:

PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\hesapla\kutuphane\sinif\ayar2.sinif.php on line 148


I tried to debug the arguments that I pass to
self::update($sql, $ext)
.

So here they are:

Array
(
[0] => "update siteconfig set siteadi=:siteadial,slogan=:sitebaslik,description=:acikla,hakkinda=:hakkindalani,copright=:telif,logo=:favicon"
[1] => "siteadial"=>"Acilsağlık.Net","sitebaslik"=>"Sağlık Hesaplama Araçlarıaa","acikla"=>"","hakkindalani"=>"","telif"=>"Tüm hakları saklıdır ","favicon"=>""
)

Answer

As error says:

PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

so as You put in Your answer:

Array
(
    [0] => "update siteconfig set siteadi=:siteadial,slogan=:sitebaslik,description=:acikla,hakkinda=:hakkindalani,copright=:telif,logo=:favicon"
    [1] => "siteadial"=>"Acilsağlık.Net","sitebaslik"=>"Sağlık Hesaplama Araçlarıaa","acikla"=>"","hakkindalani"=>"","telif"=>"Tüm hakları saklıdır ","favicon"=>""
)

See [1] You're passing string, not an array.

So here is solution:

change this:

else
{
    $hata=null;
    $sx=COUNT($durumsor);
    $sb=$sx-1;//Dizi değerden geldiği için dizilerde indis 0 dan başladığı için 1 eksik çekiyoruz
    for($a=0; $a<=$sb; $a++)
    {
        if($a==0)
        {
            $sql="".$durumsor[$a].",";
            $ext="\"".$extsor[$a]."\"=>\"".$degersor[$a]."\",";                 
        }
        elseif($a!=$sb)
        {
            $sql    .= $durumsor[$a].",";
            $ext    .= "\"".$extsor[$a]."\"=>\"".$degersor[$a]."\",";
        }
        else
        {
                $sql .= $durumsor[$a]."";
                $ext .= "\"".$extsor[$a]."\"=>\"".$degersor[$a]."\"";
        }
    }
//return(array($sql,$ext));
return self::update($sql,$ext);
}

to this:

else {
  $sql = array();
  $ext = array();
  for ($a = 0; $a < count($durumsor); $a++) {
    $sql[] = $durumsor[$a];
    $ext[$extsor[$a]] = $degersor[$a];
  }
  $sql = implode(', ', $sql);

  return self::update($sql, $ext);
}
Comments