I am having trouble wrapping my brain around how I would do this. I just started learning about about PDO, so forgive the ignorance. I have a form that simply inserts some post data and image data into a MySQL database. I have a separate table for post data and image data. They both have a row called postid
. The posts postid
currently is set to AUTO_INCREMENT
I just simply need to have the same postid
go into both images and posts tables. However, it is not quite that simple. An image will not always be added to every post. So simply having AUTO_INCREMENT
on both to increment simultaneously would not suffice. The images postid
just needs to always match the corresponding posts postid
I know this isn't a very specific question regarding a code issue, but was hoping i could get some good insight at least.
This is the code I currently have functioning correctly aside from adding postid
to the images
table.
<!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.");
}
}
?>