php - 我无法在数据库中添加行

标签 php mysql mysqli

我正在尝试为我的作品集网站构建一个 CMS。我在后端创建了所有表格。一切正常,除了我无法在 Mysqli 的投资组合数据库中添加列。我尝试在另一个 PHP 页面中回显表单数据。一切正常。甚至上传的图像也会移动到站点目录。唯一的问题是在数据库表中插入数据。这是我的代码

<?php
session_start();
?>

<?php


if(isset($_POST['submit']))
{


    $title=$_POST['title'];
    $description=$_POST['description'];
    $duration=$_POST['duration'];
    $category=$_POST['category'];

    $nImage=$_FILES['nImage']['name'];
    $file_tmp=$_FILES['nImage']['tmp_name'];
    $filesize=$_FILES['nImage']['size'];
    $file_ext=strtolower(pathinfo($nImage,PATHINFO_EXTENSION));
    $supported=array('jpg','jpeg','png');

    //Rename File
    $rand=sha1(time());
    $newname=$rand.".".$file_ext;
    $portfolio_img_dir="images/portfolio/".$newname;

    //Check If FileSize is more than 2 MB
    if($filesize<2097152)
    {

        //Check if Extension Matches
        if(in_array($file_ext,$supported))
        {
            //Move Uploaded Image
            if(move_uploaded_file($file_tmp,$portfolio_img_dir))
            {
                $qry=mysqli_query($link,"INSERT INTO `p_o_r_t_f_o_l_i_o`(`title`,`description`,`duration`,`category`,`image`) values('$title','$description','$duration','$category','$portfolio_img_dir')");

                    if($qry)
                    {

                        $_SESSION['add_port_msg_success']="Portfolio Item Successfully Added";
                        header('location:add_portfolio.php');

                    }

                    else
                    {
                        $_SESSION['add_portfolio_msg']="Portfolio Item Failed to Add";
                        header('location:add_portfolio.php');
                    }

            }
            else
            {
                $_SESSION['add_port_msg']="File Failed to Move";
                header('location:add_portfolio.php');
            }
        }

        else
        {
            $_SESSION['add_port_msg']="File Extension not Supported";
            header('location:add_portfolio.php');
        }

    }

    else
    {
        $_SESSION['add_port_msg']="Filesize too Large";
        header('location:add_portfolio.php');
    }

}

else
{
    $_SESSION['add_port_msg']="Invalid Attempt to Access Page";
    header('location:add_portfolio.php');
}
?>

这是我的 HTML 表单

<!--Add New Portfolio Item Form-->
                <div class='col-md-8 col-offset-4'>
                    <form class="form-horizontal" method="post" enctype="multipart/form-data" action="act_newp.php">
                    <fieldset>

                    <!-- Form Name -->
                    <legend>Add New Portfolio Item</legend>

                    <!-- Text input-->
                    <div class="form-group">
                      <label class="col-md-4 control-label" for="title">Item Title</label>  
                      <div class="col-md-5">
                      <input name="title" type="text" placeholder="Enter Title For the Item" class="form-control input-md" required="">

                      </div>
                    </div>

                    <!-- Textarea -->
                    <div class="form-group">
                      <label class="col-md-4 control-label" for="description">Description</label>
                      <div class="col-md-4">                     
                        <textarea class="form-control" name="description">Enter Description of Portfolio Item</textarea>
                      </div>
                    </div>

                    <!-- Multiple Radios -->
                    <div class="form-group">
                         <label class="col-md-4 control-label" for="category">Select Category</label>
                         <div class="col-md-4">

                          <?php
                                $sel="SELECT cat_id,cat_name from categories order by  cat_name asc";
                                $query=mysqli_query($link,$sel);
                                while($row=mysqli_fetch_array($query))
                                {

                                    echo "<div class='radio'>";
                                    echo "<label for='category-".$row['cat_id']."'>";
                                    echo "<input type='radio' name='category' value='".$row['cat_id']."'>";
                                    echo $row['cat_name'];
                                    echo "</label>";
                                    echo "</div>";

                                }                   
                            ?>
                        </div>
                    </div>

                    <!-- Text input-->
                    <div class="form-group">
                      <label class="col-md-4 control-label" for="duration">Duration of Project</label>  
                      <div class="col-md-4">
                      <input name="duration" type="text" placeholder="Enter Duration in Hrs" class="form-control input-md" required="">

                      </div>
                    </div>

                    <!-- File Button --> 
                    <div class="form-group">
                      <label class="col-md-4 control-label" for="nImage">File Button</label>
                      <div class="col-md-4">
                        <input name="nImage" class="input-file" type="file">
                      </div>
                    </div>

                    <!-- Button -->
                    <div class="form-group">
                      <label class="col-md-4 control-label" for="submit"></label>
                      <div class="col-md-4">
                        <button id="submit" name="submit" class="btn btn-success">Add New Portfolio Item</button>
                      </div>
                    </div>

                    </fieldset>
                    </form>
                                    </div>
                <!--/Add New Portfolio Item Form Ends-->

这是我的数据库表图像:

The first two are added by me from the Insert tab in Database menu 任何帮助将不胜感激。预先感谢:-)

最佳答案

要从 HTML form 提交数据,您正在使用 button,它显然不会发送 form 数据,您必须使用 input type="submit",所以修改这行代码

<button id="submit" name="submit" class="btn btn-success">Add New Portfolio Item</button>

<input type="submit" id="submit" name="submit" class="btn btn-success" value="Add New Portfolio Item" />

关于php - 我无法在数据库中添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27074241/

相关文章:

PHP登录数据库连接错误

php - 为什么这个简单的 "INSERT INTO"查询不起作用

php - 状态不发回到表单上?

php - 使用 PHP 在 MySQL 中 JOIN 后重复列名

php - 如何将动态添加的文本框(JavaScript)中的多个数据保存到MySQL数据库中?

PHP 提交后不在下拉列表中显示更新的值

mysql - 重新排序/重置自动增量主键

php - 将 mysqli 转换为 PDO 时出现问题 | PHP 数据库

php - Memcached - 为什么不存储数据?

php - WordPress子主题未格式化