这个问题,当我执行表单时,它不向数据库发送任何内容,如果我直接插入:4product = new produit(0)它工作,所以我不能通过表单将变量发送到实例,如果它是可行的。 / p>
<div class="bs-example">
<h1>Ajouter un produit</h1>
<form action="../produit_insert_form.php" method="POST" class="form-horizontal" >
<div class="form-group">
<label class="control-label col-xs-3" for="code_p">ID de produit:</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="code_p" placeholder="ID">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="design">Designation:</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="design" placeholder="Designation">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="type">TYPE:</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="type" placeholder="TYPE">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="qte">QTE:</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="qte" placeholder="QTE">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="prix">PRIX:</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="prix" placeholder="PRIX">
</div>
</div>
<br>
<div class="form-group">
<div class="col-xs-offset-3 col-xs-9">
<input type="submit" class="btn btn-primary" value="Ajouter">
<input type="reset" class="btn btn-default" value="Reset">
</div>
</div>
</form>
</div>
&#13;
require_once'produit.php';
$code_p = isset($_POST['code_p']);
$design =isset($_POST['design']);
$type = isset($_POST['type']);
$qte = isset($_POST['qte']);
$prix = isset($_POST['prix']);
$product = new produit($code_p) ; //create instance (Object) - classe Produit - with __construct()
$product->setAttributs($design,$type,$qte,$prix);
$product->write();
&#13;
class Produit {
private $code_p = '';
private $design;
private $type;
private $qte;
private $prix;
public function __construct($id){
require_once 'connectdb.php';
$this->code_p = $id;
if ($id > 0){
$query = "select * from produits where code_p = '$this->code_p'";
$result = cmySQL::executQuery($query);
while($Row = mysqli_fetch_assoc($result))
{
$this->design = $Row['design'];
$this->type = $Row['type'];
$this->qte = $Row['qte'];
$this->prix = $Row['prix'];
}
}
}
public function setAttributs($design, $type, $qte, $prix){
$this->design = $design;
$this->type = $type;
$this->qte = $qte;
$this->prix = $prix;
}
public function setCode_P($code_p){
$this->code_p = $code_p;
}
public function getCode_P(){
return $this->code_p;
}
public function setDesign($design){
$this->design = $design;
}
public function getDesign(){
return $this->design;
}
public function setType($type){
$this->type=$type;
}
public function getType(){
return $this->type;
}
public function setQte($qte){
$this->qte=$qte;
}
public function getQte(){
return $this->qte;
}
public function setPrix($prix){
$this->prix=$prix;
}
public function getPrix(){
return $this->prix;
}
// function pour insertion des donnees dans la table produits //
public function write()
{
require_once 'connectdb.php';
if ($this->code_p == 0){
$sqlQuery = "INSERT INTO produits(code_p,design,type,prix,qte) VALUES('$this->code_p','$this->design','$this->type','$this->prix','$this->qte')";
}
else{
$sqlQuery = "UPDATE produits SET `design` = '$this->design', `type` = '$this->type', `qte` = '$this->qte', `prix` = '$this->prix' WHERE code_p='$this->code_p'";
}
cmySQL::executQuery($sqlQuery);
}
public function delete(){
require_once 'connectdb.php';
$sqlQuery = "delete from produits where code_p='$this->code_p'";
//die ($sqlQuery);
cmySQL::executQuery($sqlQuery);
}
&#13;
答案 0 :(得分:2)
主要问题是您使用isset()
的结果分配变量,这是一个布尔值(true / false),而不是POST变量的值。它应该是这样的:
$code_p = $_POST['code_p'];
$design = $_POST['design'];
$type = $_POST['type'];
$qte = $_POST['qte'];
$prix = $_POST['prix'];
除了在收到数据时验证您的数据,您还应该在类write()
方法中执行某些操作,以防止注入攻击,例如mysqli_escape_string()
(http://php.net/manual/en/function.mysqli-escape-string.php),或者最好,准备好的陈述(http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)。你现在拥有的东西目前非常不安全,容易受到注射攻击。
答案 1 :(得分:0)
用此
替换您的表单<div class="bs-example">
<h1>Ajouter un produit</h1>
<form action="../produit_insert_form.php" method="POST" class="form-horizontal" >
<div class="form-group">
<label class="control-label col-xs-3" for="code_p">ID de produit:</label>
<div class="col-xs-9">
<input type="text" name="code_p" class="form-control" id="code_p" placeholder="ID">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="design">Designation:</label>
<div class="col-xs-9">
<input type="text" name="design" class="form-control" id="design" placeholder="Designation">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="type">TYPE:</label>
<div class="col-xs-9">
<input type="text" name="type" class="form-control" id="type" placeholder="TYPE">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="qte">QTE:</label>
<div class="col-xs-9">
<input type="text" name="qte" class="form-control" id="qte" placeholder="QTE">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3" for="prix">PRIX:</label>
<div class="col-xs-9">
<input type="text" name="prix" class="form-control" id="prix" placeholder="PRIX">
</div>
</div>
<br>
<div class="form-group">
<div class="col-xs-offset-3 col-xs-9">
<input type="submit" class="btn btn-primary" value="Ajouter">
<input type="reset" class="btn btn-default" value="Reset">
</div>
</div>
</form>
</div>
请告诉我是否有任何问题。