gpt4 book ai didi

php - MYSQL 为每个新用户创建一行

转载 作者:行者123 更新时间:2023-11-29 07:21:17 25 4
gpt4 key购买 nike

我有个小问题

我想创建一个脚本,在表格中创建一个新行,如果有一个新用户并且在该行中,将“点”列更改为零(0)

这是我当前的代码:

<?php
header('Content-Type: text/html; charset=Windows-1250');
$firstName = $_POST['firstname'];



$servername = "db.mysql-01.gsp-europe.net";
$username = "xxxxxxxxxx";
$password = "xxxxxxxxxxx";
$dbname = "xxxxxxxxxxx";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// sql to create table

$sql = "UPDATE `member_profile` SET points = points + 1 WHERE user_id = '$firstName'";


if ($conn->query($sql) === TRUE) {
echo "Thingz created successfully";
} else {
echo "Error doing sum thingz: " . $conn->error;
}

$conn->close();



?>

我在多维数据集中需要什么:当出现新的 user_id ($firstName) 时,使用该用户名创建新行,并将“points”列从“null”更改为 Zero(0)

谢谢你的时间,我很感激

最佳答案

如果我理解得很好,你想检查用户是否存在。如果用户是新用户,则用 0 分创建新行,如果存在则增加 1 分。


<?php

header('Content-Type: text/html; charset=Windows-1250');
if(isset($_POST['firstname'])){
$firstName = $_POST['firstname'];



$servername = "db.mysql-01.gsp-europe.net";
$username = "xxxxxxxxxx";
$password = "xxxxxxxxxxx";
$dbname = "xxxxxxxxxxx";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// check if the user exist
$check = "SELECT * FROM `member_profile` WHERE user_id = '$firstName'";
$result = mysqli_query($conn,$check) or die(mysqli_error($conn));
$rows = mysqli_num_rows($result);
//if exist increse points with 1
if($rows>=1){

$sql = "UPDATE `member_profile` SET points = points + 1 WHERE user_id = '$firstName'";


if ($conn->query($sql) === TRUE) {
echo "Thingz created successfully";
} else {
echo "Error doing sum thingz: " . $conn->error;
}
}
//if don't exist create user with points 0
if($rows==0)
{
$query = "INSERT into `member_profile` (user_id, points) VALUES ( '$firstName' ,'0')";

$result = mysqli_query($conn,$query)or die(mysqli_error($conn));
$conn->close();

}
}

?>

记住,我给过你一个思路,代码容易被sql inject

关于php - MYSQL 为每个新用户创建一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56233117/

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