簡體   English   中英

PDO無法將廣告類型的對象用作數組(格式錯誤)

[英]PDO Cannot use object of type ads as array (form error)

我是PDO的新手,正在嘗試制作廣告網站。 在包含來自表單的數據的頁面上,我收到以下問題:“致命錯誤。無法將ads類型的對象用作數組”。 在這里問之前,我搜索了這個問題,但沒有找到任何東西(也許是因為我是新手?)。 請不要對我投反對票。

這是具有以下功能的PHP文件:

class ads{
private $db;

public function __construct($database) {
    $this->db = $database;
}

 public function insert_ads($categorie, $subcategorie, $userid, $site, $nume, $oras, $judet, $telefon, $email, $titlu_anunt, $text_anunt, $pret_anunt ){

$time           = time();
$categorie      = 'masini';
$subcategorie   = 'dacia';
$email_code     = $email_code = uniqid('code_',true); 
$query          = $this->db->prepare("INSERT INTO `ads` (`categorie`, `subcategorie`, `userid`, `site`, `nume`, `oras`, `judet`, `telefon`, `email`, `email_code`, `titlu_anunt`, `text_anunt`, `pret_anunt`, `data_adaugare`, `vizualizari`, `confirmed`, `platit`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");

$query->bindValue(1, $categorie);
$query->bindValue(2, $subcategorie);
$query->bindValue(3, 0);
$query->bindValue(4, $site);
$query->bindValue(5, $nume);
$query->bindValue(6, $oras);
$query->bindValue(7, $judet);
$query->bindValue(8, $telefon);
$query->bindValue(9, $email);
$query->bindValue(10, $email_code);
$query->bindValue(11, $titlu_anunt);
$query->bindValue(12, $text_anunt);
$query->bindValue(13, $pret_anunt);
$query->bindValue(14, $time);
$query->bindValue(15, 1);
$query->bindValue(16, 0);
$query->bindValue(17, 0);

try{
$id_last = ("SELECT LAST_INSERT_ID()");
$result = $this->db->prepare($id_last);
$result->execute();
$last_id = $result->fetchColumn();

#code to deal with the picture uploads 
    #target folder 
$target = 'image_uploads/';
  if(isset($_FILES['image_name'])===true){
  $files = $_FILES['image_name'];
  for($x = 0 ; $x < count($files['name']); $x++){
  $name = $files['name'][$x] ;
  $temp_name = $files['tmp_name'][$x];
  #extention filter it takes only the extension  want 
$allowed ='gif,png,jpg';
$extension_allowed= explode(',',$allowed );  
$file_extention = pathinfo($name, PATHINFO_EXTENSION);
if(array_search($file_extention,$extension_allowed)){
}else {
echo 'We only allow gif, png ,jpg';
exit();
} #extention  filter ends here 
  #check the size of the image 
 $file_size = $files['size'][$x];
 if($file_size > 2097152){
 echo 'The file should be lesS than 2MB';
 exit();
 }
#check the size of the image  ends here
#Rename images 
$sub = substr(md5(rand()),0,7);
#the above generates char and numbesr 
$rand = rand(0,100000);
$rename = $rand.$sub.$name;
#Rename images ends here
$move = move_uploaded_file($temp_name,$target.$rename); 
#code to deal with the picture uploads ends here 
$images ="INSERT INTO ads_images(ads_id,image_name)
 VALUES($last_id,:image_name)";
$images_insert = $this->db->prepare($images);
$images_insert->execute(array(
':image_name'=>$rename,
));
}}  
$query->execute();
mail($email, 'Please activate your account', "Hello " . $nume. ",\r\nThank you for registering with us. Please visit the link below so we can activate your account:\r\n\r\nhttp://www.djmixtv.com/test/activate_ads.php?email=" . $email . "&email_code=" . $email_code . "\r\n\r\n-- Example team");
}catch(PDOException $e){
    die($e->getMessage());
}   
}

這是PHP代碼:

if (isset($_GET['success']) && empty($_GET['success'])) {
        echo '<h3>Your details have been updated!</h3>';            
    } else{
         if(empty($_POST) === false) {      
if (isset($_POST['nume']) && !empty ($_POST['nume'])){ // We only allow names with alphabets
                if (ctype_alpha($_POST['nume']) === false) {
                $errors[] = 'Please enter your Name only with letters!';
                }   
            }
            if (isset($_POST['oras']) && !empty ($_POST['oras'])){
                if (ctype_alpha($_POST['oras']) === false) {
                $errors[] = 'Please enter your city with only letters!';
                }   
            }
            if (isset($_POST['judet']) && !empty ($_POST['judet'])){
                if (ctype_alpha($_POST['judet']) === false) {
                $errors[] = 'Please enter your contry with only letters!';
                }   
            }
            if (isset($_POST['telefon']) && !empty ($_POST['telefon'])){
                if (ctype_digit($_POST['telefon']) === false) {
                $errors[] = 'Please enter your phone with only numbers!';
                }   
            }
            if (isset($_POST['telefon']) && !empty ($_POST['telefon'])){
                if (ctype_digit($_POST['telefon']) === false) {
                $errors[] = 'Please enter your phone with only numbers!';
                }   
            }
            if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
                $errors[] = 'Please enter a valid email address';
            }
            if (isset($_POST['titlu_anunt']) && !empty ($_POST['titlu_anunt'])){
                if (ctype_alnum($_POST['titlu_anunt']) === false) {
                $errors[] = 'Please enter your phone with only numbers!';
                }   
            } 


                $site               = htmlentities(trim($_POST['site']));
                $nume               = htmlentities(trim($_POST['nume']));
                $oras               = htmlentities(trim($_POST['oras']));
                $judet              = htmlentities(trim($_POST['judet']));
                $telefon            = htmlentities(trim($_POST['telefon']));
                $email              = htmlentities(trim($_POST['email']));
                $titlu_anunt        = htmlentities(trim($_POST['titlu_anunt']));
                $text_anunt         = htmlentities(trim($_POST['text_anunt']));
                $pret_anunt         = htmlentities(trim($_POST['pret_anunt']));


                $ads->insert_ads($categorie, $subcategorie, $site, $nume, $oras, $judet, $telefon, $email, $titlu_anunt, $text_anunt, $pret_anunt);
                header('Location: settings.php?success');
                exit();
            }

形式如下:

<form action="" method="post" enctype="multipart/form-data">

            <div id="personal_info">
                <h3 >Change Profile Information </h3>
                <ul>


                    <li>
                        <h4>Site:</h4>
                        <input type="text" name="site" value="<?php if (isset($_POST['site']) ){echo htmlentities(strip_tags($_POST['site']));} else { echo $ads['site']; }?>">
                    </li>     
                    <li>
                        <h4>Nume: </h4>
                        <input type="text" name="nume" value="<?php if (isset($_POST['nume']) ){echo htmlentities(strip_tags($_POST['nume']));} else { echo $ads['nume']; }?>">
                    </li>
                    <li>
                        <h4>Nume: </h4>
                        <input type="text" name="nume" value="<?php if (isset($_POST['nume']) ){echo htmlentities(strip_tags($_POST['nume']));} else { echo $ads['nume']; }?>">
                    </li>
                  <li>
                        <h4>Oras: </h4>
                        <input type="text" name="oras" value="<?php if (isset($_POST['oras']) ){echo htmlentities(strip_tags($_POST['oras']));} else { echo $ads['oras']; }?>">
                    </li>
                    <li>
                        <h4>Judet: </h4>
                        <input type="text" name="judet" value="<?php if (isset($_POST['judet']) ){echo htmlentities(strip_tags($_POST['judet']));} else { echo $ads['judet']; }?>">
                    </li>
                    <li>
                        <h4>Telefon: </h4>
                        <input type="text" name="telefon" value="<?php if (isset($_POST['telefon']) ){echo htmlentities(strip_tags($_POST['telefon']));} else { echo $ads['telefon']; }?>">
                    </li>
                    <li>
                        <h4>Email: </h4>
                        <input type="text" name="email" value="<?php if (isset($_POST['email']) ){echo htmlentities(strip_tags($_POST['email']));} else { echo $ads['email']; }?>">
                    </li>
                    <li>
                        <h4>Titlu Anunt: </h4>
                        <input type="text" name="titlu_anunt" value="<?php if (isset($_POST['titlu_anunt']) ){echo htmlentities(strip_tags($_POST['titlu_anunt']));} else { echo $ads['titlu_anunt']; }?>">
                    </li>
                    <li>
                        <h4>Text anunt:</h4>
                        <textarea name="text_anunt"><?php if (isset($_POST['text_anunt']) ){echo htmlentities(strip_tags($_POST['text_anunt']));} else { echo $user['text_anunt']; }?></textarea>
                    </li>
                    <li>
                        <h4>Pret: </h4>
                        <input type="text" name="pret_anunt" value="<?php if (isset($_POST['pret_anunt']) ){echo htmlentities(strip_tags($_POST['pret_anunt']));} else { echo $ads['pret_anunt']; }?>">
                    </li>
                    <li>
                    <h4>Image</h4>
                     <input type="file" name="image_name[]" multiple /><br />
                </ul>    
            </div>
            <div class="clear"></div>
            <hr />
                <span>Update Changes:</span>
                <input type="submit" value="Update">

        </form>

這與PDO不相關。

您不能將ads實例用作數組。 例如

$ads = new ads();
$ads['something'] = 1; //Fatal Error comes here

它隨處可見。

<input type="text" name="site" value="<?php if (isset($_POST['site']) ){
    echo htmlentities(strip_tags($_POST['site']));} else { echo $ads['site']; }
?>">

在PHP中,如果要使用屬性,則必須對其進行定義。

class ads {
    public myProperty;

    public function setAValueForMyProperty(){
        $this->myProperty = 1;
    }
}

$ads = new ads();
$ads->setAValueForMyProperty();
echo $ads->myProperty;

如果要將類實例用作數組,則有兩個選擇。 您可以將其內容導出到數組中:

class ads {
    protected myProperty = 1;

    public function toArray(){
        return array(
            'myProperty' => $this->myProperty;
        );
    }
}

$ads = new ads();
$arr = $ads->toArray();
echo $arr['myProperty'];

或者,您可以實現ArrayAccess接口:

class ads implements ArrayAccess {
    protected myProperty = 1;

    public function offsetSet($offset, $value) {
        $this->$offset = $value;
    }

    public function offsetExists($offset) {
        return isset($this->$offset);
    }

    public function offsetUnset($offset) {
        unset($this->$offset);
    }

    public function offsetGet($offset) {
        return $this->$offset;
    }
}

$ads = new ads();
echo $ads['myProperty'];

我認為您應該閱讀更多有關php中類如何工作的信息。

http://www.php.net/manual/en/language.oop5.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM