将照片插入数据库,然后在另一页上显示

时间:2019-05-08 00:37:32

标签: php

我尝试了很多次,将照片插入数据库中,然后在另一页上显示它,但是我做不到,我搜索了很多资源,但是我做不到。请帮助我这是我的数据库和代码,请有人帮助我在addStudent.php页面中添加图片,然后在viewStudent.php页面上显示。

数据库表

   CREATE TABLE `users` (
      `name` varchar(200) NOT NULL,
      `phoneNO` int(11) NOT NULL,
      `formNO` int(5) NOT NULL,
      `groupType` varchar(2) NOT NULL,
      `LessonTime` varchar(200) NOT NULL,
      `image` longblob NOT NULL
    ) 

addStudent.php

<?php 
include "db.php";
include "head.php"; 
include "session.php";
include "function.php";

if(isset($_POST["submit"])){
    $name = $_POST["name"];
    $phone = $_POST["phone"];
    $form = $_POST["form"];
    $groupType = $_POST['txtGroupType'];
    $lessionTime = $_POST['txtLessonTime'];

    if(empty($name)){
        $_SESSION["errorMessage"]="Name can't be empty";
        Redirect_to("addStudent.php");
    }
    if(empty($phone)){
        $_SESSION["errorMessage"]="Phone can't be empty";
        Redirect_to("addStudent.php");
    }elseif(strlen($phone)>11){
        $_SESSION["errorMessage"]="Roll too long . You can use Only 11 Number";
        Redirect_to("addStudent.php");
    }
    if(empty($form)){
        $_SESSION["errorMessage"]="Form can't be empty";
        Redirect_to("addStudent.php");
    }
    if(empty($groupType)){
        $_SESSION["errorMessage"]="Group type can't be empty";
        Redirect_to("addStudent.php");
    }
    /*if(empty($lessionTime)){
        $_SESSION["errorMessage"]="Lessson time can't be empty";
        Redirect_to("addStudent.php");
    }
    if(empty($fileChooserPhoto)){
        $_SESSION["errorMessage"]="Photo can't be empty";
        Redirect_to("addStudent.php");

    }*/else{

        $Query = "INSERT INTO `users`(`name`, `phoneNO`, `formNO`, `groupType`, `LessonTime`) VALUES ('$name','$phone','$form','$groupType','$lessionTime')";
        $result = mysqli_query($db,$Query);
        if($result){
            $_SESSION["successMessage"] = "Your Information Submited Successfully";
            Redirect_to("addStudent.php");
        }else{
            $_SESSION["errorMessage"]="Your Information Submited Fail";
            Redirect_to("addStudent.php");
        }

}
}
?>
<div class="col-sm-10">
    <h1 class="text-center">User Registration</h1>
    <?php echo errorMsg();echo successMsg();?>
    <div class="container-fluid">
            <div class="row-fluid">
            <form action="" method="post">
                <div class="form-group">    
                <label for="name">Full Name:</label>
                <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name"  pattern="[A-Za-z]{1,32}">
              </div>
              <div class="form-group">
                <label for="phone">Phone:</label>
                <input type="text" class="form-control" id="phone" placeholder="07701234567" name="phone"  placeholder="0770 123 4567" pattern="[0-9]{11}">
              </div>
              <div class="form-group">
                <label for="form">Form Number:</label>
                <input type="text" class="form-control" id="form" placeholder="Enter Form Number"  name="form" pattern="[0-9]{2}">
              </div>
               <div class="form-group">
                <label for="groupType">Group Type:</label>  &nbsp; &nbsp;
                    <select name="txtGroupType">
                    <option value="A">A</option>
                    <option value="B">B</option>
                    <option value="C">C</option>
                    <option value="D">D</option>
                 </select>    
                </div>
               <div class="form-group">
                <label for="lessonTime">Lesson Time:</label>  &nbsp;
                    <select name="txtLessonTime" size="1">
                    <option value="08:00AM  -  10:00AM">08:00 AM   -   10:00 AM</option>
                    <option value="10:00AM  -  12:00PM">10:00 AM   -   12:00 PM</option>
                    <option value="15:00PM  -  17:00PM">15:00 PM   -   17:00 PM</option>
                    <option value="18:00PM  -  20:00PM">18:00 PM   -   20:00 PM</option>
                    </select> 
              </div>
              <div class="form-group">
                <label for="fileChooser"> Photo: </label>
                <input type="file" name="fileChooserPhoto" id="fileChooserPhoto" class="form-control">     
              </div>
      <button type="submit" class="btn btn-success" name="submit">Submit</button>

            </form>
            </div>
        </div>


    </div>
<?php include "layout/footer.php"; ?>

viewStudent.php

} ?>
    <div class="container-fluid">
            <div class="row-fluid  table-responsive ">
            <table class="table table-striped table-bordered">
            <thead>
            <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Phone NO</th>
            <th>Form NO</th>
            <th>Group Type</th>
            <th>Lesson Time</th>
            <th>picture</th>
            </tr>
            </thead>
            <tbody>
            <?php $i=1;  while($DataRows = mysqli_fetch_assoc($result)):?>
            <tr>
            <td><?php echo $i++; ?></td>
            <td><?php echo $DataRows['name']?></td>
            <td><?php echo $DataRows['phoneNO']?></td>
            <td><?php echo $DataRows['formNO']?></td>
            <td><?php echo $DataRows['formNO']?></td>
            <td><?php echo $DataRows['LessonTime']?></td>
            </tr>
            <?php endwhile; ?>
            </tbody>
            </table>
</div>
        </div>
    </div>
</div>
</div>

<?php include "layout/footer.php"; ?>

db.php

<?php
    $db = mysqli_connect("127.0.0.1","root","","sms");
if (mysqli_connect_errno()) {
    echo "Database Query Faield: ".mysqli_connect_error();
    exit();
}
?>

function.php

<?php
include "db/db.php";
function Redirect_to($New_Locaton){
    header("Location:".$New_Locaton);
    exit;
}

session.php

<?php
    session_start();
    function errorMsg(){
        if(isset($_SESSION["errorMessage"])){
            $output = "<div class=\"alert alert-danger\">";
            $output .= htmlentities($_SESSION["errorMessage"]);
            $output .= "</div>";
            $_SESSION["errorMessage"] = null;
            return $output; 
        }

    }
    function successMsg(){
        if(isset($_SESSION["successMessage"])){
            $output = "<div class=\"alert alert-success\">";
            $output .= htmlentities($_SESSION["successMessage"]);
            $output .= "</div>";
            $_SESSION["successMessage"] = null;
            return $output; 
        }

    }

?>


head.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>School Drive System</title>
    <link href="css/bootstrap.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link href="css/font-awesome.css" rel="stylesheet">
    <link href="css/techer.css" rel="stylesheet" type="text/css">
</head>
<body>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="home.php">Driving School System</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><i class="fa fa-user-secret" aria-hidden="true">
You</i><span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="index.php" style="color:black;font-weight:normal">Log Out</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
 <div class="container-fluid">
 <div class="row">
    <div class="col-sm-2">
       <br><br>
        <ul id="side_Menu" class="nav nav-pills nav-stacked">
           <li class="active"><a href="home.php"><span class="glyphicon glyphicon-th"></span>&nbsp; Tools</a></li>

           <li><a href="addStudent.php"><span class="glyphicon glyphicon-comment"></span>&nbsp;&nbsp;Add Users</a></li>  
           <li><a href="viewStudent.php"><span class="glyphicon glyphicon-comment"></span>&nbsp;&nbsp;Users List</a></li>
           <li><a href="index.php"><span class="glyphicon glyphicon-equalizer"></span>&nbsp;&nbsp;Logout</a></li>

        </ul>

    </div><!-- Ending side area-->

footer.php

<div class="container">
<div class="row">
<div class="footer">
&copy; Copyright By Creavse.
</div>
</div>
</div>
 <script src="js/jquery-3.2.1.min.js"></script>
 <script src="js/bootstrap.min.js"></script>
</body>
</html>

感谢您的帮助。

0 个答案:

没有答案