v2.5.2
Giriş yap

Array to string conversion Hatası

coder
427 defa görüntülendi

Merhaba.

Güncelleme yapmak istediğimde Database.php dosyasının bindValue() kısmında Array to string conversion hatasını alıyorum.

Model dosyasına gönderilen verilerden kaynaklandığını düşünüyorum.
Uğraştım ama çözümünü bulamadım.

Database.php

<?php

class Database {
    private $host = DB_HOST;
    private $user = DB_USERNAME;
    private $pass = DB_PASSWORD;
    private $dbname = DB_NAME;
    private $dbcharset = DB_CHARSET;

    private $dbh;
    private $error;
    private $stmt;

    public function __construct() {
        // Set Dsn
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname . ';charset=' .$this->dbcharset;
        $options = array (
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );

        // Connect
        try {
            $this->dbh = new PDO ($dsn, $this->user, $this->pass, $options);
        }
        catch ( PDOException $e ) {
            $this->error = $e->getMessage();
        }
    }

	public function query($sql, $args = false) {
		$this->stmt = $this->dbh->prepare($sql);
		if($args == true){
			foreach($args as $key => $val){
				$this->bind(':'.$key, $val, null);
			}
		}
	}

	// Bind values
	public function bind($param, $value, $type = null) {
		if (is_null ($type)) {
			switch (true) {
				case is_int ($value) :
					$type = PDO::PARAM_INT;
					break;
				case is_bool ($value) :
					$type = PDO::PARAM_BOOL;
					break;
				case is_null ($value) :
					$type = PDO::PARAM_NULL;
					break;
				default :
					$type = PDO::PARAM_STR;
			}
		}

		$this->stmt->bindValue($param, $value, $type);
	}

	// Execute
	public function execute(){
		try {
			return $this->stmt->execute();
		}
		catch ( PDOException $e ) {
			echo $this->error = $e->getMessage();
			die();
		}
	}
}

Controller Dosyası

<?php

public function uyeduzenle($id)
{
        
	if($_POST){

		$dataset = [
			'kadi'      => form::post("kadi"),
			'eposta'    => form::post("eposta"),
			'ad'        => form::post("ad"),
			'soyad'     => form::post("soyad"),
			'sinif'     => isset($_POST["sinif"]) ? form::post("sinif") : "",
			'kategori'  => isset($_POST["kategori"]) ? form::post("kategori") : "",
			'durum'     => form::post("durum"),
			'uye_id'    => $id
		];

		$update = $this->UyeModel->UyeDuzenle($dataset);

		if($update == 0){
			$hata = helper::alert("Bir sorun oluştu üye güncellenemedi!","hata");
		}else{
			$hata = helper::alert("Üye başarıyla güncellendi!","basari");
		}

		$data = [
			'title' => 'Üye Düzenle',
			'hata' => isset($hata) ? $hata : null
		];

		$this->RenderLayout("Admin","admin/UyeDuzenle",$data);
	}
}

Model Dosyası

<?php

public function UyeDuzenle($data)
{
	extract($data);

	$setdata = [
		"uye_id" => $uye_id,
		"uye_kullanici_adi" => $kadi,
		"uye_eposta" => $eposta,
		"uye_adi" => $ad,
		"uye_soyadi" => $soyad,
		"uye_durum" => $durum
	];

	// Üye Bilgileri
	$this->db->query("UPDATE uyeler SET uye_kullanici_adi = :uye_kullanici_adi, uye_eposta = :uye_eposta, uye_adi = :uye_adi, uye_soyadi = :uye_soyadi, uye_durum = :uye_durum WHERE uye_id = :uye_id",$setdata);
	$this->db->execute();

	// Üye Sınıf Bilgileri
	$this->db->query("SELECT * FROM uye_sinif_map WHERE uye_id = :uye_id and sinif_id = :sinif_id",["uye_id" => $uye_id, "sinif_id" => $sinif]);
	$this->db->execute();

	// Üye Kategori Bilgileri
	foreach ($kategori as $kat) {
		$exp = explode('|', $kat);

		//$this->db->query("SELECT * FROM uye_kategori_map WHERE uye_id = :uye_id AND kategori_id = :kategori_id", ["uye_id" => $uye_id, "kategori_id" => $kategori]);
		$this->db->query("SELECT * FROM uye_kategori_map WHERE uye_id = :uye_id AND kategori_id = :kategori_id", ["uye_id" => $uye_id, "kategori_id" => $exp[0]]);
		$this->db->execute();
	}

	// Hiç Sınıf checkbox seçilmemiş ise önceden kayıtlı olan varsa üye sınıflarını sil
	$this->db->query("DELETE FROM uye_sinif_map WHERE uye_id = :uye_id",["uye_id" => $uye_id]);
	$this->db->execute();

	// Hiç Kategori checkbox seçilmemiş ise önceden kayıtlı olan varsa üye kategorilerini sil
	$this->db->query("DELETE FROM uye_kategori_map WHERE uye_id = :uye_id",["uye_id" => $uye_id]);
	$this->db->execute();

	// Seçilmiş Sınıf checkbox varsa üyeye ait sınıf insert et
	if(!empty($sinif)) {
		foreach ($sinif as $s) {
			$this->db->query("INSERT INTO uye_sinif_map SET uye_id = :uye_id, sinif_id = :sinif_id",["uye_id" => $uye_id, "sinif_id" => $s]);
			$this->db->execute();
		}
	}else{
		// Seçilmiş sınıf yoksa üyeye ait kategorileri sil. Çünkü kategoriler sınıflarla bağımlı.
		$this->db->query("DELETE FROM uye_kategori_map WHERE uye_id = :uye_id",["uye_id" => $uye_id]);
		$this->db->execute();
	}

	// Seçilmiş Sınıf ve kategori checkbox varsa üyeye ait kategori insert et
	if(!empty($sinif) && !empty($kategori)) {
		foreach ($kategori as $k) {
			$exp = explode('|', $k);

			$this->db->query("INSERT INTO uye_kategori_map SET uye_id = :uye_id, kategori_id = :kategori_id, sinif_id = :sinif_id",["uye_id" => $uye_id, "kategori_id" => $exp[0], "sinif_id" => $exp[1]]);
			$this->db->execute();
		}
	}

	return true;
}

Html

<?php if(!$_POST): ?>

	<?php
		$UyeSinif ='';
		foreach($uyesiniflar as $uyesinif){
			$UyeSinif.= $uyesinif["sinif_id"].",";
		}
		$UyeSinif = rtrim($UyeSinif,",");
		$UyeSinif = explode(",",$UyeSinif);

		$UyeKategori ='';
		foreach($uyekategoriler as $uyekategori){
			$UyeKategori.= $uyekategori["kategori_id"].",";
		}
		$UyeKategori = rtrim($UyeKategori,",");
		$UyeKategori = explode(",",$UyeKategori);
	?>

	<form role="form" method="post" action="">
		<div class="row">
			<div class="col-lg-6">
				<div class="form-group" id="sinif">
					<label>Sınıflar</label>
					<button type="button" id="s-temizle">Seçimi Temizle</button>
					<button type="button" id="s-sec">Hepsini Seç</button>

					<?php foreach ($siniflar as $sinif): ?>
						<div class="checkbox class">
							<label>
								<input type="checkbox" name="sinif[]" value="<?php echo $sinif["sinif_id"]; ?>" <?php echo in_array($sinif["sinif_id"],$UyeSinif) ? "checked" : ""; ?> ><?php echo $sinif["sinif_adi"]; ?>
							</label>
						</div>
					<?php endforeach; ?>

				</div>
			</div>
			<div class="col-lg-6">
				<div class="form-group" id="kategori">
					<label>Kategoriler</label>
					<button type="button" id="k-temizle">Seçimi Temizle</button>
					<button type="button" id="k-sec">Hepsini Seç</button>

					<?php foreach ($kategoriler as $kategori): ?>
						<div class="checkbox category">
							<label>
								<input type="checkbox" name="kategori[]" value="<?php echo $kategori["kategori_id"]."|".$kategori["sinif_id"]; ?>" <?php echo in_array($kategori["kategori_id"],$UyeKategori) ? "checked" : ""; ?> ><?php echo $kategori["kategori_adi"]; ?>
							</label>
						</div>
					<?php endforeach; ?>

				</div>
			</div>
		</div>

		<button type="submit" class="btn btn-default">Güncelle</button>
	</form>

<?php endif; ?>
Cevap yaz
Cevaplar (0)
Henüz kimse cevap yazmadı. İlk cevap yazan sen ol!