如何将登录用户的“添加到收藏夹”活动连接到 pg admin 4 数据库

首先,我对 PHP 和数据库完全陌生。我有一个关于创建网站的问题,我在其中创建了一个用户可以登录并与之交互的数据库。更具体地说,我创建了一个音乐商店网站,用户可以登录,然后他们选择自己喜欢的磁盘,并使用同名按钮将它们添加到“收藏夹列表”中。到目前为止,在我的代码下方,仅用于收藏夹部分

    <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>

接下来是我的 PHP 部分代码,

<?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);
?>

我的问题是我找不到附加/关联登录用户输入的方法 到数据库。我必须创建一个通信渠道,将已登录的用户与他在网站中所做的操作连接起来,并存储在我的数据库中。我错过了什么???

yueling195712 回答:如何将登录用户的“添加到收藏夹”活动连接到 pg admin 4 数据库

我注意到您的表单输入有误。我相信你的 PHP 代码不会从 $_POST['favorite'] 中得到任何东西。

正确的表单代码如下:


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

如您所见,我所做的是从您的 name="favourite" 中删除 form。相反,我将它移到您的表单中并使其成为 text input。默认情况下,该值为 1。提交表单后,$_POST['favorite'] 的变量将变为 1。

本文链接:https://www.f2er.com/8762.html

大家都在问