数据库不存储表单中的数据

时间:2013-05-06 06:37:57

标签: php mysql

这是我的html,代码检查是否有任何错误

    <html>
    <head>
    <title> Add Team </title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="css/thickbox.css" type="text/css" media="screen" />
    <link href="css/styles.css" rel="stylesheet" type="text/css" />
    <script language="javascript"> 
        var RE = /^.+@.+\..{3}$/;
        var RE1 = /^[a-zA-Z]+$/;
        var RE2 = /^[0-9]{10}$/;

        function validateForm()
        {
        if (document.f1.pname.value == "")
        {
        window.alert(" Name should NOT BE empty");
        document.f1.pname.focus();
        return false;
        }
        else
        if (RE1.test(document.f1.pname.value) == false)
        {
        alert("Invalid format");
        return false;
        }
        else
        if (document.f1.pname.value.length < 6)
        {
        window.alert("name must have atleast SIX characters");
        document.f1.pname.focus();
        return false;
        }
        else
        if (document.f1.role.value == "")
        {
        window.alert("role should NOT BE empty");
        document.f1.role.focus();
        return false;
        }
        else
        if (RE1.test(document.f1.role.value) == false)
        {
        alert("INvalid format");
        return false;
        }
        else
        if (document.f1.role.value.length < 6)
        {
        window.alert("role must have atleast SIX  characters");
        document.f1.role.focus();
        return false;
        }
        else
        if (document.f1.role.value == "")
        {
        window.alert("role should NOT BE empty");
        document.f1.role.focus();
        return false;
        }
        else
        if (document.f1.education.value == "")
        {
        window.alert("education should NOT BE empty");
        document.f1.education.focus();
        return false;
        }
        else
        if (RE1.test(document.f1.education.value) == false)
        {
        alert("INvalid format");
        return false;
        }
        else
        if (document.f1.education.value.length < 6)
        {
        window.alert("education must have atleast SIX characters");
        document.f1.education.focus();
        return false;
        }
        else
        if (document.f1.education.value == "")
        {
        window.alert("education should NOT BE empty");
        document.f1.education.focus();
        return false;
        }
        else
        if (document.f1.hobbies.value == "")
        {
        window.alert("hobbies should NOT BE empty");
        document.f1.hobbies.focus();
        return false;
        }
        else
        if (RE1.test(document.f1.hobbies.value) == false)
        {
        alert("INvalid format");
        return false;
        }
        else
        if (document.f1.hobbies.value.length < 6)
        {
        window.alert("hobbies must have atleast SIX characters");
        document.f1.hobbies.focus();
        return false;
        }
        else
        if (document.f1.hobbies.value == "")
        {
        window.alert("hobbeis should NOT BE empty");
        document.f1.hobbies.focus();
        return false;
        }
        else
        if (document.f1.best.value == "")
        {
        window.alert("what is your best one liner should NOT BE empty");
        document.f1.best.focus();
        return false;
        }
        else
        if (RE1.test(document.f1.best.value) == false)
        {
        alert("INvalid format");
        return false;
        }
        else
        if (document.f1.best.value.length < 6)
        {
        window.alert("what is your best one liner must have atleast SIX characters");
        document.f1.best.focus();
        return false;
        }
        else
        if (document.f1.best.value == "")
        {
        window.alert("what is your best one liner should NOT BE empty");
        document.f1.best.focus();
        return false;
        }
        else
        if (document.f1.look.value == "")
        {
        window.alert("who do they say you look like should NOT BE empty");
        document.f1.look.focus();
        return false;
        }
        else
        if (RE1.test(document.f1.look.value) == false)
        {
        alert("INvalid format");
        return false;
        }
        else
        if (document.f1.look.value.length < 6)
        {
        window.alert("who do they say you look like  must have atleast SIX characters");
        document.f1.look.focus();
        return false;
        }
        else
        if (document.f1.look.value == "")
        {
        window.alert("who do they say you look like should NOT BE empty");
        document.f1.look.focus();
        return false;
        }
        else
        if (document.f1.role.value == "")
        {
        window.alert("role should NOT BE empty");
        document.f1.role.focus();
        return false;
        }
        else
        if (RE1.test(document.f1.role.value) == false)
        {
        alert("INvalid format");
        return false;
        }
        else
        if (document.f1.role.value.length < 6)
        {
        window.alert("role must have atleast SIX characters");
        document.f1.role.focus();
        return false;
        }
        else
        if (document.f1.role.value == "")
        {
        window.alert("role should NOT BE empty");
        document.f1.role.focus();
        return false;
        }
        else
        {
        window.alert("ur entire Given input is valid..");
        return true;
        }

        }
    </script></head>
<body>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" >

            <tr>
        <form name="f1" method="post"  onsubmit="return validateForm();" action="connect.php" enctype="multipart/form-data">
            <td>
                <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFF">
                    <tr>
                        <td colspan="3"><strong>Team Member </strong></td>
                    </tr>
                    <tr>
                        <td width="78">Name</td>
                        <td width="6">:</td>
                        <td width="294"><input name="pname" type="text" id="t1" onblur="pname_validateForm();"></td>
                    </tr>
                    <tr>
                        <td>Role</td>
                        <td>:</td>
                        <td><input name="role" type="text" id="t2" onblur="role_validateForm();"></td>
                    </tr>
                    <tr>
                        <td>Profile Image</td><td>:</td>
                        <td><input type="file" name="file_upload" id="upload_file" /> </td>
                    </tr>
                    <tr>
                        <td>Education</td>
                        <td>:</td>
                        <td><input name="education" type="text" id="t3" onblur="education_validateForm();"></td>
                    </tr>
                    <tr>
                        <td>Hobbies</td>
                        <td>:</td>
                        <td><input name="hobbies" type="text" id="t4" onblur="hobbies_validateForm();"></td>
                    </tr>
                    <tr>
                        <td>What is your best one liner?</td>
                        <td>:</td>
                        <td><input name="best" type="text" id="t5" onblur="best_validateForm();"></td>
                    </tr>
                    <tr>
                        <td>Who do they say you look like?</td>
                        <td>:</td>
                        <td><input name="look" type="text" id="t6" onblur="look_validateForm();"></td>
                    </tr>
                    <tr>
                        <td>What people do not like about me?</td>
                        <td>:</td>
                        <td><input name="about" type="text" id="t7" onblur="about_validateForm();"></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td><input name="submit" class="btn_login" type="submit" value="Submit" /></td>
                    </tr>
             </form>
       </table>

这是我的数据库连接代码我不知道为什么它没有将注册表单数据存储在数据库中,而只是显示一些空白页面

我尝试了很多次但它不起作用请帮我找出错误:

   <?php

以下是数据库连接:

       $host = "localhost"; // Host name
       $username = "root"; // Mysql username
        $password = ""; // Mysql password
       $db_name = "project"; // Database name

连接到服务器并选择数据库:

     mysql_connect("$host", "$username", "$password") or die("cannot connect");
     mysql_select_db("$db_name") or die("cannot select DB");

检查图像类型并从表单中获取图像:

   if (isset($_POST['submit'])) {
    $target = "../uploads/";
    $allowedExts = array("jpg", "jpeg");
    $value = explode(".", $_FILES["file_upload"]["name"]);
    $extension = strtolower(array_pop($value));
    //$extension = end(explode(".", $_FILES["file_upload"]["name"]));
    $target = $target . basename($_FILES['file_upload']['name']);

    //$date = date("Y-m-d H:i:s");
    //Function to generate image thumbnails
     function make_thumb($src, $dest, $desired_width) {

      /* read the source image */
    $source_image = imagecreatefromjpeg($src);
      $width = imagesx($source_image);
    $height = imagesy($source_image);

    /* find the "desired height" of this thumbnail, relative to the desired width  */
    $desired_height = floor($height * ($desired_width / $width));

    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

    /* copy source image at a resized size */
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

    /* create the physical thumbnail image to its destination with 100% quality */
    imagejpeg($virtual_image, $dest, 100);
}

  // Get values from form
    $pname = $_POST['pname'];
    $role = $_POST['role'];
    $pimg = @$_POST['file_upload'];
    $education = $_POST['education'];
    $hobbies = $_POST['hobbies'];
    $best = $_POST['best'];
    $look = $_POST['look'];
    $about = $_POST['about'];

 //check for allowed extensions
if ((($_FILES["file_upload"]["type"] == "image/jpg") || ($_FILES["file_upload"]["type"] == "image/jpeg")) && in_array($extension, $allowedExts)) {
    $photoname = $_FILES["file_upload"]["name"];
    if (file_exists("../uploads/" . $pimg)) {
        die('<div class="error">Sorry <b>' . $pimg . '</b> already exists</div>');
    }

    if (move_uploaded_file($_FILES['file_upload']['tmp_name'], $target)) {
        //$query = "INSERT INTO photos (image,) VALUES ('$photoname','$date')";
        $query = "INSERT INTO teams(name,role,pimg,education,hobbies,best,look,about)VALUES('$pname','$role','$pimg','$education','$hobbies','$best','$look','$about')";
        mysql_query($query);
        $sql = "SELECT MAX(id) FROM photos";
        $max = mysql_query($sql);
        $row = mysql_fetch_array($max);
        $maxId = $row['MAX(id)'];

        $type = $_FILES["file_upload"]["type"];
        switch ($type) {
            case "image/jpeg":
                $ext = ".jpeg";
                break;
            case "image/jpg";
                $ext = ".jpg";
                break;
        }

        //define arguments for the make_thumb function
        $source = "../uploads/" . $photoname;
        $destination = "../thumbnails/thumb_" . $maxId . $ext . "";
        //specify your desired width for your thumbnails
        $width = "282";
        //Finally call the make_thumb function
        make_thumb($source, $destination, $width);

        $msg = '<div class="success">
                    <b>Upload: </b>' . basename($photoname) . '<br />
                    <b>Type: </b>' . $_FILES["file_upload"]["type"] . '<br />
                    <b>Size: </b>' . ceil(($_FILES["file_upload"]["size"] / 1024)) . 'Kb<br />
                </div>';
    } else {
        $msg = '<div class="error">Sorry, there was a problem uploading your file.</div>';
    }
} else {
    $msg = '<div class="error">The file type you are trying to upload is not allowed!</div>';
}
}

else {
echo "ERROR";
}
 ?>

1 个答案:

答案 0 :(得分:0)

在处理上传时,您应该始终使用文件的路径而不是相对路径。

$target = "../uploads/";

它应包含文件夹的路径,因为这是执行命令的脚本的相对路径。 (根据目标文件夹的确切位置,这样的东西。)

$target = $_SERVER['DOCUMENT_ROOT'].'/uploads/';

此外,此文件也应具有路径:

$destination = $_SERVER['DOCUMENT_ROOT']."/thumbnails/thumb_" . $maxId . $ext . "";

此外,这不应包含@符号,它应该是文件的名称,因此您可以从$_FILES数组中提取它:

$pimg = $_FILES['file_upload']['name'];

对于测试,您可以打印$_FILES数组的输出,以确保在更改后问题仍然存在时捕获正确的数组键(索引)字段。由于未定义的索引错误意味着您尝试提取的值不在数组中。

print_r($_FILE);