0

i have been trying to upload image files in to my database but i failed in many ways, the form saves everything except the image files. i am not sure if its the php or the java or the html page, honestly i lost my path. i have included all my pages, any help would mean a lot, thanks!!!

<html>
<head>
    <meta charset="utf-8">
    <title>PYB Register Bike</title>
    <link href="bootstrap-3.3.7/dist/css/bootstrap-theme.min.css" rel="stylesheet"> <!--styling  css link -->
    <link href="bootstrap-3.3.7/dist/css/bootstrap.min.css" rel="stylesheet"> <!-- styling css link -->
    <link href="font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet"> <!-- styling css link -->  
    <link rel="stylesheet" href="../ProtectYourBike.css"> <!-- CSS Reference -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script>    

 <script> 
    // wait for the DOM to be loaded 
    $(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $('#BikeUserRegistration').ajaxForm(function() { 
            alert("Thank you for your comment!"); 
        }); 
    }); 
</script> 

</head>
<body>

<nav class="navbar navbar-default"> <!-- creating a container at the top of the page -->
    <div class="link"><br> <!-- styling container --> 
       <h2 class="logo"> 
        <h1> <!-- Navigation links referenced to other HTML pages -->    
            <a href="../8BikeStatusPage/BikeStatusPage.html">Status</a>
            <a href="../6ReportStolenPage/ProtectYourBikeReportStolen.html">Report</a>
            <a href="../7ViewBikeListPage/ProtectYourBikeViewBikeList.html">View</a>
            <a href="../5BikeRegistrationPage/ProtectYourBikeRegisterBike.html">Register</a>
            <a href="../1PublicLoginPage/ProtectYourBikeLogin.html">Logout</a>
        </h1>
       </h2>
    </div><br>
</nav>

<div class="containerbikeregister">

<h3 class="text-center">Register your bike details here</h3><br> <!-- title  -->

        <div class="row gap"> <!-- form styling -->

            <div id="divMessage"></div>
            <br>
            <form enctype="multipart/form-data" class="register-input" id="BikeUserRegistration"> <!-- id="BikeUserRegistration" references JavaScript function --> 

        <div id="form" class="col-md-4"> <!-- form styling -->

            <label>Manufacturer Part Number</label> <!-- ID linked to JavaScript -->
            <input type="text" id="txtManufacturer" name="Manufacturer" placeholder="(MPN) EGBIKE01/ KUSH2 etc" value="" required="required" class="form-control"><br> 

            <label>Brand</label>
            <input type="text" id="txtBrand" name="Brand" placeholder="Raleigh/ Diamondback/ Mafiabikes etc" value="" required="required" class="form-control"><br> 

            <label>Model</label>
            <input type="text" id="txtModel" name="Model" placeholder="Motus/ S10/ Madmain etc" value="" required="required" class="form-control"><br> 

            <label>Type</label>
            <input type="text" id="txtType" name="Type" placeholder="Electric Bike/ Mountain Bike/ BMX etc" value="" required="required" class="form-control"><br> 

            <label>Wheel Size</label>
            <input type="text" id="txtWheels" name="Wheels" placeholder="14”/20”/26” etc" value="" required="required" class="form-control"><br> 

            <label>Colour</label>                
            <input type="text" id="txtColour" name="Colour" placeholder="White/Black/Red etc" value="" required="required" class="form-control"><br> 

            <label>Number of Gears</label>
            <input type="text" id="txtGears" name="Gears" placeholder="6/7/8/14 etc" value="" required="required" class="form-control"><br> 

                <label>Choose a Picture</label> 
                <input type="file" name="image" id="Bikepic" ><br>

        </div>
        <div id="form" class="col-md-8">  <!-- styling form -->

            <label>Brake Type</label> 
            <input type="text" id="txtBrakes" name="Brakes" placeholder="Direct-Linear Pull (V-Brakes)/Hydraulic Rim Brakes etc" value="" required="required" class="form-control"><br> 

            <label>Suspension</label>
            <input type="text" id="txtSuspension" name="Suspension" placeholder="Front/Full/None etc" value="" required="required" class="form-control"><br> 

            <label>Gender</label>
            <input type="text" id="txtGender" name="Gender" placeholder="Make/Female/Unisex etc" value="" required="required" class="form-control"> <br>

            <label>Age of Group</label>
            <input type="text" id="txtAgeof" name="Ageof" placeholder="Adult/Child/8-10/10-12 etc" value="" required="required" class="form-control"><br> 

            <label id="commentTextPrompt">Additional Informations</label><!-- creating label for validation purposes  -->
            <textarea name="Additional" type="text" id="commentText" class="form-control" rows="8" placeholder="Additional Informations"></textarea> <!-- creating a comment field with 8 rows -->

            <input name="submit" type="submit" id="btnRegister" value="Submit" class="btn btn-warning btn-block">

        </div>
        </form>
    </div>

</div>
</body>
<footer>
</footer> <!-- Script src references JavaScript pages associated with this HTML page -->
<script src="BikeRegistration.js"></script>
</html>

//java file start

$(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $('#BikeUserRegistration').on('submit', function(event) { 
             event.preventDefault();          
$.ajax({

type: "POST",
url: "BikeRegistrationDAO.php",
data: FormData+"&phpfunction=bikeRegister",
success: function(echoedMsg){
    if(echoedMsg=='true'){
        window.location="ProtectYourBikeRegisterBikeSuccess.html";
    }else{
        $("#divMessage").html(echoedMsg);
    }       
},
    error: function(msg){
        console.log(msg);
    }
});
  });
  }); 
//java file end

<?php

if($_POST['phpfunction'] == 'bikeRegister') {
    bikeRegister();
}
function bikeRegister() {

    $Manufacturer = $_POST['Manufacturer'];
    $Brand = $_POST['Brand'];
    $Model = $_POST['Model'];
    $Type = $_POST['Type'];
    $Wheel = $_POST['Wheels'];
    $Colour = $_POST['Colour'];
    $Number = $_POST['Gears'];
    $Brake = $_POST['Brakes'];
    $Suspension = $_POST['Suspension'];
    $Gender = $_POST['Gender'];
    $Age = $_POST['Ageof'];
    $Additional = $_POST['Additional'];

    include "../include/config.php";

    $result=mysqli_query($connection,"SELECT * FROM `Bike` WHERE manufacturer='$Manufacturer'");

    if($row=mysqli_num_rows($result) > 0){

        echo "This Bicycle's Manufacturer, Is already registered.";
            return;
    }           

$sql = "INSERT INTO Bike (Manufacturer, Brand, Model, Type, Wheel, Colour, Number, Brake, Suspension, Gender, Age, Additional) VALUES ('$Manufacturer', '$Brand', '$Model', '$Type', '$Wheel', '$Colour', '$Number', '$Brake', '$Suspension', '$Gender', '$Age', '$Additional')";

       if(!mysqli_query($connection, $sql))
    {
        echo 'Not Submited';

    }else{

        echo 'Submitted Successfully';
    }
}

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}
}   
?>
2
Contributors
1
Reply
41
Views
6 Months
Discussion Span
Last Post by ryantroop
1

So...

I see your check for existence SQL, and your actual insert... I don't, however, see your attempt to save the file data to the database. Am I misunderstanding your question? The reason it wouldn't be in your database is because you're not even trying to put it in there :-/

Are the images available in the directory you make with $target_file = $target_dir . basename($_FILES["image"]["name"]);

Also, in general, you don't want to put the image data in the database - that's slow, especially for large image files - you should do as you are (move the image to a directory), and in the database keep the path to the image for use later.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.