Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How to update different value for each row in Mysql with PHP?

Hi teamtreehouse team.

I have part of code which I don't know how to resloved. Please if some one can tell me on which way I must search resloved for my problems.

I want update different value for each ID in my Mysql with PHP.

Now this is my code now, and this code now update same value for all ID in Mysql.

<?php

$servername = "localhost"; $username = "root"; $password = ""; $dbname = "dataBase";

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

$result = mysqli_query($conn,"SELECT subscription_end, today_date FROM users_manager");

while($date = mysqli_fetch_array($result)) { echo $date['subscription_end']; echo $date['today_date'];

if ($date['subscription_end'] <= $date['today_date']) { $update = "UPDATE users_manager SET status='on'"; } else { $update = "UPDATE users_manager SET status='of'"; } }

if ($conn->query($update) === TRUE) { echo "<br>Record updated successfully"; } else { echo "Error updating record: " . $conn->error; }

?>

As you can see in Mysql I have database named "dataBase" and table named "users_manager" and rows named "ID" "Name" "Lastname" "subscription_end" "today_date" and "status".

If I have 5 users in "users_manager" all of that users have same vale but I dont want that, I need check for all user and update different value.

Thx

1 Answer

Robert Leonardi
Robert Leonardi
17,151 Points

What do you mean by same vale? If you input 5 different users details/values to the dataBase, then your logic should be fine.

Check your echo inside the while loop, if it loops all 5 then .. check your UPDATE sql code. Try update manually in phpmyadmin for updating 1 user, it it works then you are all set with the while loop