0

似乎我在问一个简单的问题,但我找不到适合我的答案。我希望你能帮忙!

我正在编写一个 php 脚本,该脚本从 html 文件中获取一些表单数据(name, artist, song)并将其放入带有 columns 的表中(name, artist, song, queue, newcomer)。现在,该列newcomer是一个布尔值;如果传入表单数据的名称值是唯一的,我希望这个布尔值从默认的 false 或 0 更改为 true 或 1。

这是我的尝试:

$newcomer = mysql_query(
                'IF NOT EXISTS (SELECT DISTINCT name FROM tbl_queue) 
                 THEN UPDATE tbl_queue newcomer=1'
            );

但显然,它不起作用......

提前致谢!!


您好,感谢您的回答!但是它仍然对我不起作用=(这是来自php的代码:

$newcomer = mysql_query("UPDATE tbl_queue SET newcomer=1 WHERE name='" . $_POST[name] ."' AND NOT EXISTS (SELECT DISTINCT name FROM tbl_queue WHERE name='" . $_POST[name] ."') ");

4

4 回答 4

1
"UPDATE tbl_queue SET newcomer=1
    WHERE   name = '" . $name_variable . "' AND
            NOT EXISTS (SELECT DISTINCT name
                            FROM tbl_queue
                            WHERE name='" . $name_variable . "')"

那是你想要做的吗?显然用正确的变量替换 $name_variable 。

编辑:重新格式化的 SQL

于 2013-05-07T12:51:39.477 回答
0

我很想像这样进行初始插入:-

INSERT INTO tbl_queue (name, artist, song, queue, newcomer)
SELECT DISTINCT a.name, a.artist, a.song, a.queue, CASE WHEN b.newcomer IS NULL THEN 0 ELSE 1 END as newcomer
FROM (SELECT '$name' AS name, '$artist' AS artist, '$song' AS song, '$queue' AS queue) a
LEFT OUTER JOIN tbl_queue b
ON a.name = b.name

对要插入的值进行虚拟选择,然后根据名称将其与现有表连接起来,使用 CASE 语句检查是否找到匹配的行。

编辑 - 下面是一些显示 var 分配和转义值的 php。我在这里使用了 mysql_ 函数,就像您在原始帖子中一样,但实际上应该使用 mysqli_ 函数来证明未来的事情。

<?php

$name = mysql_real_escape_string($_POST['name']);
$artist = mysql_real_escape_string($_POST['artist']);
$song = mysql_real_escape_string($_POST['song']);
$fcfs_queue = mysql_real_escape_string($_POST['fcfs_queue']);

$sql = ("INSERT INTO tbl_queue (name, artist, song, fcfs_queue, newcomer)
SELECT DISTINCT a.name, a.artist, a.song, a.fcfs_queue, CASE WHEN b.newcomer IS NULL  THEN 0 ELSE 1 END as newcomer
FROM (SELECT '$name' AS name, '$artist' AS artist, '$song' AS song, '$fcfs_queue' AS  fcfs_queue) a
LEFT OUTER JOIN tbl_queue b
ON a.name = b.name");


if (!mysql_query($sql))
{
    die('Error: ' . mysql_error());
}
echo "success";


?>
于 2013-05-07T13:06:07.363 回答
0

我希望这是对 Kickstart 的评论,但它不符合评论的范围,所以.. 我希望我在这里回答可以吗?

谢谢!然而这也不起作用......它返回这个错误:警告:mysql_error()期望参数1是资源(关于这一行:die('Error:'。mysql_error($sql));在if语句中)

我的代码是:

$sql = ("INSERT INTO tbl_queue (name, artist, song, fcfs_queue, newcomer)
SELECT DISTINCT a.name, a.artist, a.song, a.fcfs_queue, CASE WHEN b.newcomer IS NULL  THEN 0 ELSE 1 END as newcomer
FROM SELECT '$name' AS name, '$artist' AS artist, '$song' AS song, '$fcfs_queue' AS  fcfs_queue) a
LEFT OUTER JOIN tbl_queue b
ON a.name = b.name");


if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error($sql));
  }
echo "success";
于 2013-05-10T08:11:18.743 回答
0

我会为此使用触发器,因为它是您的表的“固定”要求:

DROP TRIGGER IF EXISTS `newComerCheck`//
CREATE TRIGGER `newComerCheck` BEFORE INSERT ON `songs`
 FOR EACH ROW BEGIN
    DECLARE c integer;
    SELECT COUNT(name) FROM `songs` WHERE `name`=NEW.name INTO c;

    IF (c >= 1) THEN
      SET NEW.newcomer=0;
    ELSE 
      SET NEW.newcomer=1;
    END IF;
  END
//

见小提琴: http ://sqlfiddle.com/#!2/3e73b5/1

于 2013-05-10T08:29:17.957 回答