gpt4 book ai didi

php - 将 php 页面连接到 mySql

转载 作者:行者123 更新时间:2023-11-28 02:42:24 25 4
gpt4 key购买 nike

我在将任何 php 页面连接到 MySQL 时遇到问题,我是 php 的新手,我尝试了所有可以在网上找到的方法,但都没有用。我尝试下载一个 php 表单并为此页面创建了一个数据库,但它仍然没有向数据库添加任何内容。

<html>

<link href="//db.onlinewebfonts.com/c/a4e256ed67403c6ad5d43937ed48a77b?family=Core+Sans+N+W01+35+Light" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="form.css" type="text/css">
<div class="body-content">
<div class="module">
<h1>Create an account</h1>

<?php
session_start();
$_SESSION['message'] = '';
$mysqli = new mysqli("127.0.0.1", "root", "", "accounts_complete");

//the form has been submitted with post
if ($_SERVER["REQUEST_METHOD"] == "POST") {

//two passwords are equal to each other
if ($_POST['password'] == $_POST['confirmpassword']) {

//set all the post variables
$username = $mysqli_real_escape_string($_POST['username']);
$email = $mysqli_real_escape_string($_POST['email']);
$password = md5($_POST['password']); //md5 has password for security
$avatar_path = $mysqli_real_escape_string('images/'.$_FILES['avatar']['name']);

//make sure the file type is image
if (preg_match("!image!",$_FILES['avatar']['type'])) {

//copy image to images/ folder
if (copy($_FILES['avatar']['tmp_name'], $avatar_path)){

//set session variables
$_SESSION['username'] = $username;
$_SESSION['avatar'] = $avatar_path;

//insert user data into database
$sql = "INSERT INTO users (username, email, password, avatar) "
. "VALUES ('$username', '$email', '$password', '$avatar_path')";

//if the query is successsful, redirect to welcome.php page, done!
if ($mysqli_query($sql) === true){
$_SESSION['message'] = "Registration succesful! Added $username to the database!";
header("location: welcome.php");
}
else {
$_SESSION['message'] = 'User could not be added to the database!';
}
$mysqli_close();
}
else {
$_SESSION['message'] = 'File upload failed!';
}
}
else {
$_SESSION['message'] = 'Please only upload GIF, JPG or PNG images!';
}
}
else {
$_SESSION['message'] = 'Two passwords do not match!';
}
}
?>


<form class="form" action="form.php" method="post" enctype="multipart/form-data" autocomplete="off">
<div class="alert alert-error"><?= $_SESSION['message'] ?></div>
<input type="text" placeholder="User Name" name="username" required />
<input type="email" placeholder="Email" name="email" required />
<input type="password" placeholder="Password" name="password" autocomplete="new-password" required />
<input type="password" placeholder="Confirm Password" name="confirmpassword" autocomplete="new-password" required />
<div class="avatar"><label>Select your avatar: </label><input type="file" name="avatar" accept="image/*" required /></div>
<input type="submit" value="Register" name="register" class="btn btn-block btn-primary" />
</form>
</div>
</div>
</html>

密码为空,因为没有任何密码。你能帮助我吗?提前致谢。

最佳答案

其实有很多错误,但是基于数据库的问题:

  1. 所有mysqli_ 语法都是函数,不是变量。所以你只需要删除 mysqli_ 语法中的任何初始 $
  2. 在 mysqli 中,您需要将连接变量传递给 mysqli_ 函数的第一个参数。

例子:

之前:$mysqli_query($sql);

之后:mysqli_query($mysqli, $sql)

之前:$mysqli_close();

之后:mysqli_close($mysqli)

关于php - 将 php 页面连接到 mySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47099728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com