【问题标题】:Using post method in PHP to save data to database [duplicate]在PHP中使用post方法将数据保存到数据库[重复]
【发布时间】:2014-07-05 11:07:19
【问题描述】:

在将文本框保存到我的数据库之前,我正在检查它是否有价值。我正在创建一个电影网站,所以验证工作正常。

问题在于保存,我正在上传电影的图片。图片正在上传到我的网站应用程序的文件夹到我的目录中,这里唯一的问题是我在点击保存时总是有这个错误代码

注意:未定义索引:第 233 行 C:\xampp\htdocs\star_crud\Home.php 中的 photoimg

注意:未定义索引:第 234 行 C:\xampp\htdocs\star_crud\Home.php 中的 photoimg

我的代码如下:

if (isset($_POST['create'])) {
   
    // keep track post values
    $cast = $_POST['cast'];
    $title = $_POST['title'];
    $comment =$_POST['comment'];
    $year = $_POST['year'];
    $tag = $_POST['tags'];
    $IDBM = $_POST['idbm'];
    $cast = htmlspecialchars($cast);
    $title = htmlspecialchars($title);
    $comment = htmlspecialchars($comment);
    
    // validate input
    $valid = true;
    if (empty($cast)) {
        $castError = 'Please enter Cast';
        $valid = false;
    }
     
    if (empty($title)) {
        $titleError = 'Please enter Title';
        $valid = false;
    }
      if (empty($comment)) {
        $commentError = 'Please enter Comment';
        $valid = false;
    }


  if ($valid) {
    
   
        $valid_formats = array("jpg", "png", "gif", "bmp");
 
    
        $name = $_FILES['photoimg']['name'];
        $size = $_FILES['photoimg']['size'];
        
        if(strlen($name))
            {
                list($txt, $ext) = explode(".", $name);
                if(in_array($ext,$valid_formats))
                {
                if($size<(1024*1024))
                    {
                        $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                        $tmp = $_FILES['photoimg']['tmp_name'];
                        if(move_uploaded_file($tmp, $path.$actual_image_name))
                            {
                            
                            echo "hi";
                            }
                        else
                            echo "failed";
                    }
                    else
                    echo "Image file size max 1 MB";                    
                    }
                    else
                    echo "Invalid file format..";   
            }
            
        else
            echo "Please select image..!";
            
        exit;
    
    
    
    }
    }
    else echo "error";

我已经检查了删除if(valid) 语句中的所有语句,并打印了一个字符串,它可以工作,我认为问题出在语句上。

<form class="form-horizontal" id="form1" action="Home.php" method="post">



                  <div class="control-group <?php echo !empty($titleError)?'error':'';?>">
                    <label class="control-label">Title</label>
                    <div class="controls">
                        <input name="title" type="text"  placeholder="Title" value="<?php echo !empty($title)?$title:'';?>">
                        <?php if (!empty($titleError)): ?>
                            <span class="help-inline"><?php echo $titleError;?></span>
                        <?php endif; ?>
                    </div>
                  </div>
                  <div class="control-group <?php echo !empty($emailError)?'error':'';?>">
                    <label class="control-label">Year</label>
                    <div class="controls">
                       <?php 
                        $years = range (2011, 2021);
                        echo '<select name="year">';
                            foreach ($years as $value) {
                            echo "<option value=\"$value\"> $value</option>\n";
                                }
                                echo '</select>';
                       
                       ?>
                    </div>
                  </div>
                    <div class="control-group <?php echo !empty($emailError)?'error':'';?>">
                    <label class="control-label">Category</label>
                    <div class="controls">
                       <?php 
                         require 'db2.php';
                         $q1 = mysqli_query($dbc,"SELECT Name FROM Category ");
                        

                         echo "<select name='Category'>";
                      while ($row = mysqli_fetch_array($q1)) {
                         echo "<option value='" . $row['Name'] . "'>" . $row['Name'] . "</option>";
                            }
                           echo "</select>";
                       ?>
                    </div>
                  </div>
                  <div class="control-group <?php echo !empty($castError)?'error':'';?>">
                    <label class="control-label">Cast</label>
                    <div class="controls">
                        <input name="cast" type="text"  placeholder="Cast" value="<?php echo !empty($cast)?$cast:'';?>">
                        <?php if (!empty($castError)): ?>
                            <span class="help-inline"><?php echo $castError;?></span>
                        <?php endif;?>
                    </div>
                  </div>
                  <div class="ajaxform">
                    <div class="control-group <?php echo !empty($imageError)?'error':'';?>">
                    <label class="control-label">Image Upload</label>
                    <div class="controls">
                      
            


                  <input type="file" name="photoimg"  onchange="readURL(this);" id="photoimg" /><br/>
                          <img id="blah"  src="#"  height="150" width="150"  alt="your image" />
                   


                    </div>
                </div>
                    
                  </div>
                    <div class="control-group <?php echo !empty($TagsError)?'error':'';?>">
                    <label class="control-label">Tags</label>
                    <div class="controls">
                        <input name="tags" id="mySingleField" type="hidden"  > <!-- only disabled for demonstration purposes -->
                         <ul id="singleFieldTags"></ul><?php if (!empty($TagsError)): ?>
                            <span class="help-inline"><?php echo $TagsError;?></span>
                        <?php endif; ?>
                    </div>
                  </div>
                    <div class="control-group <?php echo !empty($IDMBError)?'error':'';?>">
                    <label class="control-label">IDBM</label>
                    <div class="controls">
                     
                        <input name="idbm"  type="textarea"><?php if (!empty($IDMBError)): ?>
                            <span class="help-inline"><?php echo $IDMBError;?></span>
                        <?php endif;?>
                        
                    </div>
                  </div>
                    <div class="control-group <?php echo !empty($CommentError)?'error':'';?>">
                    <label class="control-label">Comment</label>
                    <textarea name="comment" id="comment" rows="4" style="width:780px" cols="50">
                    </textarea>
                    <?php if (!empty($commentError)): ?>
                            <span class="help-inline"><?php echo $commentError;?></span>
                        <?php endif;?>
                    <div class="controls">
                     
                        
                    </div>
                  </div>
                  <div class="form-actions">
                      <button type="submit" name="create" class="btn btn-success">Create</button>
                      <a class="btn" href="index.php">Home</a>
                    </div>
                </form>

【问题讨论】:

  • 请出示您的表格。它使用enctype="multipart/form-data" 吗?这是文件上传所必需的。
  • @Barmar 我编辑问题并填写表格

标签: php


【解决方案1】:

photoimg 应该是您输入字段的名称:

<input type='file' name='photoimg' />

由于可能不是,$_FILES 数组的偏移量不起作用,PHP 无法访问任何子键(tmp_name 等)。

【讨论】:

  • 是一样的
    外观
【解决方案2】:

将表格更改为:

<form class="form-horizontal" id="form1" action="Home.php" method="post" enctype="multipart/form-data">

您必须使用enctype="multipart/form-data" 启用文件上传。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 2016-06-07
    • 2019-05-05
    • 2016-05-11
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多