注册表 - 上传图片

时间:2013-06-14 15:02:42

标签: 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);

?>

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

5 个答案:

答案 0 :(得分:1)

代码

  

<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>
  

更改后

<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>

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

  

描述

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

谢谢

答案 1 :(得分:0)

这很简单,只需将$target添加到SQL查询中即可。

// Escape the string, so you wouldn't be affected by SQL injection.
// Move to MySQLi or PDO, and follow guide http://bobby-tables.com/php.html
// to have your queries automatically escaped, as it's easy to forget about
// using mysql_real_escape_string() (also, it's annoying to type it).
$target = mysql_real_escape_string($target);
mysql_query("INSERT INTO member(..., target) VALUES (..., '$target')");

答案 2 :(得分:0)

只使用我提供的布局:

对于html使用,请记住,您可以随时改进它:

<form action="nameofthepage.php" method="post" enctype="multipart/form-data">

                    <table align="center" width="750">

                        <tr align="center">
                            <td colspan="6"><h2>Create an Account</h2></td>
                        </tr>

                        <tr>
                            <td align="right">Customer Name:</td>
                            <td><input type="text" name="c_name" required/></td>
                        </tr>

                        <tr>
                            <td align="right">Customer Email:</td>
                            <td><input type="text" name="c_email" required/></td>
                        </tr>

                        <tr>
                            <td align="right">Customer Password:</td>
                            <td><input type="password" name="c_pass" required/></td>
                        </tr>

                        <tr>
                            <td align="right">Customer Image:</td>
                            <td><input type="file" name="c_image" required/></td>
                        </tr>



                        <tr>
                            <td align="right">Customer Country:</td>
                            <td>
                            <select name="c_country">
                                <option>Select a Country</option>
                                <option>Afghanistan</option>
                                <option>India</option>
                                <option>Japan</option>
                                <option>Pakistan</option>
                                <option>Israel</option>
                                <option>Nepal</option>
                                <option>United Arab Emirates</option>
                                <option>United States</option>
                                <option>United Kingdom</option>
                            </select>

                            </td>
                        </tr>

                        <tr>
                            <td align="right">Customer City:</td>
                            <td><input type="text" name="c_city" required/></td>
                        </tr>

                        <tr>
                            <td align="right">Customer Contact:</td>
                            <td><input type="text" name="c_contact" required/></td>
                        </tr>

                        <tr>
                            <td align="right">Customer Address</td>
                            <td><input type="text" name="c_address" required/></td>
                        </tr>


                    <tr align="center">
                        <td colspan="6"><input type="submit" name="register" value="Create Account" /></td>
                    </tr>



                    </table>

                </form>

和php使用:

<?php 
    if(isset($_POST['register'])){


        $ip = getIp();

        $c_name = $_POST['c_name'];
        $c_email = $_POST['c_email'];
        $c_pass = $_POST['c_pass'];
        $c_image = $_FILES['c_image']['name'];
        $c_image_tmp = $_FILES['c_image']['tmp_name'];
        $c_country = $_POST['c_country'];
        $c_city = $_POST['c_city'];
        $c_contact = $_POST['c_contact'];
        $c_address = $_POST['c_address'];


        move_uploaded_file($c_image_tmp,"customer/customer_images/$c_image");

         $insert_c = "insert into customers (customer_ip,customer_name,customer_email,customer_pass,customer_country,customer_city,customer_contact,customer_address,customer_image) values ('$ip','$c_name','$c_email','$c_pass','$c_country','$c_city','$c_contact','$c_address','$c_image')";

        $run_c = mysqli_query($con, $insert_c); 

        $sel_cart = "select * from cart where ip_add='$ip'";

        $run_cart = mysqli_query($con, $sel_cart); 

        $check_cart = mysqli_num_rows($run_cart); 

        if($check_cart==0){

        $_SESSION['customer_email']=$c_email; 

        echo "<script>alert('Account has been created successfully, Thanks!')</script>";
        echo "<script>window.open('customer/my_account.php','_self')</script>";

        }
        else {

        $_SESSION['customer_email']=$c_email; 

        echo "<script>alert('Account has been created successfully, Thanks!')</script>";

        echo "<script>window.open('checkout.php','_self')</script>";


        }
    }





?>

但不要忘记更改表格和值的名称 希望能够帮助他们!

答案 3 :(得分:0)

使用move_uploaded_file($_FILES['photo']['tmp_name'], $target)后,图片会移至$target路径。所以图像的路径是$target。现在只需在您使用的查询功能上使用$target而不是$pic。喜欢 -

$target = mysql_real_escape_string($target);  //escape string for sql injection 
mysql_query("INSERT INTO member(fname, lname, gender, address, contact, picture, username, password, skype, email, photo)VALUES('$fname', '$lname', '$mname', '$address', '$contact', '$target', '$username', '$password', '$skype', '$email', '$pic')");

答案 4 :(得分:0)

何时从表单上传照片/媒体,您需要在表单中添加<form method="" action="" enctype = "multipart/form-data">属性。如果没有上传媒体将无法正常工作。

另外,请务必仅使用post方法进行媒体上传