Request Detail for single request

Date this request was submitted: 2022-05-18 17:28:49 (4 months ago)

Posted by:

Course: Designing Solutions Through Programming (Block 1, Room C152)

Date this request was closed: 2022-07-29 10:25:14 (2 months ago)

Status: closed

Priority: High


Original request for help:

What I'm trying to do is add file names that are writen as variables into the database for a specific user (when they are logged in) so that I can use a for loop or something like that to iterate through all the file names. This would make it so that when logged in, the user can see all the stock markets they have added to their "favorites". Right now, my code does add the file name into the database but it adds it a completely new row instead of adding it to the logged in user's table. I'm also not sure if there is a better way to do this because I don't know if it's possible to have several variables put into the same database row. An image of my database and the code is shown belolw.

https://drive.google.com/file/d/1yazT0E5ulEjRQR_cxV5VxC-vgNEm0PLc/view?usp=sharing

<?php 
session_start();
include('database_inc.php');


$TSLA_add = "API_TSLA.php";


$check_for_duplicate = mysqli_query($connect, "SELECT * from stock_users WHERE favorites = '$TSLA_add';");

if (mysqli_num_rows($check_for_duplicate) == 0) {

    $result = mysqli_query($connect,
    "INSERT INTO `stock_users` 
    (`favorites`) 
    VALUES ('$TSLA_add');");
    echo "Added. ";

    
} else {

    echo "This is already in the database.";

}




?>

 


I think you need a NEW table.

 

  1. name the table stock_user_picks
  2. first column is ID, int, auto-increment
  3. second column is user_id, int, null
  4. third column is stock_symbol, text, null
     

Then when a user adds a stock, their userid and stock symbol should be INSERTED into this table. Then your SQL should look something like this: 

$check_for_duplicate = mysqli_query($connect, "SELECT * from stock_user_picks WHERE stock_symbol = '$TSLA_add' AND user_id = '$logged_in_id';");

 

Login to reply

If you were logged in you could reply!