简体   繁体   中英

i want each user to be able to add data with their id, so when they log in they can only view data that they added

the code works fine when it is only one user but once i create a second user, the data doesn't get inserted into the database

$sql = "INSERT INTO images ( users_id, picture, Carehome, dateworked, hours) 
    VALUES ((SELECT id FROM users  ), '$target_file', '$Carehome', '$dateworked', '$hours')";

You call all users ID

SELECT id FROM users

You can solve it by get it id by id

<?php
$sql_ids = "SELECT id FROM users";
//get users row by row
//your_db_conn is example you must replace it by your variable 
while($row = mysqli_fetch_assoc(mysqli_query(your_db_conn,$sql_ids))){
//save user id in $id
    $id = $row['id'];
    $sql = "INSERT INTO images ( users_id, picture, Carehome, dateworked, hours) 
    VALUES ('$id', '$target_file', '$Carehome', '$dateworked','$hours')";
    mysqli_query(your_db_conn,$sql);
}
?>

Make sure user id is not a primary key in images table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM