我无法在我的数据库中添加行

时间:2014-11-22 05:01:51

标签: 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 任何帮助将受到高度赞赏。在此先感谢: - )

3 个答案:

答案 0 :(得分:0)

要从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" />

答案 1 :(得分:0)

你可能应该使用更少的嵌套ifs,特别是当它们都执行&#39; else&#39;的相同结果时。

if ( ( $a=3 )
  && ( $b=7 )
  && ($car >= 9)
  && ( $name == 'steve' )
  && ( $more == 'stuff' ) ) {
  // Run stuff here
) else {
  exit('error');
}

mysqli_query();插入后,使用mysqli_error($link);运行错误检查它会告诉您任何问题。 mysqli_query不会返回任何错误,只有&#39; true&#39;或者&#39; false&#39;取决于它的运行方式。

答案 2 :(得分:0)

我的问题解决了。我在查询中使用了连接。以下是我的工作代码的样子(只是查询):

$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."')");

非常感谢所有试图提供帮助的人。干杯!!

相关问题