这个PDO脚本有什么问题

问题描述:

Here i am writing a script which will send three different values to a database using a simple html form.But whenever i submit the form no values are inserted and i get the following error :

error

what might be the reason for this error.How can i fix this problem.

$server='localhost';
$user='root';
$password='';
$dbname='fruits';
if(isset($_POST['name']) &&isset($_POST['colour']) && isset($_POST['calories'])){
   if(!empty($_POST['name']) && !empty($_POST['colour']) && !empty($_POST['calories'])){
$name=$_POST['name'];
$colour=$_POST['colour'];
$calories=$_POST['calories'];
try{
$conn=new PDO('mysql:host=$server,dbname=$dbname,$user,$password');
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$stmt=$conn->prepare("INSERT INTO favfruit(name,colour,calories) 
                      VALUES(:name,:colour,:calories)");

$stmt->execute(array(':name'=>$name,':colour'=>$colour,':calories'=>$calories));
echo 'it was successfully entered to database';
}catch(PDOException $e){
    echo 'error : '.$e->getMessage();
}
}
}

HTML form :

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
    fruit name :<input type='text' placeholder='fruit name' name='name'></br>
    colur      :<input type='text' placeholder='fruit colour' name='colour'></br>
    calories   :<input type='text' placeholder='calories' name='calories'></br>
    <input type='submit' value='SUBMIT FORM'>
</form>

这里我正在编写一个脚本,它将使用一个简单的html格式向数据库发送三个不同的值。但每当我 提交表单没有插入任何值,我收到以下错误: p>

p>

出现此错误的原因可能是什么。如何解决此问题。 p>

  $ server ='localhost';  
 $ user ='root'; 
 $ password =''; 
 $ dbname ='fruits'; 
if(isset($ _ POST ['name'])&amp;&amp; isset($ _ POST ['color  '])&amp;&amp; isset($ _ POST ['calories'])){
 if(!empty($ _ POST ['name'])&amp;&amp;!empty($ _ POST ['color'])&amp;  ;&amp;!empty($ _ POST ['calories'])){
 $ name = $ _ POST ['name']; 
 $ color = $ _ POST ['color']; 
 $ calories = $ _ POST [  'calories']; 
try {
 $ conn = new PDO('mysql:host = $ server,dbname = $ dbname,$ user,$ password'); 
 $ conn-&gt; setAttribute(PDO :: ATTR_ERRMODE  ,PDO :: ERRMODE_EXCEPTION); 
 $ stmt = $ conn-&gt; prepare(“INSERT INTO favfruit(名称,颜色,卡路里)
 V  ALUES(:姓名,:颜色,:卡路里)“); 
 
 $的stmt-&GT;执行(阵列( ':名称'=&GT; $名称, ':颜色'=&GT; $颜色,':卡路里 '=&gt; $ calories)); 
echo'它已成功输入数据库'; 
} catch(PDOException $ e){
 echo'error:'。$ e-&gt; getMessage(); 
}  
} 
} 
  code>  pre> 
 
 

HTML表单: p>

 &lt; form action ='&lt;?php  echo $ _SERVER ['PHP_SELF'];  ?&GT;”  method ='post'&gt; 
水果名称:&lt; input type ='text'placeholder ='fruit name'name ='name'&gt;&lt; / br&gt; 
 colur:&lt; input type ='text' 占位符='水果颜色'名称='颜色'&gt;&lt; / br&gt; 
卡路里:&lt;输入类型='文字'占位符='卡路里'名称='卡路里'&gt;&lt; / br&gt; 
&lt;  ; input type ='submit'value ='SUBMIT FORM'&gt; 
&lt; / form&gt; 
  code>  pre> 
  div>

Your problem is just a typo. Replace

$conn=new PDO('mysql:host=$server,dbname=$dbname,$user,$password');

by

$conn=new PDO("mysql:host=$server;dbname=$dbname",$user,$password);

require_once($_SERVER['DOCUMENT_ROOT'].'/settings.php');
class db extends pdo{
    //variables
    public $db = '';
    public $config;
    public $settings = array(
        'host'      => SERVER,
        'database'  => DB,
        'username'  => USER,
        'password'  => PASS,
    );

    public function __construct(){

        $this->db = new PDO(
            "mysql:host={$this->settings['host']};" .
            "dbname={$this->settings['database']};" .
            "charset=utf8",
            "{$this->settings['username']}",
            "{$this->settings['password']}"
        );

        $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    }

Within the settings.php file define the constants for your db connection then create a new function.

 public function InsertFruit() {
 if(isset($_POST['name']) &&isset($_POST['colour']) && isset($_POST['calories'])){
       if(!empty($_POST['name']) && !empty($_POST['colour']) && !empty($_POST['calories'])){
    $name=$_POST['name'];
    $colour=$_POST['colour'];
    $calories=$_POST['calories'];
    try{
    $query = <<<SQL
    INSERT INTO favfruit(name,colour,calories)
    VALUES(:name,:colour,:calories)
SQL;
    $resource = $this->db->prepare($query);
    $resource->execute(array(
    'name'  => $_POST['name'],
    'colour' => $_POST['colour'],
    'calories' => $_POST['calories'],
));
    echo $_POST['name'].' entered into database';
    $stmt=$conn->prepare("INSERT INTO favfruit(name,colour,calories) 
                          VALUES(:name,:colour,:calories)");

    }catch(PDOException $e){
        echo 'error : '.$e->getMessage();
    }
    }
    }
    }

I know for sure as long as your webserver is up and running that this will work for your connection and that it will ease any other connections you need to use later on. As for your actual form just use a case process to determine what function is ran.