我想使用 POO 将入口(使用表单)添加到 Mysql 表中。这是我所做的:我的班级 :
<?php
class portfolio{
public $title;
public $img_desk;
public $img_tablet;
public $img_phone;
public $id;
public function __construct($title, $img_desk, $img_tablet, $img_phone, $port_id){
$this->title = $title;
$this->img_desk = $img_desk;
$this->img_tablet = $img_tablet;
$this->img_phone = $img_phone;
$this->id = $port_id;
}
public function getPortfolio($id){
$reponse = $bdd->query('SELECT * FROM designer WHERE id = $id');
$portfolio = $reponse->fetch();
$a = new portfolio($portfolio['title'], $portfolio['img_desk'], $portfolio['img_tablet'], $portfolio['phone'], $portfolio['id']);
return $a;
}
public function addPortfolio($title, $img_desk, $img_tablet, $img_phone){
$add = $bdd->exec('INSERT INTO designer(title, img_desk, img_tablet, img_phone) VALUES(:title, :img_desk, :img_tablet, :img_phone)');
$add->execute(array( // _> was used.!!!
'title' => $title,
'img_desk' => $img_desk['name'],
'img_tablet' => $img_tablet['name'],
'img_phone' => $img_phone['name']
// imgs...
));
if (isset($img_desk) AND $img_desk['error'] == 0){
// Testons si le fichier n'est pas trop gros
if ($img_desk['size'] <= 3000000)
{
require("class/img.class.php");
// Testons si l'extension est autorisée
$infosfichier = pathinfo($img_desk['name']);
$extension_upload = $infosfichier['extension'];
$extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png');
if (in_array($extension_upload, $extensions_autorisees))
{
// On peut valider le fichier et le stocker définitivement
move_uploaded_file($img_desk['tmp_name'], 'img/port/' . basename($img_desk['name']));
}
}
}
echo 'Élement ajouté';
}
public function editPortfolio($old, $new){
$edit = $bdd->prepare('UPDATE designer SET title = :title, img_desk = :img_desk, img_tablet = :img_tablet, img_phone = :img_phone WHERE id = $old->id ');
$edit->execute(array(
'title' => $new->title,
'img_desk' => $new->img_desk,
'img_tablet' => $new->img_tablet,
'img_phone' => $new->img_phone
));
}
public function deletePortfolio($cible){
//DELETE FROM designer WHERE id = $cible->id;
}
public function getAllPortfolio(){
//$reponse = $bdd->query('SELECT * FROM designer');
//$donnees = $reponse->fetch();
//return $donnees;
}
}
?>
这是我在包含以下形式的 php 文件上使用的:
<?php
include('class/portfolio.class.php');
if(isset($_POST['title'])){
addPortfolio($_POST['title'], $_FILES['img_desk'], $_FILES['img_tablet'], $_FILES['phone']);
echo 'ok';
}else{
?>
<form method="post" action="admin.php" enctype="multipart/form-data">
<input type="text" placeholder="Titre" name="title"><br>
<input type="file" name="img_desk"><br>
<input type="file" name="img_tablet"><br>
<input type="file" name="img_phone"><br>
<input type="submit" value="Ajouter" name="ok">
</form>
<?php
}
?>
它告诉我:
Fatal error: Call to undefined function addPortfolio() in C:'wamp'www'designers'admin.php on line 47
有人可以解释一下吗?我知道我做错了,但我实际上正在学习OOP。提前谢谢你:)。
您需要
首先创建一个类portfolio
因此,更正后的代码应该是:
你的代码在这里。
$portfolio = new portfolio;
if(isset($_POST['title'])){
$portfolio->addPortfolio($_POST['title'], $_FILES['img_desk'], $_FILES['img_tablet'], $_FILES['phone']);
echo 'ok';
}else{
你的代码在这里
或者
你可以像这样直接使用函数前提是该函数被声明为静态,但由于您有个人数据,因此不建议使用!
portfolio::addPortfolio();