duancong7573 2014-07-19 02:28
浏览 59

使用函数在mysql中插入2个查询

Currently I am trying to write a single function in my php script that adds a new page into my database and updates the navigation table at the same time. For some reason both queries are failing, and I'm not sure what else I should do. This is my add_content function call:

elseif ($react == "add_content") {
    echo 'add content';
    $title = $_POST['title'];
    $content = $_POST['content'];
    $page = $_POST['page'];
    if (!empty($title) && !empty($content) && !empty($page)) {
        addcontent($title, $content, $page);
    } else {
    echo 'not enough info';
    }

and this is my function:

function addcontent($title, $content, $page) {
global $db;
$query1 = "INSERT INTO `content`(`title`, `content`) VALUES (:title, :content)";
$query2 = "INSERT INTO `navigation` ('page', 'content_id') SELECT :page, c.content_id  FROM content c WHERE title=:page;";
try {
    $stat = $db->prepare($query1);
    $stmt = $db->prepare($query2);
    $stat->bindParam(':title', $title);
    $stat->bindParam(':content', $content);
    $stat->bindParam(':page', $page);
    $stat->execute($query1);
    $stmt->execute($query2);
    $result = "<strong>$title</strong> added<p>";
} catch (Exception $ex) {
    $result = "$title deltetion failed";
}

}

I know the two queries work because I've already tested them with dummy information, it's just getting them to work inside of this function that's causing the problem.

I'm not sure if this is needed, but just in case, this is my table to add content:

<form method="POST" action="index.php">
            <input type="hidden" name="react" value="add_content">
            <input type="hidden" name="page|title" value="admin">
            <div align="center"><center><p>ADD A New Page<br>
             Title: <input type="text" name="title|page" size="20"><br>
             Content: <input type="text" name="content" size="20"><br>
             <input type="submit" value="Submit"></p></center></div>
        </form>
  • 写回答

1条回答 默认 最新

  • douyin8809 2014-07-19 02:33
    关注
    $query2 = "INSERT INTO `navigation` ('page', 'content_id') 
    SELECT :page, c.content_id  FROM content c WHERE title=:page;";
           ^ column name marker
    

    You can't use markers for column names. From the docs

    The markers are legal only in certain places in SQL statements. For example, they are allowed in the VALUES() list of an INSERT statement (to specify column values for a row), or in a comparison with a column in a WHERE clause to specify a comparison value. However, they are not allowed for identifiers (such as table or column names), in the select list that names the columns to be returned by a SELECT statement), or to specify both operands of a binary operator such as the = equal sign. The latter restriction is necessary because it would be impossible to determine the parameter type. In general, parameters are legal only in Data Manipulation Language (DML) statements, and not in Data Definition Language (DDL) statements.

    If you're trying to select the constant $page you can select c.title because your where condition guarantees that it will be equal to $page.

    $query2 = "INSERT INTO `navigation` ('page', 'content_id') 
    SELECT c.title, c.content_id  FROM content c WHERE title=:page;";
    

    Also, you're binding $page to the wrong statement

    $stat->bindParam(':page', $page);
    

    Change it to

    $stmt->bindParam(':page', $page);
       ^
    
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系