dounao2829 2019-03-30 09:52
浏览 68

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

I need update my profile in the mysql database and to setup the user type in the program. How to upload image in this register program? have tried everything, new to php and I am getting an error every time running my below program. How to delete the stored values?

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条回答 默认 最新

  • douzhao5656 2019-03-30 09:59
    关注

    You can get the file in the post variable from the form and here is the code to upload file and saving it into the storage/uploads folder

    $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);
    

    Here is the post which will helps you create random string for the filename (you can save this filename into the database) https://stackoverflow.com/a/13212994/5928015

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。