Hi there,
I was hoping for a little help please ...I have a form that allows users to upload an image to a mysql database....I got the code form a tutorial and am trying to modify it....
The image is Inserted into a its own "Image" table which contains the following fields: imageId (int, autocount), image(longblob), FileType, (Varchar).
I am trying to add another field into that table AnimalID (int) - is Primary key of another table. This is fine i have added the field ok....
My problem is trying to get the INSERT php code on the AddImage.php page to actually insert this newly built field as well as the other fields of the image table.
I dont know what to put in the INSERT & VALUES bit.... I just need it so when the user inserts the image, it will also insert the AnimalID (which i added tothe table structure)
If anybody can please help me that would be great.
Heres the code on addImage.php:
mysql_select_db($database_woodside, $woodside);
$query_animal = sprintf("SELECT AnimalID FROM animal WHERE AnimalID = %s", $colname_animal);
$animal = mysql_query($query_animal, $woodside) or die(mysql_error());
$row_animal = mysql_fetch_assoc($animal);
$totalRows_animal = mysql_num_rows($animal);
if ($_POST['Submit']) {
if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size']) {
//print_r($_FILES);
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("woodside");
$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"),
$_FILES['file']['size']));
$query = sprintf("INSERT INTO image(Image, FileType) VALUES
('%s', '%s')", $photo, $_FILES['file']['type']);
if (mysql_query($query)) {
$messages[] = "Your files is successfully store in database";
} else {
$messages[]= mysql_error();
}
} else {
$messages[]="The file is bigger than the allowed size please resize";
}
}
?>
<html>
<head>
<title>Add Image</title>
</head>
<body>
<?php
if (isset($messages)) {
foreach ($messages as $message) {
print $message ."<br>";
}
}
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="hidden" name="MAX_FILE_SIZE" value="96000">
<input type="submit" name="Submit" value="Submit">
<input name="animalid" type="hidden" id="animalid" value="<?php echo $row_animal['AnimalID']; ?>">
</form>
</body>
</html>
Thank you 🙂