无法在php中将数据添加到数据库中

s4n0splo  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(481)
<?php
if(isset($_POST["submit"]))
 if (!empty($_FILES["uploadImage"]["name"])) {

  //Including dbconfig file.
 require 'config.php';

  $ImageSavefolder = "images/student/";
move_uploaded_file($_FILES["uploadImage"]["tmp_name"] , 
 "$ImageSavefolder".$_FILES["uploadImage"]["name"]);
$name = mysqli_real_escape_string($conn, $_POST['name']);
 $fathername = mysqli_real_escape_string($conn, $_POST['fathername']);
    $htno = mysqli_real_escape_string($conn, $_POST['htno']);
     $phoneno = mysqli_real_escape_string($conn, $_POST['phoneno']);
     $department = mysqli_real_escape_string($conn, $_POST['department']);
    $class = mysqli_real_escape_string($conn, $_POST['class']);
  $address = mysqli_real_escape_string($conn, $_POST['address1']);
 $address2 = mysqli_real_escape_string($conn, $_POST['address2']);
     $city = mysqli_real_escape_string($conn, $_POST['city']);
    $state = mysqli_real_escape_string($conn, $_POST['state']);
  $zip = mysqli_real_escape_string($conn, $_POST['zip']);

   $sql= "INSERT INTO student_detail(name,fathername,htno,phoneno,department,class,address1,address2,city,state,zip) VALUES ('$name','$fathername','$htno','$phoneno','$department','$class','$address','$address2','$city','$state','$zip','".$_FILES['uploadImage']['name']."')"; 

  if(!mysqli_query($conn,$sql))
    {
     echo "Not Updated";
   } 
    else
     {
      echo "<br><div class='alert alert-success' role='alert'>Added Sucessfully !</div>";
     }

    }

 ?>

此代码不适用于将以下数据添加到数据库中。我做错什么了吗?请帮我解决这个问题。我已经用创建了数据库 config.php 表单数据

<form method="post" action="" enctype="multipart/form-data">
  <div class="form-row">
   <div class="form-group col-md-6">
    <label for="name">Name</label>
     <input type="text" class="form-control" placeholder="Please Enter Name" name="name">
    </div>
    <div class="form-group col-md-6">
     <label for="fathername">Father's Name</label>
      <input type="text" class="form-control"  placeholder="Please Enter Father's Name" name="fathername">
     </div>
     <div class="form-group col-md-6">
     <label for="htno">Hall Ticket/ Roll No.</label>
      <input type="text" class="form-control"  placeholder="Please Enter Hall Ticket/ Roll No." name="htno">
     </div>
     <div class="form-group col-md-6">
     <label for="phoneno">Phone Number</label>
      <input type="text" class="form-control" placeholder="Please Enter Phone No." name="phoneno">
     </div>
     <div class="form-group col-md-6">
     <label for="department">Department</label>

      <select  class="form-control" name="department">
          <option selected="selected">Choose your Department</option>
          <?php
          require('config.php');
          $result = mysqli_query($conn,"SELECT * FROM department");

          while($test= mysqli_fetch_array($result))
           {
          echo "<option value='".$test['department_name']."'>".$test['department_name']."</option>";
            }
           ?>
         </select>
     </div>
     <div class="form-group col-md-6">
     <label for="class">Class</label>
      <select class="form-control" name="class">
          <option selected="selected">Choose your Class</option>
          <?php
          require('config.php');
   $result = mysqli_query($conn,"SELECT * FROM class");

  while($test= mysqli_fetch_array($result))
{
 echo "<option value='".$test['class_name']."'>".$test['class_name']."
   </option>";
  }
   ?>
         </select>
     </div>
    </div>
   <div class="form-group">
    <label for="address">Address</label>
     <input type="text" class="form-control"  placeholder="House No./Flat No." name="address1">
    </div>
    <div class="form-group">
     <label for="address2">Address 2 (Optional)</label>
      <input type="text" class="form-control" placeholder="Locality/Area/Street" name="address2">
     </div>
     <div class="form-row">
      <div class="form-group col-md-6">
       <label for="city">City</label>
        <input type="text" class="form-control"  name="city">
       </div>
       <div class="form-group col-md-4">
        <label for="state">State</label>
         <select  class="form-control" name="state">
          <option selected="selected"name="bihar">bihar</option>
          <option>...</option>
         </select>
        </div>
        <div class="form-group col-md-2">
         <label for="zip">Zip</label>
          <input type="text" class="form-control"  name="zip">
           </div>
           <div class="form-group col-md-6">
            <label for="profile">Profile Pic</label><br>
            <input type="file" accept="image/*" onchange="loadFile(event)" name="uploadImage" id="uploadImage">
            <img id="output" style="width:20%;"/>
              <script>
                var loadFile = function(event) {
                var reader = new FileReader();
                reader.onload = function(){
                var output = document.getElementById('output');
                output.src = reader.result;
                 };
                reader.readAsDataURL(event.target.files[0]);
                 };
               </script>
           </div>

          </div>
           <button type="submit" name="submit" class="btn btn-primary">Submit</button>
          </form>
bd1hkmkf

bd1hkmkf1#

<?php
/*  dbconnection.php file
    $conn = mysqli_connect("localhost","root","12345") or die (mysqli_error());
    mysqli_select_db($conn,"student") or die (mysqli_error());

* /

//Including dbconnection file here
include('dbconnection.php');
    if(isset($_POST["submit"]))
    {
        if (!empty($_FILES["uploadImage"]["name"])) 
        {
            $ImageSavefolder = "images/student/";
            $name = $_FILES["uploadImage"]["name"];
            $tmp_name = $_FILES["uploadImage"]["tmp_name"];
            move_uploaded_file(tmp_name, $ImageSavefolder.$name);
            $sql = "INSERT INTO students (name,fathername,htno,phoneno,department,class,address1,address2,city,state,zip,image) VALUES ('".$_POST["name"]."','".$_POST["fathername"]."','".$_POST["htno"]."','".$_POST["phoneno"]."','".$_POST["department"]."','".$_POST["class"]."','".$_POST["address1"]."','".$_POST["address2"]."','".$_POST["city"]."','".$_POST["state"]."','".$_POST["zip"]."','".$name."')";
            if ($conn->query($sql) === TRUE)
            {
                echo "<script type= 'text/javascript'>alert('Record Inserted Successfully');</script>";
            } 
            else 
            {
                echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
            }
        }
    }
?>
f4t66c6m

f4t66c6m2#

您试图插入查询中不存在的额外值,请参见此处。你的领域

(name,fathername,htno,phoneno,department,class,address1,address2,city,state,zip)

你的价值观呢

('$name','$fathername','$htno','$phoneno','$department','$class','$address','$address2','$city','$state','$zip','".$_FILES['uploadImage']['name']."')

您还需要添加图像字段来完成完整的查询

$sql= "INSERT INTO student_detail(name,fathername,htno,phoneno,department,class,address1,address2,city,state,zip,imageFieldName) VALUES ('$name','$fathername','$htno','$phoneno','$department','$class','$address','$address2','$city','$state','$zip','".$_FILES['uploadImage']['name']."')";

相关问题