如何在现有上传图像中添加注册表单代码

时间:2014-07-27 14:30:01

标签: php

这是我的代码:

     <html>
<head>
<title>upload images to database</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<h1>Register Form</h1>
<form class = "form" action="backup.php" method = "POST" enctype = "multipart/form-data">
Upload Photo:<input type= "file" name= "image"><br />
Firstname:<input type = "text" name = "fname"><br />
Lastname:<input type = "text" name = "lname"><br />
Password:<input type = "text" name = "password"><br />
Retype-password:<input type = "text" name = "rpassword"><br />
Email:<input type = "text" name = "email"><br />
Address:<input type = "text" name = "address"><br />
Phone:<input type = "text" name = "phone"><br />
<input type= "submit" class = "submit" value= "upload">
</form>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//connect to database
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("image") or die(mysql_error());
$file = '';
$file= $_FILES['image']['tmp_name'];
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$email = $_POST['email'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$password = $_POST['password'];
$rpassword = $_POST['rpassword'];
 if(!isset($file))
 {
 echo "please select an image";
 }
 else
 {
$image = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$image_name = mysql_real_escape_string($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size ==FALSE)
{
echo "That's not an image";
}
else
    {
    if(!$insert = mysql_query("INSERT INTO upload VALUES('','$image_name','$image','$firstname','$lastname','$email','$address','$password','$rpassword')"))
    {
    echo "Problem uploading image";
    }
    else{

    $lastid = mysql_insert_id();
    echo "Image upload.<p/>Your Image</p><img src=get.php?id=$lastid>";

    }
} 
 }
}
?>
</body>
</html>

这是我上传图片的php代码,存储在数据库中,也显示在主页上。

现在它工作正常,我想为额外的字段添加代码,如名字,姓氏等,如寄存器表格。

这里我需要为注册表单添加代码。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

你应该使用$ _POST方法,因为你的表单中有action="POST" html它使用post request将数据发送到服务器

添加html

<form action="index.php" method = "POST" enctype = "multipart/form-data">
    First Name: <input type = "text" name= "fname"><br />
    Last Name:<input type = "text" name= "lname"><br />
    Password :<input type = "text" name = "password"><br />
    Retype-password: <input type = "text" name = "rpassword"><br />
    Email:<input type = "text" name = "email"><br />
    Phone Num: <input type = "text" name = "phone"><br />
    Address: <input type ="text" name = "address"><br />
    ...... some your html 
</form>

在服务器上获取数据

$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$email = $_POST['email'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$password = $_POST['password'];
$rpassword = $_POST['rpassword'];

Look at this tutorial