生气的php / mysql脚本

时间:2015-02-19 15:08:28

标签: php mysql

好的抱歉,早些时候喋喋不休这是我的完整代码

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";
echo "<p>Query<p>";

// Query Gets Tag IDs where the Categroy and Tag Alias is the same
$TagID = "SELECT at6ln_discuss_tags.id, at6ln_discuss_tags.title
        FROM at6ln_discuss_tags 
        INNER JOIN at6ln_discuss_category ON at6ln_discuss_tags.alias = at6ln_discuss_category.alias";
$resultTagID = $conn->query($TagID);
//Query Gets Posts IDs where the posts Categroy ID and Category ID are the same
$PostID = "SELECT at6ln_discuss_posts.id
        FROM at6ln_discuss_posts 
        INNER JOIN at6ln_discuss_category ON at6ln_discuss_posts.category_id = at6ln_discuss_category.id";
$resultPostID= $conn->query($PostID);
//Query Gets catergory ID where the Categroy and Tag Alias is the same
$CategoryID = "SELECT at6ln_discuss_category.id
        FROM at6ln_discuss_category 
        INNER JOIN at6ln_discuss_tags ON at6ln_discuss_category.alias = at6ln_discuss_tags.alias";
$resultCategoryID= $conn->query($CategoryID);
//Query Gets Posts catergory ID where the Categroy and Tag Alias is the same
$PostsCategoryID = "SELECT at6ln_discuss_posts.category_id, at6ln_discuss_category.alias
        FROM at6ln_discuss_posts, at6ln_discuss_category
        WHERE (at6ln_discuss_posts.category_id = at6ln_discuss_category.id)";
$resultPostsCategoryID= $conn->query($PostsCategoryID);
//Query Gets catergory Alais where the Categroy and Tag Alias is the same
$PostsCategoryAlias = "SELECT at6ln_discuss_category.alias
        FROM at6ln_discuss_category
        INNER JOIN at6ln_discuss_tags ON at6ln_discuss_category.alias = at6ln_discuss_tags.alias";
$resultPostsCategoryAlias= $conn->query($PostsCategoryAlias);
$sql= "INSERT INTO at6ln_discuss_posts_tags (post_id,tag_id) VALUES ($idposts,$TagID)";
if ($resultTagID->num_rows > 0) {
echo "<table>";
echo "<tr>";
echo "<th> Post ID </th>";
echo "<th> Tag ID </th>";
echo "<th> Post Catergory ID </th>";
echo "</tr>";
 // output data of each row
 while($posts = $resultPostID->fetch_assoc() ) {
     // output data of each row
     $category = $resultCategoryID->fetch_assoc();
     $postscatID = $resultPostsCategoryID->fetch_assoc();
     // output data of each row
     $idcategory = $category["id"];
     $idposts = $posts["id"];
     $idpostcat = $postscatID["category_id"];
     if ($idpostcat == 5){
        $TagID=10;
        query($sql);
            echo "<tr>";
            echo "<td>". $idposts ."</td>";
            echo "<td>". $TagID. "</td>";
            echo "<td>". $idpostcat ."</td>";
            echo "</tr>";
     }
     if ($idpostcat == 7){
        $TagID=12;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 13){
        $TagID=17;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 14){
        $TagID=18;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 15){
        $TagID=19;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 16){
        $TagID=20;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 27){
        $TagID=29;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 28){
        $TagID=30;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 30){
        $TagID=32;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 34){
        $TagID=35;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 35){
        $TagID=36;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 37){
        $TagID=38;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 38){
        $TagID=39;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 49){
        $TagID=47;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 50){
        $TagID=48;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 52){
        $TagID=50;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 53){
        $TagID=51;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
     if ($idpostcat == 55){
        $TagID=46;
        query($sql);
        echo "<tr>";
        echo "<td>". $idposts ."</td>";
        echo "<td>". $TagID. "</td>";
        echo "<td>". $idpostcat ."</td>";
        echo "</tr>";
     }
 }
echo "</table>";} else {
 echo "0 results";}

最有可能是一种非常简单的方法,但它基本上是我的第一个脚本。

我没有得到任何错误,这是我的概率,它得到的同时创建表得到第二个如果然后什么都不做

如果我删除了$sql查询,那么该表工作正常,因此我的INSERT中出现了一些错误,我无法弄清楚

编辑:我得到的错误是:

  

未定义的变量:第50行的/Applications/MAMP/htdocs/DEV/Technet-21/scripts/tags-categorys.php中的idposts

     

致命错误:在第69行的/Applications/MAMP/htdocs/DEV/Technet-21/scripts/tags-categorys.php中调用未定义的函数query()

1 个答案:

答案 0 :(得分:1)

我已经重写了代码的相关部分。您在几个地方遇到了麻烦。第一个错误是因为您将变量直接插入到插入查询中。

在任何情况下,这都是非常糟糕的主意,因为它会让您对sql注入开放。您永远不应该信任用户提供的输入。有关详细信息,请查看How can I prevent SQL injection in PHP?

它也会导致您出现问题,因为您需要变量的未来值,而不是当前值,这是插入变量时插入的内容。

第二个错误是因为您正在使用query(),这是对未定义函数的函数调用。由于它实际上是$conn对象的一种方法,因此您需要使用$conn->query()

您的代码存在的另一个问题是大量复制/粘贴。您可以使用if / switch构造 - http://php.net/manual/en/control-structures.switch.php

来减少此问题并删除大量case语句

以下是代码的修改版本:

$sql = 'INSERT INTO at6ln_discuss_posts_tags (post_id,tag_id) VALUES (?,?)';
$stmt = $conn->prepare($sql);
if ($resultTagID->num_rows > 0) {
    echo "<table>";
    echo "<tr>";
    echo "<th> Post ID </th>";
    echo "<th> Tag ID </th>";
    echo "<th> Post Catergory ID </th>";
    echo "</tr>";
    // output data of each row
    while ($posts = $resultPostID->fetch_assoc()) {
        // output data of each row
        $category = $resultCategoryID->fetch_assoc();
        $postscatID = $resultPostsCategoryID->fetch_assoc();
        // output data of each row
        $idcategory = $category["id"];
        $idposts = $posts["id"];
        $idpostcat = $postscatID["category_id"];

        switch($idpostcat) {
            case 5:
                $TagID = 10;
                break;
            case 7:
                $TagID = 12;
                break;
            case 13:
                $TagID = 17;
                break;
            case 14:
                $TagID = 18;
                break;
            case 15:
                $TagID = 19;
                break;
            case 16:
                $TagID = 20;
                break;
            case 27:
                $TagID = 29;
                break;
            case 28:
                $TagID = 30;
                break;
            case 30:
                $TagID = 32;
                break;
            case 34:
                $TagID = 35;
                break;
            case 35:
                $TagID = 36;
                break;
            case 37:
                $TagID = 38;
                break;
            case 38:
                $TagID = 39;
                break;
            case 49:
                $TagID = 47;
                break;
            case 50:
                $TagID = 48;
                break;
            case 52:
                $TagID = 50;
                break;
            case 53:
                $TagID = 51;
                break;
            case 55:
                $TagID = 46;
                break;

        }
        $stmt->bind_param('ii', $idposts, $TagID);
        $stmt->execute();
        echo "<tr>";
        echo "<td>" . $idposts . "</td>";
        echo "<td>" . $TagID . "</td>";
        echo "<td>" . $idpostcat . "</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}

此代码使用带有绑定参数的预准备语句来安全地插入数据。您可以在http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

了解更多信息