繁体   English   中英

如何通过用户操作从一个表中选择特定数据并将其一部分重新插入另一表

[英]How to select specific data from one table and insert a part of it back to another table by user action

我对PHP和MySQL很陌生。 无论如何,我目前正在尝试编写卡拉OK事件的应用程序。

活动的访问者应该能够在数据库中在线搜索他们想唱歌的特定歌曲,然后应该将这首歌曲添加到某种队列中。 然后可以将队列显示在屏幕上,以便访问者可以看到有多少人在唱歌,以及他们在唱歌等。

我的msql数据库非常简单。 该表称为“歌曲”,我有4列

id, title, artist, language

因此,我首先需要的是一种搜索表,人们可以在其中输入任何种类的搜索词,然后取回匹配的歌曲。 他们搜索艺术家或歌曲的天气无关紧要,因此他们也可以输入“ Love”和“ Beatles”以获取匹配结果。

因此,例如,如果搜索项是“ love”,则访问者将获得一张包含100行歌曲的表格,标题中包含“ love”。

我终于完成了这项工作,这是到目前为止的代码

<form action="searchform.php" method="get"   > 
 <input type="text" name="searchquery" dir="ltr">
  <input type="submit" value="start search">
  </form>

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'inc/db.php';
$daten = array();

if(isset($_GET ['searchquery']) && $_GET['searchquery'] !== ' ') 
    { 
    $searchquery= trim($_GET['searchquery']);
    echo "<p>Your search was: <b>". $searchquery ."</b></p>";
    $search_for = "%$searchquery%";
    $songsearch=$db->prepare("SELECT title,artist FROM songs WHERE artist LIKE ? OR title LIKE ?");
    $songsearch->bind_param('ss', $search_for, $search_for);
    $songsearch->execute();
    $songsearch->bind_result($showtitle, $showartist);
    echo "<table><tr><th>Title</th><th>Artist</th></tr>";
    while ($songsearch->fetch()) {
  echo "<tr><td>$showtitle</td>";
    echo "<td>$showartist</td></tr>";
}
echo "</table>";
}
?>

INC / db.php中

<?php
error_reporting(E_ALL);
$db = new mysqli('localhost', 'root', 'root', 'songlist');
$db->set_charset('utf8');
?>

现在,我希望访问者能够单击返回列表(表)中的一首歌名,然后单击此键,我希望将这首歌添加到队列中。 我的想法是仅将具有ID,title,artist列的另一个表添加到名为“ queue”的数据库中,并使用“ INSERT”命令将所选歌曲添加到该表中。

像这样

$addtoqueue= $db->prepare("INSERT INTO queue (title, artist) VALUES (?, ?)");

将歌曲添加到队列后,它应该转发到“ queue.php”,该队列显示队列表中的数据并每30秒左右刷新一次。

因此,我可能需要围绕

echo "<tr><td>$showtitle</td>";

行......像

<tr><td><a href="function to add dataset to the queue">$showtitle</a></td>

但是我不知道如何“选择”访问者单击的特定数据集,以便将该数据集的所有列插入“队列”表。

ps我现在尝试了以下操作,但是我在此出现错误,而不是预期的结果

$queuedata = array();
if (isset($_GET['action']) and $_GET['action']=='queue') {
    if ( isset($_GET['id']) ) {
    $id_fetch = (INT) $_GET['id'];
    if ($id_fetch > 0)
    {   
        $dsfetch=$db->prepare("SELECT id, title, artist FROM songs WHERE id = ?");
    $dsfetch->bind_param('iss', $id, $title, $artist);
        $queuedata[] = $dsfetch;
        }
}
$addqueue = $queuedata->prepare("INSERT INTO queue (id, title, artist) VALUES ('$id, $title, $artist')");
$addqueue->bind_param('sss', $id, $title, $artist); 
if ($addqueue->execute()) {
    header('Location: queue.php?action=feedback');
    die();
        }
    }
if (isset($_POST['action']) and $_POST['action']=='feedback') {
    echo '<p class="feedbacksuccess">Song successfully added to queue</p>';
}

和输出

echo "<tr><td><a href=\"?action=queue&id=$queuedata->id;\">$showtitle</td>";

我收到此错误:

错误:注意:试图在第55行的C:\\ xampp \\ htdocs \\ karaoke-files \\ suchtest2.php中获取非对象的属性

我也是编程的新手,但我会尽力提供帮助。

如果我理解您需要进行选择,然后将信息发送到php脚本。 据我了解,问题是php是服务器端语言。 您需要做一些事情才能在事情的前端进行工作。 因此,请查找一些javascrpt。

您可以将bundle.js包含到index文件中,并且它具有选择项目的功能。

我将提供一些小的示例代码,您可以在网上轻松找到它们。

例如。 使用javascript选择所需的内容。 然后您就可以在前端进行工作了。

<button id="1" onClick="reply_click(this.id)">B1</button>
<button id="2" onClick="reply_click(this.id)">B2</button>
<button id="3" onClick="reply_click(this.id)">B3</button>

<script type="text/javascript">
function reply_click(clicked_id)
{
    alert(clicked_id);
}
</script>`

使用javascript,您还可以调用php脚本。 阅读此文章从javascript调用php函数

或者您可以简单地使用html form 您可以使用echo使用php填充它。 action您可以指定要将数据发送到的PHP。

<form action="action_page.php">
  First name:<br>
  <input type="text" name="firstname" value="Mickey">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse">
  <br><br>
  <input type="submit" value="Submit">
</form>

在这种情况下,您要调用的php脚本action_page.php可以像这样接收您的输入: $firstName = $_REQUEST['firstname'];

并且,例如,如果您想填充form's下拉框并填充数据,则可以阅读此文章,从PHP的mySQL表填充下拉框

答案很长,希望对您有所帮助。

我终于找到了解决方案

if (isset($_GET['action']) and $_GET['action']=='queue') {
    if ( isset($_GET['id']) ) {
    $id_fetch = (INT) $_GET['id'];
    if ($id_fetch > 0)
    {   
$dsfetch=$db->prepare("SELECT id, title, artist FROM songs WHERE id = ?");
    $dsfetch->bind_param('i', $id_fetch);
    $dsfetch->execute();
    $dsfetch->bind_result($id, $title, $artist);
    while ($dsfetch->fetch()) {
            echo $id . ' / '. $title .' '. $artist;
        }
    $addqueue = $db->prepare("INSERT INTO queue (id, title, artist, user) VALUES (?, ?, ?, ?)");
    $addqueue->bind_param('isss', $id, $title, $artist, $name); 
    if ($addqueue->execute()) {
    header('Location: index.php?aktion=feedback');
    die();
        }
    }
}   
}
if (isset($_GET['aktion']) and $_GET['aktion']=='feedback') {
    echo '<p class="feedbacksuccess">Song has been added to queue</p>';
}

然后在输出中

echo "<tr><td><a href=\"index.php?aktion=queue&id=$showid\">$showtitle</a></td>";

不知道这是否是“好代码”,但它有效:-)

感谢所有提示

暂无
暂无

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

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