检查数据库中的存在值

时间:2016-04-29 12:18:30

标签: php mysql

我的桌面艺术家:artistID,name,genreID,avatar,short-decs,view,vnmartist。 artist structure table 我创建upload.php来上传mp3并插入有关歌曲的信息,包括列:name,artistID,genreID。

因为歌曲名称和艺术家,类型被用户输入。我想检查艺术家的名字。如果name为null - >将名称插入数据库,然后获取artistID并插入数据库。如果name不为null - >获取artistID并向表艺术家插入数据。

我的PHP代码如下:

$artist = $_POST['artist-name'];
$link = mysql_connect("localhost", "root", "");
mysql_select_db("vnmtest", $link);

$result = mysql_query("SELECT * FROM artist where name='".$artist."'",$link);
if($num_rows = mysql_num_rows($result) == 0)

echo "data is null";

else echo "data okay";

在测试上传1文件时,我输入了艺术家姓名:“NhậtTrường”,这就像艺术家名字中的数据,但程序仍然回显“数据为空”。

我的艺术家表:

[![我的艺术家表] [2]] [2]

我的完整代码如下:

  

        if (!empty($_FILES["audiofile"])) {
        $myFile = $_FILES["audiofile"];

        if ($myFile["error"] !== UPLOAD_ERR_OK) {
            echo "<p>An error occurred.</p>";
            exit;
        }

        // ensure a safe filename
        $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

        // don't overwrite an existing file
        $i = 0;
        $parts = pathinfo($name);
        while (file_exists(UPLOAD_DIR . $name)) {
            $i++;
            $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
        }

        // preserve file from temporary directory
        $success = move_uploaded_file($myFile["tmp_name"],
            UPLOAD_DIR . $name);
        if (!$success) { 
            echo "<p>Unable to save file.</p>";
            exit;
        }

        // set proper permissions on the new file
        chmod(UPLOAD_DIR . $name, 0644);
        $name = $_POST['song-name'];
        $artist = $_POST['artist-name'];
        $genreID = $_POST['genreID']; 

        // kiểm tra artist tồn tại hay chưa
        $artist = $_POST['artist-name'];
        print_r($_POST['artist-name']);
        //$sql = "SELECT * FROM artist where name='".$artist."'";
        //$result = $conn->query($sql);




            $link = mysql_connect("localhost", "root", "");
            mysql_select_db("vnmtest", $link);

            $result = mysql_query("SELECT * FROM artist where name='".$artist."'",$link);
            if($num_rows = mysql_num_rows($result) == 0)

            echo "data is null";

            else echo "data okay";



        // output data of each row

        // $artist_select = $conn->query ("SELECT EXISTS(select * from artist where name = '".$artist."') ");

    }

&GT;

1 个答案:

答案 0 :(得分:0)

我认为可能有三种可能性:

1)字符集错误:尝试在页面中设置适当的字符集(例如:设置标题:$mysqli->query("SET CHARACTER SET utf8")

2)查询错误:尝试在

中更改您的查询
$result = mysql_query("SELECT count(*) num FROM artist where name='".$artist."'",$link);

并修改你的if语句以检查&#34; num&#34; <场> 0

3)&#34;如果&#34;错误:尝试更改if(保留当前查询)

if(mysql_num_rows($result) == 0)