这个PDO脚本有什么问题

时间:2015-03-21 07:55:00

标签: php mysql forms pdo

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

error

这可能是造成这种错误的原因。我如何解决这个问题。

$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 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>

2 个答案:

答案 0 :(得分:3)

你的问题只是一个错字。替换

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

通过

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

答案 1 :(得分:0)

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);
    }

在settings.php文件中,定义数据库连接的常量 然后创建一个新功能。

 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();
    }
    }
    }
    }

我确信,只要您的网络服务器启动并运行,这将适用于您的连接,并且它将简化您稍后需要使用的任何其他连接。至于你的实际表单,只需使用案例过程来确定运行的函数。