php - 登记表 - 上传图片

标签 php html mysql forms registration

我正在处理一个网站的注册表单,我正在尝试将照片上传到服务器并将文件路径传递到数据库。

这可能只是一个简单的问题,因为我是 php 和 mysql 的初学者。

这是我的标准表单,我们称之为 register.php。我删除了除图像之外的所有其他输入。

<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
    <input type="file" id="inputImage" Name="photo">
    <button class="btn btn-large btn-success" type="submit">Register</button>
</form>

这是执行文件,我们称之为code_exec.php

<?php
session_start();
include('connection.php');

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mname = $_POST['mname'];
$address = $_POST['address'];
$contact = $_POST['contact'];
$pic = $_POST['pic'];
$username = $_POST['username'];
$password = $_POST['password'];
$skype = $_POST['skype'];
$email = $_POST['email'];

//This is the directory where images will be saved 
$target = "upload/"; 
$target = $target . basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$pic = ($_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 an error if its not 
    echo "Sorry, there was a problem uploading your file.";
} 

mysql_query("INSERT INTO member(fname, lname, gender, address, contact, picture, username, password, skype, email, photo)VALUES('$fname', '$lname', '$mname', '$address', '$contact', '$pic', '$username', '$password', '$skype', '$email', '$pic')");

header("location: index.php?remarks=success");
mysql_close($con);

?>

如何找到图片文件的路径?

最佳答案

在代码中

Before

<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
    <input type="file" id="inputImage" Name="photo">
    <button class="btn btn-large btn-success" type="submit">Register</button>
</form>

After changes

<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post" enctype="multipart/form-data">
    <input type="file" id="inputImage" Name="photo">
    <button class="btn btn-large btn-success" type="submit">Register</button>
</form>

你只需添加 enctype="multipart/form-data" 在您的 html 表单标签中

Description

1.当您从表单上传任何类型的文件时,您必须在表单元素中写入enctype 属性。

谢谢

关于php - 登记表 - 上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17111518/

相关文章:

php - 将mysql表中的php记录插入到不同的表中

php - HTML/PHP 复选框不工作

javascript - 更好地测试 base64 URI 支持(我可以在 JS 中创建一个大的 base64 编码图像吗?)

javascript - 当元素底部超过点时移除类

java - Liferay 搜索迭代器仅在页面选择器中显示前 11 页

MySQL 带有 CASE 的复杂 WHEN 过滤器

php - 通过codeigniter中的嵌套数组将多行插入数据库

php - 阻止 NetBeans 将评论分组在一起

php - 复选框按钮在 Wordpress 中不起作用

mysql - 当同时连接到同一个表时,如何按 Rails 中的聚合计数进行排序?