简体   繁体   English

使用mysqli_fetch_assoc的数据库表检索项目的“添加到收藏夹”功能

[英]“Add to favorites” functionality for database table retrieved items using mysqli_fetch_assoc

I'm populating some table rows from my database as html table rows using the following php example(for summarizing the code, I haven't included the mysql connection) 我使用以下php示例将数据库中的某些表行填充为html表行(为汇总代码,我未包括mysql连接)

Populating results: 填充结果:

  $conn = //connected to db successfully
  <?php
  $sql = "SELECT * FROM my_table";
  $rs = mysqli_query($conn,$sql);
  $rows = mysqli_fetch_assoc($rs);
  ?>
  <table>
  do { ?>
  <tr>
  <td><?php echo $rows['column1']; ?></td><td><?php echo $rows['column2']; ?></td>
  <td><button type="button" onclick="addToFav();">Add to my favorites</button></td>
  </tr>
  <?php }while($rows = mysqli_fetch_assoc($rs)); ?>
  </table>

According to above query in which I've retrieved information with several columns(I've used two columns in example above), if a user clicks on the "Add to my favorites" I want the item to be added to the logged in user favorites so the user can see that in the profile page. 根据上述查询,我​​检索了几列信息(在上面的示例中使用了两列),如果用户单击“添加到我的收藏夹”,我希望将该项目添加到已登录的用户中收藏夹,以便用户可以在个人资料页面中看到。 How should I insert the item information to e,g 'favorites' table according to that I'm using 'mysqli_fetch_assoc' solution for retrieving information from 'items_table'. 根据我使用“ mysqli_fetch_assoc”解决方案从“ items_table”中检索信息的方式,应该如何将商品信息插入到“ favorites”表中。 Besides, I would like to do the action(adding item information to favorites table)by AJAX. 此外,我想通过AJAX进行操作(将项目信息添加到“收藏夹”表中)。

I appreciate your guys helping me with the solution for inserting each item information(including several columns) in addition to the logged in user 'id' to 'favorites' table so it can be displayed correctly for each user profile. 感谢您为已登录用户“ id”到“收藏夹”表中插入每个项目信息(包括几列)的解决方案提供帮助,以便为每个用户个人资料正确显示该解决方案。 I really appreciate if you could also provide a solution for "addToFav()" function for this case as well since I'm pretty new to AJAX. 如果您也可以为这种情况提供“ addToFav()”函数的解决方案,我将非常感谢,因为我对AJAX还是很陌生。 Sorry if the question got a little broad. 抱歉,这个问题有点广泛。 Thanks in advance for your guidance. 在此先感谢您的指导。

    <?php
    $conn = //connected to db successfully
    $sql = "SELECT * FROM my_table";
    $results = mysqli_query($conn, $sql);
    ?>
    <table>
    <?php 
    /**
     * mysqli_fetch_assoc returns next row as an associative array if it exists
     * 
     * also, alternate syntax for control structures
     */
    while($row = mysqli_fetch_assoc($results)): 
    ?> 
    <tr>
        <td>
            <?php echo $row['column1']; ?>
        </td>
        <td>
            <?php echo $row['column2']; ?>
        </td>
        <td>
            <button type="button" onclick="addToFav();">Add to my favorites</button>
        </td>
    </tr>
    <?php endwhile; ?>
    </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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