4

Web サイトの登録フォームに取り組んでいます。写真をサーバーにアップロードし、ファイル パスをデータベースに渡そうとしています。

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

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

簡単$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

を使用する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 に答える