Jump to content
Larry Ullman's Book Forums

Inserting Into Mysql Database


Recommended Posts

Hi,

 

I am developing a database for a used car salesman and am having trouble uploading a set of images for a particular car i.e. a gallery of images for that particular car.

 

I can get it to insert into the database but it's assigning it to that particular car that I am stuck with,

 

If copy and paste my code, would someone be willing to have a look at where it's going wrong.

 

Thanks.

Link to comment
Share on other sites

Just to explain I have a table called images which holds a column for ID, VIN and ImageFile.

 

Thanks. Here is upload form.

 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<form action="upload-file.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

 

Here is upload file.

 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<?php
$vin = trim($_POST['VIN']);
$currentfolder = getcwd();
print "This script is running in: " .$currentfolder."<br>"."\n";
$target_path = getcwd()."/uploads/";
print "The uploaded file will be stored in the folder: " .$target_path."<br>"."\n";
    
$target_path = $target_path . basename($_FILES['file']['name']);
$imagename = "uploads/".basename( $_FILES['file']['name']);
print "The full name of the uploaded file is ".$target_path."<br>"."\n";
    
print "The relative name of the file for use in the IMG tag is " .$imagename."<br><br>"."\n";
    
if(move_uploaded_file($_FILES['file']['tmp_name'],$target_path)) {
print "The file ". basename($_FILES['file']['name']). " has been uploaded<br>". "\n";}

// Connect and select:
$dbc = mysqli_connect('localhost', 'root', '', 'cars');

// Define the query:
$file_name = $_FILES["file"]["name"];

$query = "INSERT INTO tbl_images (VIN, ImageFile) VALUES ('$vin', '$file_name')";
print $query."<br>\n";

if ($r = mysqli_query($dbc, $query)) { // Run the query.
    
print "<p>You have successfully entered $target_path into the database</p>\n";
}

mysqli_close($dbc); // Close the connection.
?>

</body>
</html>

 

The script successfully uploads the image into ImageFile column in images table and my print messages confirm this but does n't allocate to the VIN (Vehicle Identification Number), I am not sure how to get it into that particular VIN. Any help would be much appreciated.

 

Thanks

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...