4

我正在为网站制作注册表单,我正在尝试将照片上传到服务器并将文件路径传递到数据库。

这可能只是一个简单的问题,因为我是 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);

?>

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

4

5 回答 5

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>

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

描述

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

谢谢

于 2017-05-14T01:53:28.977 回答
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')");
于 2013-06-14T15:19:12.333 回答
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>";


        }
    }





?>

但不要忘记更改表的名称和值,希望它有所帮助!

于 2016-12-21T21:56:21.587 回答
0

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

还要确保post仅将方法用于媒体上传

于 2017-09-02T06:40:58.063 回答
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')");
于 2017-02-22T08:28:42.697 回答