-4

嗨,我是 PHP 和 MySql 的新手,我面临一个问题:

我有一个页面,允许用户将图像和一些详细信息(例如他们的姓名和电子邮件地址)上传到我的数据库,但我使用的代码不起作用

事实上,它不是向我的数据库添加数据,而是正在上传图像

我还想问有没有什么办法可以强制用户填写所有表单字段

html表单如下:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <div id="content"><br><div align="center">
            <form enctype="multipart/form-data" action="upload1.php" method="POST"> 
                Your Full Name: <input type="text" name="name" maxlength="40"><br> 
                Your Image Please: <input type="file" name="photo"><br>
                <input type="submit" value="Upload!"> 
            </form>
        </div>
    </body>
</html>

这是代码upload1.php

<?php 

    // random 4 digit  to add to our file name 

    // some people use date and time in stead of random digit 

    $random_digit=rand(00000000000000,99999999999999);

    //combine random digit to you file name to create new file name

    //use dot (.) to combile these two variables

    $new_file_name=$random_digit.$file_name;

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

    //This is our size condition 
    if ($photo_size > 350000) 
    { 
        echo "Your file is too large.<br>"; 
        $ok=0; 
    } 

    if (!($uploaded_type=="image/gif")) {
        $ok=0;
    } 
    if (!($uploaded_type=="image/jpg")) {
        $ok=0;
    } 
    if (!($uploaded_type=="image/png")) {
        $ok=0;
    } 
    if (!($uploaded_type=="image/bmp")) {
        $ok=0;
    } 
    if (!($uploaded_type=="image/jpeg")) {
        $ok=0;
    } 

    if ($ok=0) 
    { 
        Echo "Sorry your file was not uploaded"; 
    } 

    else 
    { 

        //This gets all the other information from the form 
        $name=$_POST['name'];
        $email=$_POST['email'];
        $pic=($_FILES['photo']['name']); 
        $banner="/$target";
        $url="xxxxxxxxxx";
        $clicks='0';

        // Connects to your Database 
        mysql_connect("xxxxxxxxxxx", "xxxxxxxxxxx", "xxxxxxxxx") or die(mysql_error()) ; 
        mysql_select_db("xxxxxxx") or die(mysql_error()) ;


        //Writes the information to the database 
        mysql_query("INSERT INTO girls (name, banner, clicks, url, email) VALUES ('{$name}','{$banner}','{$clicks}','{$url}','{$email}') "); 


        //Writes the photo to the server 
        if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
        { 

            //Tells you if its all ok 
            echo " The file has been uploaded and renamed to '$target' your information has also been added to the database.<br>To view your image online visit www.facesnap.tk/$target ";
        } 
        else { 

            //Gives and error if its not 
            echo "Sorry, there was a problem in your upload. Be sure that you follow instructions."; 
        } 
    }
?> 
4

1 回答 1

1

您必须仅将 '' 放入 varchars 而不是 int 值中,如果 '{$clicks}' 是数据库中的 int,请尝试删除 ''。

现在要使字段成为必填项,请在您的 upload1.php 脚本中执行此操作:

    session_start();

    //connect to db

    $errors = array();

    //validate name
    if (!isset($_POST['name']) || empty($_POST['name'])) {
        $errors[] = 'Your name is required.';
    }
    else {
       $name = mysql_real_escape_string(trim($_POST['name']));
    }

    //validate email
    if (!isset($_POST['email']) || empty($_POST['email'])) {
        $errors[] = 'Your email is required.';
    }
    else {
       $email = mysql_real_escape_string(trim($_POST['email']));
       $regex = '/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/';

       //change the 128 to your email field length in your db
       if (preg_match($regex, $email) && strlen($email) <= 128) {
           $email = strtolower($email);
       }
       else {
           $errors[] = 'Your email is not valid.';
       }
    }

    //validate the file upload
    if (!isset($_FILES['photo']) || empty($_FILES['photo'])) {
         $errors[] = 'Your photo is required.';
    }
    else if ($_FILES['logo']['name'] == '') {
         $errors[] = 'Your photo is required.';
    }
    else if (!file_exists($_FILES['photo']['tmp_name']) || !is_uploaded_file($_FILES['photo']['tmp_name'])) {
         $errors[] = 'The file could not be uploaded, please try again later.';
    }
    else {
        //validate the extention with your function is_img_ext()
        if (is_img_ext($_FILES['photo']['name'])) {
            $errors[] = 'The file you uploaded is not an image.';
        }
        //validate image size
        if ($_FILES['photo']['size'] > 350000) {
            $errors[] = 'The image you uploaded is too large.';
        }

        //if no errors and the file not exist move it to the target dir
        if (empty($errors)) {

             //generate a new filename for the image
             $random_digit=rand(00000000000000,99999999999999);
             $new_file_name = $random_digit.$file_name;
             $target = "g/".$new_file_name; 
             $target = $target . basename( $_FILES['photo']['name']);

            if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { 

            echo " The file has been uploaded and renamed to '$target' your information has also been added to the database.<br>To view your image online visit www.facesnap.tk/$target ";
            } 
            else { 

            echo "Sorry, there was a problem in your upload. Be sure that you follow instructions."; 
            } 

        }
    }

    if(!empty($errors)) {
        $_SESSION['form_error'] = $errors;
        header('Location: your_form.php');
    die();
    }

    //your rest script
    .....



    function is_img_ext($filename) {
        $ext = explode('.', $filename);
        $ext = strtolower(end($ext));

        if ($ext == jpeg || $ext == jpg || $ext == png || $ext == gif || $ext == bmp) {
            return true;
        }
        else {
            return false;
        }
    }

现在在 your_form.php 中:

    session_start();
    if (isset($_SESSION['form_error'])) {
        $errors = $_SESSION['form_error'];
        unset($_SESSION['form_error']);
    }
    echo '<ul>';
    foreach($errors as $error) {
        echo '<li>' . $error . '</li>';
    }
    echo '</ul>';
    //your form here
于 2012-09-21T17:46:03.220 回答