如何复制mySQL表行但插入一个自定义值?

时间:2018-02-01 16:13:32

标签: php mysql pdo

使用此代码,我复制了我的表格行:

  <Container fluid className="full-height bg-light">
      <Row className="h-100 justify-content-center full-height align-items-center bg-light">
        <Col xs="10" lg="3" className="p-0">
          <Card>
            <CardBody>
              <CardTitle>LOGIN</CardTitle>
              <CardText>Sign in with your account.</CardText>
              <InputGroup className="mb-3">
                <div className="input-group-prepend">
                  <span className="input-group-text">
                    <i className="icon-user"></i>
                  </span>
                </div>
                <Input type="text" placeholder="Username"/>
              </InputGroup>
              <InputGroup className="mb-4">
                <div className="input-group-prepend">
                  <span className="input-group-text">
                    <i className="icon-lock"></i>
                  </span>
                </div>
                <Input type="password" placeholder="Password"/>
              </InputGroup>
              <Row>
                <Col xs="12" lg="6">
                  <Button color="primary" className="px-4">Login</Button>
                </Col>
                <Col xs="12" lg="6" className="text-right">
                  <Button color="link" className="px-0">Forgot password?</Button>
                </Col>
              </Row>
            </CardBody>
          </Card>
        </Col>
        <Col xs="10" lg="3" className="p-0">
          <Card color="primary">
            <CardBody className="text-white">
              <CardTitle>CREATE ACCOUNT</CardTitle>
              <CardText>If you want to create an account for your company to start using this product, click on the Create button. If your organization already has an account and you'd like to join it, click on the "Join" button.</CardText>
              <Row>
                <Col xs="12" md="6">
                  <Button color="success" className="px-4">Create</Button>
                </Col>
                <Col xs="12" md="6" className="text-right">
                  <Button color="success" className="px-4">Join</Button>
                </Col>
              </Row>
            </CardBody>
          </Card>
        </Col>
      </Row>
    </Container>

它运行良好,但我需要通过新插入一个自定义值来放置 $old_name = "Laura"; $new_name = "Sam"; $pdo = $db->prepare('INSERT INTO animals (status, name) SELECT status, name FROM animals WHERE name = :name'); $pdo->execute(array( ':name' => $old_name, )); 的位置。

这是我的方法:

name

错误消息是:

  

致命错误:未捕获PDOException:SQLSTATE [42000]:语法错误或   访问冲突:1064您的SQL语法错误

1 个答案:

答案 0 :(得分:3)

您不需要VALUES() INSERT SELECT,并且列名可能有些含糊不清,因为您同时选择并插入同一个表 - 因此您应该为其中一个它们。

此外,由于您在:name占位符上输入了不同的值,因此您需要两个不同的占位符。

这可能应该这样做:

$old_name = "Laura";
$new_name = "Sam";

$pdo = $db->prepare('INSERT INTO animals(animals.status, animals.name) SELECT a.status, :new_name FROM animals AS a WHERE a.name = :old_name');
$pdo->execute(array(
    ':old_name' => $old_name,
    ':new_name' => $new_name,
));