如何使用mysql通过核心php提交表单数据?

dpiehjr4  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(358)

在我的代码中只是通过php将一些数据插入mysql数据库。插入所有数据,但只插入一列的数据。我的密码在这里:

<html>
    <form action="registration.php"  method="post">
      First Name <input name="st_name" type="text" id="st_name">
      Last Name  <input name="f_name" type="text" id="f_name">
      Department <input name="department" type="text" id="department">
      Registration No <input name="regi_no" type="text" id="regi_no"> 
      Email  <input name="email" type="text" id="email"> 
      pass:<input name="pass" type="password" id="pass">  
     <input type="submit" name="Submit" value="Submit">
     </form>
 </html>

提交后:

<?php 
    $st_name=$_POST["st_name"];   
    $st_lastname=$_POST["f_name"];      
    $dept=$_POST["department"];     
    $regi_no=$_POST["regi_no"];     
    $email=$_POST["email"];         
    $pass=$_POST["pass"];          

   $con = mysql_connect("localhost","root","");   // mysql connection

   mysql_select_db("ppp", $con);    

   mysql_query("INSERT INTO registration VALUES('$st_name','$st_lastname','$dept','$session','$regi_no','$email','$pass')") or die(mysql_error());
     mysql_close($con);
 ?>
isr3a4wc

isr3a4wc1#

创建表

CREATE TABLE IF NOT EXISTS `form` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `gender` varchar(255) NOT NULL,
  `hobby` varchar(255) NOT NULL,
  `education` varchar(255) NOT NULL,
  `file` varchar(255) NOT NULL,
  `gallery` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

连接.php

<?php
$con = mysqli_connect("localhost","root","","example_db");
?>

索引.php

<!DOCTYPE html>
<html lang="en">
<head>
  <title>PHP</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
  <body>
    <div class="container">
      <h2>Submit data by using core php</h2>
      <form  action="insert.php" method="POST" enctype="multipart/form-data"> 
        <div class="row">
          <div class="col-md-6">
            <div class="form-group row">
              <div class="col-sm-12">
                <label for="name" class="block">Name *</label>
              </div>
              <div class="col-sm-12">
                <input type="text" name="name" class="form-control">
              </div>
            </div>
          </div>
          <div class="col-md-6">
            <div class="form-group row">
              <div class="col-sm-12">
                <label for="education" class="block">Education *</label>
              </div>
              <div class="col-sm-12">
                <select id="education" name="education" class="form-control">
                  <option value="BE">BE</option>
                  <option value="ME">ME</option>
                  <option value="Btech">Btech</option>
                  <option value="Mtech">Mtech</option>
                </select>
              </div>
            </div>
          </div>
        </div>
        <div class="row">
         <div class="col-md-6">
          <div class="form-group row">
            <div class="col-sm-12">
              <label for="gender" class="block">Gender *</label>
            </div>
            <div class="col-sm-12">
              <input type="radio" name="gender" value="male" >Male
              <input type="radio" name="gender" value="female">Female
            </div>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group row">
            <div class="col-sm-12">
              <label for="hobby" class="block">Hobby *</label>
            </div>
            <div class="col-sm-12">
              <input type="checkbox" name="hobby[]" value="cricket" >cricket
              <input type="checkbox" name="hobby[]" value="wb" >wb
              <input type="checkbox" name="hobby[]" value="fb" >fb
            </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-md-6">
          <div class="form-group row">
            <div class="col-sm-12">
              <label for="file" class="block">File *</label>
            </div>
            <div class="col-sm-12">
             <input type="file" name="file" class="form-control">
           </div>
         </div>
       </div>
       <div class="col-md-6">
        <div class="form-group row">
          <div class="col-sm-12">
            <label for="photos" class="block">Gallary *</label>
          </div>
          <div class="col-sm-12">
            <input type="file" name="photos[]" multiple="" class="form-control">
          </div>
        </div>
      </div>
    </div>
    <input type="submit" name="submit" value="submit" id="submit" class="btn btn-info">
  </form>
  </div>
  </body>
</html>

插入.php

<?php
include("connect.php");
if(isset($_POST['submit'])){

    $name=$_POST['name'];
    $gender=$_POST['gender'];
    $education=$_POST['education'];

    $hb=$_POST['hobby'];
    $hobby=implode(",",$hb);

    $file=$_FILES['file']['name'];

    move_uploaded_file($_FILES['file']['tmp_name'],"fs/$file");

    $gallery=array();
        foreach ($_FILES['photos']['name'] as $key => $value) {
            $pic=$_FILES['photos']['name'][$key];
            move_uploaded_file($_FILES['photos']['tmp_name'][$key], "fs/$pic");
            array_push($gallery,$pic);
        }
    $g=implode(",", $gallery);

    $sql="INSERT INTO form (name,gender,hobby,education,file,gallery)VALUES('$name','$gender','$hobby','$education','$file','$g')";

    if(mysqli_query($con,$sql))
    {
        header("location:select.php");
    }
    else
    {
        echo"not ok";
    }
}
?>

选择.php

<?php include("connect.php");?>
<html>
<header>
</header>
    <body>
        <table>
            <tr>
                <td>id</td>
                <td>name</td>
                <td>gender</td>
                <td>hobby</td>
                <td>education</td>
                <td>image</td>
                <td>action</td>
            </tr>
                <?php 
                $sqls="SELECT * FROM form ";
                $query=mysqli_query($con,$sqls);
                while($row=mysqli_fetch_array($query)){
                ?>
            <tr>
                <td><?php echo $row['0']?></td>
                <td><?php echo $row['1']?></td>
                <td><?php echo $row['2']?></td>
                <td><?php echo $row['3']?></td>
                <td><?php echo $row['4']?></td>
                <td>
                <img src="fs/<?php echo $row['5'];?>" height="80" width="80"/></td>
                <td>
                <?php $temp=explode(",",$row['6']);
                foreach ($temp as $pik) {
                ?>
                <img src="fs/<?php echo $pik; ?>" height="80" width="80"/>
                <?php } ?>
                </td>
                <td><a href="delete.php?did=<?php echo $row['0']; ?>">Delete</a></td>
                <td><a href="update.php?uid=<?php echo $row['0']; ?>">Update</a></td>
            <?php } ?>
            </tr>
        </table>
    </body>
</html>

删除.php

<?PHP
include("connect.php");
$id=$_GET['did'];
$sqld="DELETE FROM form WHERE id=$id";
$queryq=mysqli_query($con,$sqld);
if($queryq){
    header("location:select.php");
}
?>

更新.php

<?php
include("connect.php");
$id=$_GET['uid'];
$sqls="SELECT * FROM form WHERE id='$id' ";
$query=mysqli_query($con,$sqls);
$row=mysqli_fetch_array($query);

if(isset($_POST['update'])){
     $name=$_POST['name'];
     $gender=$_POST['gender'];
     $hb=$_POST['hobby'];
     $hobby=implode(",",$hb);
     $education=$_POST['education'];
     $file=$_FILES['file']['name'];
    move_uploaded_file($_FILES['file']['tmp_name'],"fs/$file");

    $gall=array();
    foreach ($_FILES['photos']['name'] as $key => $value) {
        $gal=$_FILES['photos']['name'][$key];
        move_uploaded_file($_FILES['photos']['tmp_name'][$key], "fs/$gal");
        array_push($gall, $gal);
    }
    $g=implode(",", $gall);

    $sql="UPDATE  form SET name='$name',gender='$gender',hobby='$hobby',education='$education',file='$file', gallery='$g' WHERE id='$id'";
    $query=mysqli_query($con,$sql);
    if($query)
    {
        header("location:select.php");
    }
    else
    {
        echo"fail";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>PHP</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
  <div class="container">
    <h2>Submit data by using core php</h2>
    <form method="POST" action="" enctype="multipart/form-data">
      <div class="row">
        <div class="col-md-6">
          <div class="form-group row">
            <div class="col-sm-12">
              <label for="name" class="block">Name *</label>
            </div>
            <div class="col-sm-12">
              <input type="text" name="name" class="form-control" value="<?php echo $row['name'];?>">
            </div>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group row">
            <div class="col-sm-12">
              <label for="education" class="block">Education *</label>
            </div>
            <div class="col-sm-12">
              <select id="education" name="education" class="form-control">
                <option value="BE"  <?php if($row['education'] == 'BE') echo 'selected';?>>BE</option>
                <option value="ME"  <?php if($row['education'] == 'ME') echo 'selected';?>>ME</option>
                <option value="Btech"  <?php if($row['education'] == 'Btech') echo 'selected';?>>Btech</option>
                <option value="Mtech"  <?php if($row['education'] == 'Mtech') echo 'selected';?>>Mtech</option>
              </select>
            </div>
          </div>
        </div>
      </div>
      <div class="row">
         <div class="col-md-6">
          <div class="form-group row">
            <div class="col-sm-12">
              <label for="gender" class="block">Gender *</label>
            </div>
            <div class="col-sm-12">
              <input type="radio" name="gender" value="male" <?php if($row['gender'] == 'male') echo 'checked';?>>Male
              <input type="radio" name="gender" value="female" <?php if($row['gender'] == 'female') echo 'checked';?>>Female
            </div>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group row">
            <div class="col-sm-12">
              <label for="hobby" class="block">Hobby *</label>
            </div>
            <div class="col-sm-12">
                <?php 
                             $chkbox=$row['hobby'];
                             $arr=explode(",",$chkbox);
                      ?>
              <input type="checkbox" name="hobby[]" value="cricket" <?php if(in_array("cricket",$arr)){echo "checked";}?>>cricket
              <input type="checkbox" name="hobby[]" value="wb" <?php if(in_array("wb",$arr)){echo "checked";}?>>wb
              <input type="checkbox" name="hobby[]" value="fb" <?php if(in_array("fb",$arr)){echo "checked";}?>>fb
            </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-md-6">
          <div class="form-group row">
            <div class="col-sm-12">
              <label for="file" class="block">File *</label>
            </div>
            <div class="col-sm-12">
             <img src="fs/<?php echo $row['file'];?>" height="80" width="80">
             <input type="file" name="file" class="form-control">
           </div>
         </div>
       </div>
       <div class="col-md-6">
        <div class="form-group row">
          <div class="col-sm-12">
            <label for="photos" class="block">Gallary *</label>
          </div>
          <div class="col-sm-12">
            <?php $temp=explode(",", $row['gallery']);
                    for($i=0;$i<count($temp);$i++){?>
                    <img src="fs/<?php echo $temp[$i];?>" height="80" width="80"> <?php }?>
            <input type="file" name="photos[]" multiple="" class="form-control">
          </div>
        </div>
      </div>
    </div>
    <input type="submit" name="update" value="update" id="submit" class="btn btn-info">
  </form>
</div>
</body>
</html>

相关问题