MySQL为每个新用户创建一行

7y4bm7vi  于 2023-11-16  发布在  Mysql
关注(0)|答案(1)|浏览(155)

我有一个小问题
我想创建一个脚本,在表中创建一个新的行,如果有一个新的用户和行,更改“点”列为零(0)。
这是我现在的代码:

  1. <?php
  2. header('Content-Type: text/html; charset=Windows-1250');
  3. $firstName = $_POST['firstname'];
  4. $servername = "db.mysql-01.gsp-europe.net";
  5. $username = "xxxxxxxxxx";
  6. $password = "xxxxxxxxxxx";
  7. $dbname = "xxxxxxxxxxx";
  8. // Create connection
  9. $conn = new mysqli($servername, $username, $password, $dbname);
  10. // Check connection
  11. if ($conn->connect_error) {
  12. die("Connection failed: " . $conn->connect_error);
  13. }
  14. // sql to create table
  15. $sql = "UPDATE `member_profile` SET points = points + 1 WHERE user_id = '$firstName'";
  16. if ($conn->query($sql) === TRUE) {
  17. echo "Thingz created successfully";
  18. } else {
  19. echo "Error doing sum thingz: " . $conn->error;
  20. }
  21. $conn->close();
  22. ?>

字符串
我需要在立方体:当出现新的user_id($firstName)时,使用此用户名创建新行,并将“points”列从“null”更改为Zero(0)。
谢谢你的时间,我很感激

qybjjes1

qybjjes11#

如果我理解得很好,你要检查用户是否存在。如果用户是新的,创建新的线与用户0点,如果存在increse点与1。

  1. <?php
  2. header('Content-Type: text/html; charset=Windows-1250');
  3. if(isset($_POST['firstname'])){
  4. $firstName = $_POST['firstname'];
  5. $servername = "db.mysql-01.gsp-europe.net";
  6. $username = "xxxxxxxxxx";
  7. $password = "xxxxxxxxxxx";
  8. $dbname = "xxxxxxxxxxx";
  9. // Create connection
  10. $conn = new mysqli($servername, $username, $password, $dbname);
  11. // Check connection
  12. if ($conn->connect_error) {
  13. die("Connection failed: " . $conn->connect_error);
  14. }
  15. // check if the user exist
  16. $check = "SELECT * FROM `member_profile` WHERE user_id = '$firstName'";
  17. $result = mysqli_query($conn,$check) or die(mysqli_error($conn));
  18. $rows = mysqli_num_rows($result);
  19. //if exist increse points with 1
  20. if($rows>=1){
  21. $sql = "UPDATE `member_profile` SET points = points + 1 WHERE user_id = '$firstName'";
  22. if ($conn->query($sql) === TRUE) {
  23. echo "Thingz created successfully";
  24. } else {
  25. echo "Error doing sum thingz: " . $conn->error;
  26. }
  27. }
  28. //if don't exist create user with points 0
  29. if($rows==0)
  30. {
  31. $query = "INSERT into `member_profile` (user_id, points) VALUES ( '$firstName' ,'0')";
  32. $result = mysqli_query($conn,$query)or die(mysqli_error($conn));
  33. $conn->close();
  34. }
  35. }
  36. ?>

字符串
记住,我给了你一个想法,代码容易被SQL注入

展开查看全部

相关问题