对php表单的修改


Modifications to a php form

php入门。我有以下表单脚本:

<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_FILES['uploaded_file']['name'])) {
$db_name = "xx";
$server = "localhost";
$DBuser = "xxx";
$DBpass = "xxxx";
$name = $_POST['name'];
$email = $_POST['email'];
$file = "Imagini_lume/" . $email . ".jpg";
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Adresa de e-mail este incompleta!";
} else {
    $name = trim($name);
    if ($name != '') {
        $temp_file = trim($_FILES['uploaded_file']['name']);
        if ($temp_file != '') {
            // here
            mysql_connect($server, $DBuser, $DBpass) or die(mysql_error());
            mysql_select_db($db_name) or die(mysql_error());
            // check for existing
            //$result = mysql_query("select email from Emails where email='$email'") or die(mysql_error());
            $data = mysql_fetch_array($result);
            if ($data == null) {
                mysql_query("insert into Emails (Email,Full_Name,Image) values('$email','$name','$file');") or die(mysql_error());
                if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file)) {
                    //sucess
                } else {
                    echo "Eroare incarcare imagine!";
                }
                // email sending
                sendEmail("xx@gmail.com", getContent($name, $email, $file));
                echo "Sucessfull!";
            //} else {
                //echo "Adresa de e-mail este folosita de cineva.";
            }
        } else {
            echo "Trebuie sa selectati desenul dvs.!";
        }
    } else {
        echo "Introduceti numele complet!";
    }
}} else {
    echo "Va rugam sa completati spatiile de mai sus!";
}

注:if (isset($_POST['name']) && isset($_POST['email']) && isset($_FILES['uploaded_file']['name'])) {} else { echo "Va rugam sa completati spatiile de mai sus!";}与上述不同。我只是这样添加的,这样我就可以在"代码示例"中包含所有脚本。

问题是:

  1. 如果用户使用相同的名称和电子邮件地址上传多个文件,这些文件将被覆盖,我想区分这些文件(例如"name@email.com1.jpg","name@email.com2.jpg"和on)。我已经找到了这个脚本,但作为一个初学者,我不知道在哪里插入以下代码(另一个注意事项-我不知道是完整的代码还是缺少一些脚本):

    while(file_exists($name . $extension)) {
        $name .= '1';
    }
    
  2. 我想"强迫"用户键入它的全名。由于设计原因,我只有一个"全名"文本框,我想强制用户键入至少2个单词(名字和姓氏)-基本上是单词的最小nr,而不是字符的nr。再次,我找到了一个代码,但仍然不知道如何将其应用于我的代码,在哪里插入:

    if ($("#et_newpost_content").text().split(/'s+/).length < 250) {
        alert('Message must be at least 250 words.')
    }
    

请告知。

好的,很简单。增加图像名称计数器的第一个脚本实现起来非常简单:

1)

$db_name = "xx";
$server = "localhost";
$DBuser = "xxx";
$DBpass = "xxxx";
$name = $_POST['name'];
$email = $_POST['email'];
// Now checking the name existence
// Defining the initial state: the format is: my@email.com + counter + extension
// i.e. my@mail.com1.jpg
$ext = ".jpg";
$count = 1;
$base_file_name = "Imagini_lume/" . $mail;
// while already exists a file name like that, increment count
while (file_exists($base_file_name.$count.$ext))
    $count++;
// finally define the official name of the file!
$file = $base_file_name.$count.$ext;

2) 您编写的代码是javascript(jquery),而不是php。。。如果你想检查参数服务器端,你可以使用filter_var:

if (filter_var($name, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[A-Z][a-z]* [A-Z][a-z]*/"))) === FALSE)
    die("Error: You should enter your complete name. i.e. John Black");

(查看php指南中的正则表达式章节)

最后,请不要在真实的web应用程序中使用该代码。正如它所写的,它对SQL注入攻击是不安全的:

SQL注入

此外,您没有检查每个用户输入参数的正确性。慢慢来积累一些经验;)