【问题标题】:Undefined index when inserting into database插入数据库时​​未定义的索引
【发布时间】:2014-04-01 12:37:16
【问题描述】:

我有一个功能,人们可以将两个文本字段插入数据库。它现在将空字段插入数据库中给我这些错误:

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

注意:未定义变量:第 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;
    }

任何帮助表示赞赏。

【问题讨论】:

  • 将数据库字段和表名放在反引号或单引号中
  • 为什么$_POST [之间有空格?不确定这是否会导致错误。您可以发送var_dump($_POST); 来检查您的值是否正确发送。

标签: php mysql


【解决方案1】:

将 try catch 放在 if 条件中。如果请求是 get 你会有错误。

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

【讨论】:

  • 好吧,未定义的索引现在已被删除,但表单仍将空行发送到数据库。有什么想法吗?
  • 确保$_POST 中确实存在这些字段,正如另一条评论中所建议的那样。使用var_dump
  • 另外一点你的image是一个上传的文件,请改用$_FILES
  • var_dump 不显示任何内容。在我如图所示更改代码后(删除了空格 - 感觉很愚蠢),它没有上传任何内容,甚至没有上传空行。
  • 也许将method=post 更改为method='post'。否则,您可以在 include_once 之后关闭您的 php 标签并放置 html(删除返回)。这部分你没有php。
猜你喜欢
  • 2012-02-18
  • 1970-01-01
  • 2016-07-14
  • 2020-06-18
  • 2016-04-05
  • 2012-12-08
  • 2014-01-01
  • 2019-10-07
  • 1970-01-01
相关资源
最近更新 更多