插入数据库时​​未定义的索引

时间:2014-04-01 12:37:16

标签: php mysql

我有一个人们可以将两个文本字段插入数据库的功能。它会立即将空字段插入到数据库中,以便立即给出这些错误:

注意:第17行的/Applications/XAMPP/xamppfiles/htdocs/modul8B/controllers/home.php中的未定义变量:catchphrase

注意:未定义的变量:对第17行的/Applications/XAMPP/xamppfiles/htdocs/modul8B/controllers/home.php感兴趣

注意:未定义的变量:第17行的/Applications/XAMPP/xamppfiles/htdocs/modul8B/controllers/home.php中的图像

以下表格:

<?php
include_once 'models/profile_table_class.php';

return"
<div class='main-wrap'>
    <div class='container'>



      <div class='header'>




            <form id='profile-form' method=post action='index.php?page=home' enctype='multipart/form-data'>
                <h1> Your Profile </h1> 
                   <textarea name='catchphrase' id='catchphrase'>

                    </textarea>

                    <textarea name='interest' id='about-you' >

                    </textarea>

                    <h3 class='upload-heading'>Upload Image:</h3>
                    <input type='file' name='image' id='file'>

                    <input id='profile-submit' type='submit' value='profile-submit'/>
             </form>

       </div>
    </div>            
</div>
";

这是插入数据的脚本:

class Profile_Table {
    private $db; 

    public function __construct($pdo) {
        $this->db = $pdo; 
        ;
    }
    public function insertProfile( $catchphrase, $interest, $image){
        $sql = "INSERT INTO profile (catchphrase, interest, image) Values ('".$catchphrase."', '".$interest."', '".$image."')";
        $statement = $this->db->prepare($sql);
        $data = array ($catchphrase, $interest, $image);  
        $statement->execute ($data);
        return $statement;
    }

以下是错误源自的代码:

include_once "models/profile_table_class.php";
    $profile = new Profile_Table($db);


$profileIsSubmitted = isset($_POST['profile-submit']);
if ($profileIsSubmitted) {
    $catchphrase = $_POST ['catchphrase'];
    $interest = $_POST ['interest'];
    $image = $_POST ['image'];

}  try {
        $profile->insertProfile($catchphrase, $interest, $image);
    } catch (Exception $e) {
        $errorDescription = $e;
        echo $e;
    }

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:1)

将try catch置于if条件中。如果请求得到,您将有错误。

if ($profileIsSubmitted) {
    $catchphrase = $_POST ['catchphrase']; // and remove space
    $interest = $_POST ['interest'];// and remove space
    $image = $_POST ['image'];// and remove space

    try {
        $profile->insertProfile($catchphrase, $interest, $image);
    } catch (Exception $e) {
        $errorDescription = $e;
        echo $e;
    }
}