2013-05-06 66 views
0

這裏是我的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 = /^[email protected]+\..{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> 

這裏是我的數據庫連接代碼,我不知道爲什麼它沒有存儲

:在數據庫中,但只是展示

我試過很多次,但它不會工作,請幫我找出錯誤一些空白頁的登記表格數據

這裏是與數據庫的連接:

 $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"; 
} 
?> 
+3

您絕不應該在Web應用程序中使用'root'帳戶進行數據庫連接。 – 2013-05-06 06:40:14

+0

打開錯誤並檢查您收到的錯誤。 – Rikesh 2013-05-06 06:40:41

+0

你是否收到任何錯誤? – 2013-05-06 06:42:02

回答

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); 
+0

但如果我刪除@symbol它顯示一些通知作爲未定義索引。 – Ganesh 2013-05-06 06:46:38

+0

$ _SERVER ['DOCUMENT_ROOT']。'/ uploads /';我試過這個,但表單已被更改,沒有顯示提交按鈕... – Ganesh 2013-05-06 06:52:00

+0

這應該是你正在處理文件的路徑。我已經更新了我的回覆。 – 2013-05-06 06:53:14

相關問題