我在如何进行此操作时遇到了麻烦。我刚刚开始学习有关PDO的知识,因此请原谅。我有一个简单地将一些发布数据和图像数据插入MySQL数据库的表单。我有一个单独的表格用于发布数据和图像数据。它们都具有称为postid的行。当前将postid帖子设置为AUTO_INCREMENT我只需要将相同的postid放入图像和帖子表即可。但是,它并不是那么简单。图像不会总是添加到每个帖子中。因此,仅在两个元素上同时使用AUTO_INCREMENT来同时增加就不够了。图片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.");
    }
}
?>

最佳答案

执行插入帖子的查询,该帖子创建自动增量ID。
使用$dbh->lastInsertId()获取此新ID。
准备图像查询并使用您在步骤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语句中为2个MySQL表行分配相同的ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22412711/

10-12 18:06