du5114 2016-08-08 09:44
浏览 58

使用表单注册上传图像

Hi this is my form code:

echo "<form method=\"POST\" action=\"\">
"; 
echo "<td>User Id</td><td> <input type=\"text\" name=\"user_id\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Full Name</td><td> <input type=\"text\" name=\"full_name\">    </td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Qualification</td><td> <input type=\"text\" name=\"qualification\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Locality</td><td> <input type=\"text\" name=\"locality\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Description</td><td> <input type=\"text\" name=\"description\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Languages Known</td><td><input type=\"text\" name=\"language\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Experience</td><td><input type=\"text\" name=\"experience\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Dress</td><td><input type=\"text\" name=\"dress_code\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Bank Details</td><td><input type=\"text\" name=\"bank_details\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Two Wheelers</td><td><input type=\"text\" name=\"two_wheeler\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Threshold</td><td><input type=\"text\" name=\"threshold\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td><input name=\"pic1\" type=\"file\"></td><td><input name=\"pic\" type=\"submit\" value=\"Upload Image\"></td>
"; // Kindly check this line
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Photo 2</td><td><input type=\"text\" name=\"pic2\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Photo 3</td><td><input type=\"text\" name=\"pic3\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Photo 4</td><td><input type=\"text\" name=\"pic4\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Photo 5</td><td><input type=\"text\" name=\"pic5\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>ID Number</td><td><input type=\"text\" name=\"id_proof\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Contact Details</td><td><input type=\"text\" name=\"contact_details\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td>Date</td><td><input type=\"text\" name=\"datetime_added\"></td>
"; 
echo "</tr>
"; 
echo "<tr>
"; 
echo "<td><input id=\"button\" type=\"submit\" name=\"btn-signup\" value=\"Sign-Up\"></td>
"; 
echo "</tr>
"; 
echo "</form>
";

I have a confusion that how can i upload a image in the database with form.Below is my php file

if(isset($_POST['btn-signup']))
{
$user_id = mysql_real_escape_string($_POST['user_id']);
$full_name = mysql_real_escape_string($_POST['full_name']);
$qualification = mysql_real_escape_string($_POST['qualification']);
$locality = mysql_real_escape_string($_POST['locality']);
$description = mysql_real_escape_string($_POST['description']);
$language = mysql_real_escape_string($_POST['language']);
$experience = mysql_real_escape_string($_POST['experience']);
$dress_code = mysql_real_escape_string($_POST['dress_code']);
$bank_details = mysql_real_escape_string($_POST['bank_details']);
$two_wheeler = mysql_real_escape_string($_POST['two_wheeler']);
$threshold = mysql_real_escape_string($_POST['threshold']);
$pic1 = mysql_real_escape_string($_POST['pic1']);
$pic2 = mysql_real_escape_string($_POST['pic2']);
$pic3 = mysql_real_escape_string($_POST['pic3']);
$pic4 = mysql_real_escape_string($_POST['pic4']);
$pic5 = mysql_real_escape_string($_POST['pic5']);
$id_proof = mysql_real_escape_string($_POST['id_proof']);
$contact_details = mysql_real_escape_string($_POST['contact_details']);
$datetime_added = mysql_real_escape_string($_POST['dtetime_added']);

$uname = trim($uname);
$email = trim($email);
$upass = trim($upass);

$target = "upload/"; 
$target = $target . basename( $_FILES['photo']['name']); 

 //This gets all the other information from the form 
 $pic1=($_FILES['photo']['name']); 

 //Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been     uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file.";
} 


// email exist or not
$query = "SELECT user_id FROM promoter WHERE user_id='$user_id'";
$result = mysql_query($query);

$count = mysql_num_rows($result); // if email not found then register

if($count == 0){

    if(mysql_query("INSERT INTO promoter(user_id,full_name,qualification,locality,description,language,experience,dress_code,bank_details,two_wheeler,threshold,pic2,pic3,pic4,pic5,id_proof,contact_details,datetime_added, target)    VALUES('$user_id','$full_name','$qualification','$locality','$description','$language','$experience','$dress_code','$bank_details','$two_wheeler','$threshold','$pic2','$pic3','$pic4','$pic5','$id_proof','$contact_details','$datetime_added','$target')"))
    {
        ?>
        <script>alert('successfully registered ');</script>
        <?php
    }
    else
    {
        ?>
        <script>alert('error while registering you...');</script>
        <?php
    }       
}
else{
        ?>
        <script>alert('Sorry Email ID already taken ...');</script>
        <?php
}

}

There i have given five photo option in the form, but i am testing in the first pic1 only . Help me how can i upload the image file with my code. I am confused how to add image or have to add the image path. Kindly correct my code.

  • 写回答

1条回答 默认 最新

  • dsk95913 2016-08-08 09:47
    关注

    Add enctype="multipart/form-data" to your form tag

    echo "<form method=\"POST\" action=\"\" enctype=\"multipart/form-data\"> ";

    评论

报告相同问题?