简体   繁体   中英

I can't update mysql(i) database. How to setup the user type in the database?

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM