Note that there are some explanatory texts on larger screens.

plurals
  1. POStore multiple values on MySQL table column and use them in a query?
    primarykey
    data
    text
    <p>Sorry for the hard-to-understand title, but here goes nothing:</p> <p>I've two kinds of users on my users table, <code>trainee</code> &amp; <code>coach</code>. </p> <p><code>Trainee</code> can add a <code>coach</code>, and <code>coach</code> would see the things <code>trainee</code> would create on the database. </p> <p>This would be simple, if <code>coach</code> could only have 1 <code>trainee</code>, simply storing <code>trainee</code>'s userID on a column called <code>iscoachof</code>. However, multiple users have to be able to add the coach, so <code>iscoachof</code> could contain a lot more userID's than one. Like <code>34,72,345,87,97</code>.</p> <p>So, here's my current code that would update the column <code>iscoachof</code> with the new value.</p> <pre><code> $fd = $_POST['coach']; $user = $_SESSION['login']['id']; $query = "UPDATE users SET iscoachof='$user' WHERE id='$fd'"; $add = $db-&gt;prepare($query); if($add-&gt;execute()){ echo "Lisätty"; } else{ echo "Ei lisätty"; } </code></pre> <p>It simply overwrites the current value, and this is not an option. </p> <p>So, I need to do something like</p> <pre><code>$currentval = "SELECT iscoachof FROM users WHERE id='$fd'; $do = $db-&gt;prepare($currentval); $do-&gt;execute(); while($row = $do-&gt;fetchObject()){ $currentval = $row-&gt;iscoachof; } </code></pre> <p>To get the current value before updating it, but how should I store it so I can use it later on a query like this on the place of <code>$data</code></p> <pre><code>$q = "SELECT * FROM diaries WHERE UserID='$data' AND date='$today'"; $do = $db-&gt;prepare($q); $do-&gt;execute(); </code></pre> <p>So basically, <strong>is it possible to store it in an array, save it to the database on extract the values later on?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload