gpt4 book ai didi

php - 使用 PHP 获得正确响应,但 MySQL 数据库未更新

转载 作者:行者123 更新时间:2023-11-29 18:26:28 26 4
gpt4 key购买 nike

我正在尝试创建一个简单的注册表单。我有一个用于连接数据库的文件

conn.php

<?php
$db_name = "bp_reader";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";
$conn = mysqli_connect ($server_name, $mysql_username, $mysql_password, $db_name);

/*the connection here is fine*/
if($conn){ echo "connected"; }else{ echo "not connected"; }

?>

注册php

<?php
require "conn.php";

$name = $_POST["name"];
$email = $_POST["email"];
$age = $_POST["age"];
$height = $_POST["height"];
$weight = $_POST["weight"];
$password = $_POST["password"];

//check if user exists
$sql = "select * from user_profile where user_email like '".$email."';";

$result = mysqli_query($conn, $sql);

$response = array();

if(mysqli_num_rows($result) > 0){

$code = "registration failed";
$message = "User already exists";


array_push($response, array("code"=>$code, "message"=>$message));

echo json_encode($response);

}else {
$sql = "insert into user_profile values ('".$name."', '".$email."', '".$age."', '".$height."', '".$weight."', '".$password."');";

$result = mysqli_query($conn, $sql);

$code = "registration Success";
$message = "Thank you for registration... you can login now..";

//jason data
array_push($response, array("code"=>$code, "message"=>$message));

echo json_encode($response);
}

mysqli_close($conn);
?>

我有一个简单的 html 注册表

<html>

<body>
<form action="register.php" method="post">
<table>
<tr>
<td>Name:</td>
<td>
<input type="text" name="name" /> </td>
</tr>
<tr>
<td>Email:</td>
<td>
<input type="email" name="email" /> </td>
</tr>
<tr>
<td>DOB:</td>
<td>
<input type="date" name="age" /> </td>
</tr>
<tr>
<td>height:</td>
<td>
<input type="number" name="height" /> </td>
</tr>
<tr>
<td>weight:</td>
<td>
<input type="number" name="weight" /> </td>
</tr>
<tr>
<td>Password:</td>
<td>
<input type="password" name="password" /> </td>
</tr>
<tr>
<td>
<input type="submit" value="Register" /> </td>
</tr>
</table>
</form>
</body>
</html>

问题是当我填写表格并提交时,我得到了注册成功的正确响应,但是当我在 phpMyAdmin 上检查我的数据库时,数据库保持不变。我无法弄清楚问题出在哪里。

最佳答案

如果你有 id 字段作为标识符和自动增量,你应该设置它:

   $sql = "insert into user_profile values ('', '".$name."', '".$email."', '".$age."', '".$height."', '".$weight."', '".$password."');";

关于php - 使用 PHP 获得正确响应,但 MySQL 数据库未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46150624/

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