使用表单注册上传图像

时间:2016-08-08 09:44:25

标签: php database file upload

嗨,这是我的表格代码:

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

我有一个困惑,我怎么能用form.Below上传我的php文件中的图像

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
}

}

我在表格中给了五个照片选项,但我只在第一张照片中测试。帮助我如何使用我的代码上传图像文件。我很困惑如何添加图像或必须添加图像路径。请更正我的代码。

1 个答案:

答案 0 :(得分:0)

enctype="multipart/form-data"添加到表单标记

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