1

First of all, I am totally new to PHP and databases in general. I have a question regarding the creation of a website, in which I've created a database that users can login into and interact with. To be more specific, I created a music store website that users can login into and then they choose their favorite disks and add them to their "Favorites List" by using a button of the same name. Below my code so far, only for the favorites portion

    <table>
      <tr>

        <td>
          <div class="container" style="height:150px; width: 250px;">
            <img src="images\ironmaiden2.jpg" style="height:160px; width:250px;" >
            <div class="middle">
              <div class="text2" id="1"><a href="https://www.youtube.com/watch?v=FhBnW7bZHEE" target="_blank" style="color:white;">The Writing and the Wall</a></div>
            </div>
          </div>
          <h4><a href="ironmaiden.html">Iron Maiden</a></h4>

          <form name="favorite" action="favorite.php" method="post">
            <input type="submit" name="submit" value="Add to Favorites">
          </form>

        </td>

and next my code for the PHP portion,

<?php

//header("Location: login.php");

require_once 'credent.php'; 

$connecionstr="host=".DB_SERVER." port=5432 dbname=".DB_BASE."
user=".DB_USER." password=".DB_PASS." options='--client_encoding=UTF8'"; 

$dbconn = pg_connect($connecionstr);

if (!$dbconn) {
    die("Connection failed: " . pg_connect_error());
}




if(isset($_POST["submit"]))
{
    if(isset($_POST['favorite'])){
    $favorite=$_POST['favorite'];
    $sql=pg_query("insert into favorite(id) values('$favorite')");
}  
}


pg_close($dbconn);
?>

My problem is that I can't find a way to attach / correlate the logged in user input to the database. I have to create a communication channel that connects the user that has logged in, with the actions he makes in the website, stored in my database. What am I missing???

Vel_More
  • 293
  • 1
  • 10

1 Answers1

0

I've notice your form input is wrong. I believe in your PHP code you got nothing from $_POST['favorite'].

The correct code for form is like:


<form action="favorite.php" method="post">
    <input type="text" name="favorite" value="1">
    <input type="submit" name="submit" value="Add to Favorites">
</form>

As you can see what i did is remove name="favourite" from your form. Instead, i move it inside your form and make it as a text input. By default the value is 1. Once the form is submit, the variable of $_POST['favorite'] will become 1.

Kah Fung
  • 41
  • 1
  • 4