Greenwitch88 Posted October 8, 2022 Share Posted October 8, 2022 (edited) Hi. I'm new to this forum. My name is Elise. I am trying to create a dog game. So, I have a script that changes the dog feed from respectively to +1 or +14 and so on. This is my code: <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $sql = "UPDATE Hundar SET feed++ WHERE user=1"; } ?> <!DOCTYPE html> <html lang="sv"> <head> <title>Canis-Ludwig! — Hund SIM</title> <meta charset="UTF-8"> </head> <body> <h1><?php echo "Canis-Ludwig";?></h1> <?php echo $feed;?> <input type="submit" value="Mata hunden"><br><br> <hr> <footer><?php echo "Copyright © aloelissan";?></footer> </body> </html> So, I'm trying to apply the dog feed update ONLY to user that is logged in, for say ID #1106. As you also can see I lack knowledge about MySQL because I don't know how to write the feed++ at SET. Edited October 8, 2022 by Greenwitch88 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2022 Share Posted October 8, 2022 SET feed = feed + 1 If that is the whole of your code then there are a lot of other things you don't know. Quote Link to comment Share on other sites More sharing options...
Greenwitch88 Posted October 8, 2022 Author Share Posted October 8, 2022 (edited) Thanks. But how about the user=1 being the user that is logged in? Edited October 8, 2022 by Greenwitch88 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2022 Share Posted October 8, 2022 Do you store the id of the logged in user in a $_SESSION variable when they successfully log in? Quote Link to comment Share on other sites More sharing options...
Greenwitch88 Posted October 8, 2022 Author Share Posted October 8, 2022 No, because I don't know how to do that. 😮 Can you help me? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2022 Share Posted October 8, 2022 Not from the code you've posted. Telepathy is not in my skillset. What is your log in code? Also Where do you connect to the database? Where do you execute the query you just created? Where do you check if the query was sucessful? What happens if the feed value is already 14? Quote Link to comment Share on other sites More sharing options...
Greenwitch88 Posted October 8, 2022 Author Share Posted October 8, 2022 Yes, I do have a connect.php file, including this code: <?php $servername = "localhost"; $username = "root"; $password = "Canopus88!"; $db = "whatever"; // Create connection $conn = mysqli_connect($servername, $username, $password, $db); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?> The code I just executed I use in kennel.php, I don't know what you mean with check if the query was successful... The feed status is stored in my database in a column called feed whereas the amount is stored there, in a table called dogs Quote Link to comment Share on other sites More sharing options...
Greenwitch88 Posted October 8, 2022 Author Share Posted October 8, 2022 I have problems with AMPPS, because MySQL don't want to start. How do I solve this? Quote Link to comment Share on other sites More sharing options...
Greenwitch88 Posted October 8, 2022 Author Share Posted October 8, 2022 I would love to learn PHP & MySQL so that I can start blogging. Quote Link to comment Share on other sites More sharing options...
Greenwitch88 Posted October 8, 2022 Author Share Posted October 8, 2022 I initially wanted to create a game, and I'm still wondering about whether to check if the user is logged in, and apply food to the owner's dog depending on ID. But I will use this knowledge for a blog, as well. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2022 Share Posted October 8, 2022 6 minutes ago, Greenwitch88 said: I don't know what you mean with check if the query was successful... There is no guarantee a query will always work, even if there are no errror. However for a query to fail it has to be executed and I saw no such code in the code you posted. However the best way to check is to put tis line of code mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); immediately before the line where you call mysqli_connect(). Make sure that error_repoting is set to E_ALL and display_error is on (php.ini file settings) 10 minutes ago, Greenwitch88 said: The feed status is stored in my database in a column called feed whereas the amount is stored there, in a table called dogs I know, I read your update query string. You said the feed value can be from +1 to +14. Your query always adds 1, but what if the value is already 14 - what should happen then? Quote Link to comment Share on other sites More sharing options...
Greenwitch88 Posted November 5, 2022 Author Share Posted November 5, 2022 I am going to increase the food by +14 up to +100, so please refrase your question :) Quote Link to comment Share on other sites More sharing options...
Greenwitch88 Posted November 5, 2022 Author Share Posted November 5, 2022 I am the world's best programmer Quote Link to comment Share on other sites More sharing options...
Greenwitch88 Posted November 11, 2022 Author Share Posted November 11, 2022 How do I create a chat system, how do I create a message, send and retrieve it and from a second player? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.