php - 在 PDO 语句中将相同的 id 分配给 2 个 MySQL 表行

标签 php mysql pdo

我无法思考如何做到这一点。我刚刚开始了解PDO,所以请原谅我的无知。我有一个表单,只需将一些帖子数据和图像数据插入 MySQL 数据库中。我有一个单独的表用于发布数据和图像数据。它们都有一个名为 postid 的行。帖子 postid 当前设置为 AUTO_INCRMENT 我只需要在图像和帖子表中使用相同的 postid 即可。然而,事情并不那么简单。图像并不总是添加到每个帖子中。因此,仅仅让两者同时递增是不够的。图像 postid 只需始终匹配相应的帖子 postid

我知道这不是一个关于代码问题的非常具体的问题,但希望我至少能得到一些好的见解。

除了将 postid 添加到 images 表之外,这是我当前正常运行的代码。

<!DOCTYPE html>
<html>
<body>
<center>
  <form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
    <div><textarea name="entry" maxlength="600"></textarea></div>
    <div><input name="userfile" type="file" /></div>
    <div><input type="submit" value="Submit" /></div>
  </form>

</body></html>


<?php
/*** check if a file was submitted ***/
if(!isset($_FILES['userfile']))
    {
    echo '<p>Please select a file</p>';
    }
else
    {
    try    {
        upload();
        /*** give praise and thanks to the php gods ***/
        echo '<p>Thank you for submitting</p>';
        }
    catch(Exception $e)
        {
        echo '<h4>'.$e->getMessage().'</h4>';
        }
    }
?>

<?php
//Upload function
function upload(){

/*** check if a file was uploaded ***/
if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false)
    {
    /***  get the image info. ***/
    $size = getimagesize($_FILES['userfile']['tmp_name']);
    /*** assign our variables ***/
    $type = $size['mime'];
    $imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb');
    $size = $size[3];
    $name = $_FILES['userfile']['name'];
    $maxsize = 99999999;
    $poster = $_SESSION['username'];
    $entry = $_POST['entry'];


    /***  check the file is less than the maximum file size ***/
    if($_FILES['userfile']['size'] < $maxsize )
        {
        /*** connect to db ***/
        $dbh = new PDO("mysql:host=localhost;dbname=db_name", 'user', 'password');

                /*** set the error mode ***/
                $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            /***sql query ***/
        $stmt = $dbh->prepare("INSERT INTO images (image_type ,image, image_size, image_name) VALUES (? ,?, ?, ?)");
        $stmt2 = $dbh->prepare("INSERT INTO posts (poster, entry) VALUES (? ,?)");

        /*** bind the params ***/
        $stmt->bindParam(1, $type);
        $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
        $stmt->bindParam(3, $size);
        $stmt->bindParam(4, $name);
        $stmt2->bindParam(1, $poster);
        $stmt2->bindParam(2, $entry);

        /*** execute the query ***/
        $stmt->execute();
        $stmt2->execute();

        }
    else
        {
        /*** throw an exception is image is not of type ***/
        throw new Exception("File Size Error");
        }
    }
else
    {
    // if the file is not less than the maximum allowed, print an error
    throw new Exception("Unsupported Image Format and/or to large of a file.");
    }
}
?>

最佳答案

  1. 执行插入帖子的查询,这会创建自动增量 ID。
  2. 使用 $dbh->lastInsertId() 获取这个新 ID。
  3. 准备图像查询并使用您在第 2 步中获得的 ID。
$stmt = $dbh->prepare("INSERT INTO posts (poster, entry) VALUES (?, ?)");
...
$stmt->execute();
$id = $dbh->lastInsertId();

$stmt = $dbh->prepare("INSERT INTO images (post_id, image_type, image, image_size, image_name) VALUES (?, ? ,?, ?, ?)");
$stmt->bindParam(1, $id);
...
$stmt->execute();

关于php - 在 PDO 语句中将相同的 id 分配给 2 个 MySQL 表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22412711/

相关文章:

java - 带有参数名和值的分组依据的 JPA 命名查询

mysql - 什么数据存储模型用于存储维基百科中的文章

php - HTML 表单不通过 PHP 处理器将数据发布到 MySQL 数据库

php - 登录 php 和 mysqli

php - Ajax请求并使用php pdo复制一些行但更改一些值

php - 使用 PDO 插入时来自另一个表的 ID

java - PHP/Java 套接字 - 奇怪的错误?

php - 如何根据给定条件选择最短的GPS坐标?

PHP 表单图像上传有效,但文本输入未存储在数据库中

php - 插入的数据没有显示在 phpMyAdmin 的表中