INSERT INTO 不是在一张桌子上工作而是在其他桌子上工作并且更新也工作?

问题描述

我的 PHP/sql 脚本在我克隆的新表不能工作,但在我复制的表上工作得很好来自的新表:

SELECT * INTO slide FROM news

这是我的插入表单:

<div class="the-form" style="width:100%;">
<form class="userTrans" method="post">
<input type="hidden" name="act_userTrans" value="__insertNews_">
    <p>
        <label for="title">Title:</label>
        <input type="text" name="title" id="title">
    </p>
    <p>
        <label for="type">News Type:</label>
        <input type="text" name="type" id="type">
    </p>
    <p>
        <label for="autor">Author:</label>
        <input type="text" name="autor" id="autor">
    </p>

    <input type="text" name="text2" id="text2">
    <p class="form-footer">
        <button class="button userTrans" style="background-color: #DB6D1D;">Publish News</button>
    </p>
</form>
</div>

这是我的编辑表单:

<div class="the-form" style="width:100%;">
<form class="userTrans" method="post">
<input type="hidden" name="act_userTrans" value="__updateNews_">
    <p>
        <label for="title">Title:</label>
        <input type="text" name="title" id="title" value="<?=$edit[title]?>"/>
    </p>
    <p>
        <label for="type">News Type:</label>
        <input type="text" name="type" id="type" value="<?=$edit[type]?>"/>
    </p>
    <p>
        <label for="autor">Author:</label>
        <input type="text" name="autor" id="autor" value="<?=$edit[autor]?>"/>
    </p>

        <input type="text" name="text2" id="text2" value="">
        <input type="hidden" name="id" value="<?=$edit[id]?>">

    
    <p class="form-footer">
        <button class="button userTrans" style="background-color: #DB6D1D;">Publish News</button>
    </p>
</form>
</div>

这是我更新和插入两种不同表单的过程:

    if($activity == "__insertNews_")
    {
        $title = htmlspecialchars($_POST['title']);
        $autor = htmlspecialchars($_POST['autor']);
        $type = (int)$_POST['type'];
        if(empty($_POST['type']) || empty($_POST['autor']) || empty($_POST['title']) || empty($_POST['text2']))
        {
            echo response(0,'Fill up all the forms.',0);
            exit();
        }
        //echo $_POST[text2];
        $news = mssql_query("INSERT INTO DB1.dbo.news (title,text,type,autor) VALUES ('$title','$_POST[text2]','$type','$autor') ");
        echo response(1,'Publishing '.$title.' success!',0);
    }
    
    if($activity == "__updateNews_")
    {
        $title = htmlspecialchars($_POST['title']);
        $autor = htmlspecialchars($_POST['autor']);
        if(empty($_POST['autor']) || empty($_POST['title']))
        {
            echo response(0,0);
            exit();
        }
        $news = mssql_query("UPDATE DB1.dbo.news set title='$title',autor='$autor' WHERE id='$id'  ");
        echo response(1,'Editing '.$title.' success!',0);
    }

因此,使用上面的那些脚本,我能够插入和更新 dbo.news

上的任何内容

但是,当我将 DB1.dbo.news 更改为我创建的新表 (DB1.dbo.slide) 时,“插入”将不起作用。

我尝试使用相同的表单和处理脚本添加数据,“插入”在 dbo.slide 上不起作用,但是当我在 dbo.news 上测试它时,我能够插入数据。我还测试了 UPDATE,它对两者 dbo.slide 和 dbo.news 都有效。

现在我想知道,为什么 INSERT 的 SAME 脚本可以在其他表上运行,但在新表 (dbo.slide) 上不起作用。这确实令人困惑,因为我没有更改任何代码,我只是更改了要插入数据的表,而 INSERT 函数停止工作。

调试此问题并找出导致此问题的原因的最佳方法是什么?

解决方法

mssql_query 函数在较新的 php 版本中不再存在。我建议你使用 PDO,因为它对你的好处之一是,减少常用的插入和更新操作,并且它安全抵御恶意攻击和 SQL 注入 >

但是现在,如果没有 DB1.dbo.slide 列和 DB1.dbo.slide 列以及您的 DB1.dbo.slide 插入查询,您将无能为力。

因此,对于主要帮助,请检查 DB1.dbo.slide 列并将其与 DB1.dbo.news 进行比较。然后根据 DB1.dbo.slide 列重写您的 SQL 查询。

如果您的插入查询适用于 DB1.dbo.news,请确保您对 DB1.dbo.slide 的插入查询不正确

,

解决了!我删除了所有克隆的数据库并使用CREATE TABLE语句重新创建了所有内容,包括主键、约束等,现在可以正常工作了!

看来我不应该用它来克隆表

SELECT * INTO slide FROM news

感谢大家百忙之中抽空回复,真的很感谢!