php使用check box

if (isset($_POST['submit'])) {
    foreach ($_POST['todelete'] as $delete_id) { //这里是循环遍历这个数组 todelete 每一个值被设置成 $delete_id
      $query = "DELETE FROM email_list WHERE id = $delete_id";
      mysqli_query($dbc, $query)
        or die('Error querying database.');
    } 

    echo 'Customer(s) removed.<br />';
  }

  // Display the customer rows with checkboxes for deleting
  $query = "SELECT * FROM email_list";
  $result = mysqli_query($dbc, $query);
  while ($row = mysqli_fetch_array($result)) {

    // todelete[]是一个数组 能够把checkbox中选中的放到这个数组中
    echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />'; 
    echo $row['first_name'];
    echo ' ' . $row['last_name'];
    echo ' ' . $row['email'];
    echo '<br />';
  }

这里是一个简单的 PHP 登录注册页面的完整代码示例,包括 HTML、CSS 和 PHP 代码: HTML 代码: ``` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login / Register</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <div class="login-box"> <h2>Login</h2> <form action="login.php" method="post"> <div class="form-group"> <label>Username</label> <input type="text" name="username" required> </div> <div class="form-group"> <label>Password</label> <input type="password" name="password" required> </div> <button type="submit" name="login">Login</button> </form> </div> <div class="register-box"> <h2>Register</h2> <form action="register.php" method="post"> <div class="form-group"> <label>Username</label> <input type="text" name="username" required> </div> <div class="form-group"> <label>Email</label> <input type="email" name="email" required> </div> <div class="form-group"> <label>Password</label> <input type="password" name="password" required> </div> <div class="form-group"> <label>Confirm Password</label> <input type="password" name="confirm_password" required> </div> <button type="submit" name="register">Register</button> </form> </div> </div> </body> </html> ``` CSS 代码: ``` .container { display: flex; justify-content: center; align-items: center; height: 100vh; } .login-box, .register-box { width: 350px; background-color: #fff; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); padding: 20px; margin-right: 20px; } .login-box h2, .register-box h2 { margin-top: 0; } .form-group { margin-bottom: 10px; } label { display: block; margin-bottom: 5px; } input[type="text"], input[type="email"], input[type="password"] { width: 100%; padding: 10px; border-radius: 5px; border: none; box-shadow: 0 0 5px rgba(0, 0, 0, 0.1); } button[type="submit"] { display: block; width: 100%; padding: 10px; margin-top: 10px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; } ``` PHP 注册页面代码(register.php): ``` <?php session_start(); // Check if user is already logged in if (isset($_SESSION['username'])) { header('Location: home.php'); exit; } // Check if form is submitted if (isset($_POST['register'])) { $username = $_POST['username']; $email = $_POST['email']; $password = $_POST['password']; $confirm_password = $_POST['confirm_password']; // Check if passwords match if ($password !== $confirm_password) { $error = 'Passwords do not match.'; } else { // Connect to database $conn = mysqli_connect('localhost', 'username', 'password', 'database_name'); // Check if username or email already exists $query = "SELECT * FROM users WHERE username='$username' OR email='$email'"; $result = mysqli_query($conn, $query); if (mysqli_num_rows($result) > 0) { $error = 'Username or email already exists.'; } else { // Hash password $hashed_password = password_hash($password, PASSWORD_DEFAULT); // Insert user into database $query = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$hashed_password')"; mysqli_query($conn, $query); // Redirect to login page header('Location: index.php'); exit; } } } ?> ``` PHP 登录页面代码(login.php): ``` <?php session_start(); // Check if user is already logged in if (isset($_SESSION['username'])) { header('Location: home.php'); exit; } // Check if form is submitted if (isset($_POST['login'])) { $username = $_POST['username']; $password = $_POST['password']; // Connect to database $conn = mysqli_connect('localhost', 'username', 'password', 'database_name'); // Check if username exists $query = "SELECT * FROM users WHERE username='$username'"; $result = mysqli_query($conn, $query); if (mysqli_num_rows($result) > 0) { $row = mysqli_fetch_assoc($result); $hashed_password = $row['password']; // Verify password if (password_verify($password, $hashed_password)) { // Set session variables $_SESSION['username'] = $username; // Redirect to home page header('Location: home.php'); exit; } else { $error = 'Incorrect password.'; } } else { $error = 'Username not found.'; } } ?> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值