我无法更新mysql(i)数据库。如何在数据库中设置用户类型?

时间:2019-03-30 09:52:01

标签: php mysql mysqli

我需要更新mysql数据库中的配置文件并在程序中设置用户类型。如何在此注册程序中上传图像?已经尝试了一切,php的新手,每次运行下面的程序时都会出错。如何删除存储的值?

Register.php

<?php
session_start();
require('connect.php');

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

$username = $_POST['username'];
$name = $_POST['name'];
$email = $_POST['email'];
$password = md5($_POST['password']);
$dob = $_POST['dob'];
$gender = $_POST['gender'];
$location = $_POST['location'];
$course = $_POST['course'];
$mobile = $_POST['mobile'];

 $query = "INSERT INTO `user`(username, name, password, email, dob, gender, location, course, mobile) VALUES ('$username', '$name', '$password',  '$email', '$dob', '$gender', '$location', '$course', '$mobile')";

       $result = mysqli_query ($mysqli,$query);
  if($result){
               $smsg = "Thank you!Your registration form has been successfully submitted.";
         }else{
               $fmsg = "This email already exists ";
             }
}

   ?>

<html>
<head>
<title>User Registration PHP & MYSQL</title>


<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap-theme.min.css" >
<link rel="stylesheet" href="styles.css" >


<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

</head>
<body>
    <div class="container">
      <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
      <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
        <form class="form-signin" method="POST">
        <h2 class="form-signin-heading">Please Register</h2>
        <div class="input-group">
          <span class="input-group-addon" id="basic-addon1">ID</span>
        <input type="text" name="username" class="form-control" placeholder="username" required></div>

          <label for="inputname" class="sr-only">Full Name</label>
          <input type="text" name="name" id="inputname" class="form-control" placeholder="Full Name"required>

          <label for="inputEmail" class="sr-only">Email address</label>
          <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address"required>

          <label for="inputPassword" class="sr-only">Password</label>
          <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>

          <label for="inputDoB" class="sr-only">DOB</label>
          <input type="date" name="dob" id="inputDoB" class="form-control" placeholder="DOB"required>

          <td>Gender:</td>
          <input type="radio" name="gender" value="m">Male
          <input type="radio" name="gender" value="f">female
          </tr>
          <label for="inputlocation" class="sr-only">Location</label>
          <input type="text" name="location" id="inputlocation" class="form-control" placeholder="Location"required>

          <label for="inputcourse" class="sr-only">Course</label>
          <input type="text" name="course" id="inputcourse" class="form-control" placeholder="Course" required>

          <label for="inputmobile" class="sr-only">Mobile</label>
          <input type="text" name="mobile" id="inputmobile" class="form-control" placeholder="Mobile" required>

          <button class="btn btn-lg btn-primary btn-block" type="submit" name="submit">Register</button>
          <a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>       
</div>
</body>
</head>
</html>

Edit.php

<?php
session_start();
require('connect.php');

if(isset($_POST['submit']) && ! empty($_POST['id'])){

$id = $_POST['id'];
$name = $_POST['name'];
$dob = $_POST['dob'];
$gender = $_POST['gender'];
$location = $_POST['location'];
$course = $_POST['course'];
$mobile = $_POST['mobile'];

 $sql = " UPDATE user SET name = '$name', 
                          dob ='$dob', 
                          gender ='$gender',
                          location ='$location', 
                          course = '$course', 
                          mobile = '$mobile' 
                          WHERE id= $id ";

    if (mysqli_query($mysqli, $sql)) {
      $smsg = "Record updated successfully";
   } else {
      $fmsg = "Error updating record: " . mysqli_error($mysqli);
   }
}

   ?>

<html>
<head>
<title>User Update PHP & MYSQL</title>


<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap-theme.min.css" >
<link rel="stylesheet" href="main.css" >


<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

</head>
<body>
    <div class="container">
    <a class= "float-right" href = "logout.php"> LOGOUT </a> 
      <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
      <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
        <form class="form-signin" method="POST">
        <h2 class="form-signin-heading">Edit Profile</h2>
        <div class="input-group">
        <input type="hidden" name="id" placeholder="ID" value="<?php echo $id;?>"></div>


          <label for="inputname" class="sr-only">Full Name</label>
          <input type="text" name="name" id="inputname" class="form-control" placeholder="Full Name"required>


          <label for="inputDoB" class="sr-only">DOB</label>
          <input type="date" name="dob" id="inputDoB" class="form-control" placeholder="DOB"required>

          <td>Gender:</td>
          <input type="radio" name="gender" value="m">Male
          <input type="radio" name="gender" value="f">female
          </tr>
          <label for="inputlocation" class="sr-only">Location</label>
          <input type="text" name="location" id="inputlocation" class="form-control" placeholder="Location"required>

          <label for="inputcourse" class="sr-only">Course</label>
          <input type="text" name="course" id="inputcourse" class="form-control" placeholder="Course" required>

          <label for="inputmobile" class="sr-only">Mobile</label>
          <input type="text" name="mobile" id="inputmobile" class="form-control" placeholder="Mobile" required>

          <button class="btn btn-lg btn-primary btn-block" type="submit" name="submit" value="Update data">Update</button>       
</div>
</body>
</head>
</html>

Login.php

<?php
//start session
session_start();
if(!isset($_SESSION['username'])){
    header('location:login.php');
}   
?>

<html>
<head>
<title>HomePage</title>

<link rel="stylesheet" href="main.css" >
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >

</head>

<body>
    <div class= "container" >
      <b><a class= "float-right" href = "logout.php"> LOGOUT </a></b> 
      <h1> Welcome</h1>  
      <h2>
<?php 
require("connect.php"); 
$query = "SELECT * FROM user";


echo '<table border="1" cellspacing="1" cellpadding="1"> 
      <tr>
          <td> <font face="Arial"><b>Username</font></b></td> 
          <td> <font face="Arial"><b>Full name</font></b></td> 
          <td> <font face="Arial"><b>Email ID</font></b></td> 
          <td> <font face="Arial"><b>Password</font></b></td> 
          <td> <font face="Arial"><b>Date of Birth</font></b></td>
          <td> <font face="Arial"><b>Gender</font></b></td> 
          <td> <font face="Arial"><b>location</font></b></td>
          <td> <font face="Arial"><b>Degree</font></b></td>
          <td> <font face="Arial"><b>Mobile</font></b></td>
          <td> <font face="Arial"><b>Update</font></b></td>
          <td> <font face="Arial"><b>Delete</font></b></td>

      </tr>';

if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $id=$row["id"];
        $username = $row["username"];
        $name = $row["name"];
        $email = $row["email"];
        $password = $row["password"];
        $dob = $row["dob"];
        $gender = $row["gender"];
        $location = $row["location"];
        $course = $row["course"];
        $mobile = $row["mobile"]; 

        echo '<tr>
                  <td>'.$username.'</td> 
                  <td>'.$name.'</td> 
                  <td>'.$email.'</td> 
                  <td>'.$password.'</td>
                  <td>'.$dob.'</td>
                  <td>'.$gender.'</td>
                  <td>'.$location.'</td>
                  <td>'.$course.'</td> 
                  <td>'.$mobile.'</td>
                  <td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>
                  <td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>
              </tr>';


    }
    $result->free();
} 
?></h2>  
      </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以从表单中的post变量中获取文件,这是上传文件并将其保存到storage / uploads文件夹的代码

$info = pathinfo($_FILES['your_file_name']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "newname.".$ext; 

$target = 'avatar/'.$newname;
move_uploaded_file( $_FILES['your_file_name']['tmp_name'], $target);

此处是该帖子,可帮助您为文件名创建随机字符串(您可以将该文件名保存到数据库中)https://stackoverflow.com/a/13212994/5928015