Jump to content

Hi, there! I'm new here. Need help with MySQL SELECT * WHERE statement [to select a user's dog only depending on who's logged in]


Greenwitch88

Recommended Posts

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! &mdash; 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 &copy; 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 by Greenwitch88
Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • 4 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.