2014-03-06 49 views
-1

我創建的更新頁面,誰願意更新或他們的個人資料編輯信息的學生不保存。當他們編輯/更新他們的記錄,我需要驗證..我的驗證正常工作,但它並沒有在數據庫中保存..驗證工作,但在數據庫

<?php 
// First we execute our common code to connection to the database and start the session 
    require("common.php"); 

    // At the top of the page we check to see whether the user is logged in or not 
    if(empty($_SESSION['user'])) 
    { 
     // If they are not, we redirect them to the login page. 
     header("Location: login.php"); 

     // Remember that this die statement is absolutely critical. Without it, 
     // people can view your members-only content without logging in. 
     die("Redirecting to login.php"); 
    } 

    // Everything below this point in the file is secured by the login system 

    // We can display the user's username to them by reading it from the session array. Remember that because 
    // a username is user submitted content we must use htmlentities on it before displaying it to the user. 
    // Database Variables (edit with your own server information) 

     $server = 'localhost'; 
     $user = 'root'; 
     $pass = ''; 
     $db = 'testing'; 

     // Connect to server and select databse. 
     mysql_connect("$server", "$user", "$pass")or die("cannot connect"); 
     mysql_select_db("$db")or die("cannot select DB"); 

$sql ="SELECT * FROM users_info WHERE username = '".$_SESSION['user']['username']."' "; 
$result=mysql_query($sql); 

if($result === FALSE) { 
    die(mysql_error()); // TODO: better error handling 
} 

    // define variables and set to empty values 
$nameErr = $addressErr = $ageErr = $cellnoErr = $emailErr = $fathers_nameErr = $f_occupationErr = $mothers_nameErr = $m_occupationErr = ""; 
$name = $address = $age = $cellno = $telno = $email = $fathers_name = $f_occupation = $mothers_name = $m_occupation = ""; 

while($rows=mysql_fetch_array($result)){ 
$test=mysql_fetch_array($result); 

if(!$result) 
     { 
     die("Error: Data not found.."); 
     }  
       $name = $test['name']; 
       $address = $test['address']; 
       $age = $test['age']; 
       $cellno = $test['cellno']; 
       $telno = $test['telno']; 
       $email = $test['email']; 
       $fathers_name = $test['fathers_name']; 
       $f_occupation = $test['f_occupation']; 
       $mothers_name = $test['mothers_name']; 
       $m_occupation = $test['m_occupation']; 
} 

if (isset($_POST['save'])) 
{ 
    if (empty($_POST["name"])) 
    {$nameErr = "Name is required";} 
    else 
{ 
$name = test_input($_POST["name"]); 
// check if name only contains letters and whitespace 
if (!preg_match("/^[a-zA-Z ]*$/",$name)) 
    { 
    $nameErr = "Only letters and white space allowed"; 
    } 
} 

    if (empty($_POST["address"])) 
    {$addressErr = "Address is required";} 
    else 
    { 
    $address = ($_POST["address"]); 
     } 

    if (empty($_POST["age"])) 
    {$ageErr = "Age is required";} 

    if (empty($_POST["cellno"])) 
    {$cellnoErr = "Cellphone Number is required";} 

    if (empty($_POST["email"])) 
    {$emailErr = "Email is required";} 
if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) 
     { 
     $emailErr = "Invalid email format"; 
     } 

    if (empty($_POST["fathers_name"])) 
    {$fathers_nameErr = "Father's Name is required";} 
    if(!preg_match("/^[a-zA-Z ]*$/",$fathers_name)) 
     { 
     $fathers_nameErr = "Only letters and white space allowed"; 
     } 

    if (empty($_POST["f_occupation"])) 
    {$f_occupationErr = "Father's Occupation is required";} 
    if(!preg_match("/^[a-zA-Z ]*$/",$fathers_name)) 
     { 
     $fathers_nameErr = "Only letters and white space allowed"; 
     } 

    if (empty($_POST["mothers_name"])) 
    {$mothers_nameErr = "Mother's Name is required";} 
    if(!preg_match("/^[a-zA-Z ]*$/",$mothers_name)) 
     { 
     $mothers_nameErr = "Only letters and white space allowed"; 
     } 

    if (empty($_POST["m_occupation"])) 
    {$m_occupationErr = "Mother's Occupation is required";} 
    if(!preg_match("/^[a-zA-Z ]*$/",$m_occupation)) 
     { 
     $m_occupationErr = "Only letters and white space allowed"; 
     } 

function validate($data) 
{ 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 


    mysql_query ("UPDATE `users_info` SET `name` ='$name', `address` ='$address',`age` ='$age', `cellno` ='$cellno' , `telno` ='$telno', `email` ='$email', `fathers_name` ='$fathers_name', `f_occupation` ='$f_occupation', `mothers_name` ='$mothers_name', `m_occupation` ='$m_occupation' WHERE username = '".$_SESSION['user']['username']."' ") or die(mysql_error()); 

    header("Location: myprofile.php"); 
} 
} 
?> 

common.phpincludes session_start();,應有盡有。我只是想知道爲什麼,如果我更新/編輯記錄,它不會保存在數據庫中,並且在他們的配置文件下一頁中沒有顯示。

回答

0

return結束的功能的執行。你在validate()函數返回執行查詢之前:

function validate($data) 
{ 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
    // Doesn't go any further... 

    mysql_query ("UPDATE `users_info` SET `name` ='$name', `address` ='$address',`age` ='$age', `cellno` ='$cellno' , `telno` ='$telno', `email` ='$email', `fathers_name` ='$fathers_name', `f_occupation` ='$f_occupation', `mothers_name` ='$mothers_name', `m_occupation` ='$m_occupation' WHERE username = '".$_SESSION['user']['username']."' ") or die(mysql_error()); 

    header("Location: myprofile.php"); 
} 
+0

Hello Marcus!我已經擦除了'return $ data;',但它保持在同一頁面上。 – user3389082

0

的變量不是在你的功能設置。請參閱Variable Scope

您需要的變量傳遞給函數使用它們。另外,在函數中調用return時,它立即停止執行該函數。您的更新永遠不會被觸發。

PHP Return

不知道什麼是變量$的數據保存。並且我看不到驗證功能的呼叫

function validate($data, $test) 
{ 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 

    $name = $test['name']; 
    $address = $test['address']; 
    $age = $test['age']; 
    $cellno = $test['cellno']; 
    $telno = $test['telno']; 
    $email = $test['email']; 
    $fathers_name = $test['fathers_name']; 
    $f_occupation = $test['f_occupation']; 
    $mothers_name = $test['mothers_name']; 
    $m_occupation = $test['m_occupation']; 

    mysql_query ("UPDATE `users_info` SET `name` ='$name', `address` ='$address',`age` ='$age', `cellno` ='$cellno' , `telno` ='$telno', `email` ='$email', `fathers_name` ='$fathers_name', `f_occupation` ='$f_occupation', `mothers_name` ='$mothers_name', `m_occupation` ='$m_occupation' WHERE username = '".$_SESSION['user']['username']."' ") or die(mysql_error()); 

    header("Location: myprofile.php"); 
    exit(); 
} 
+0

Hello Mathius!所以我需要在函數內聲明我的變量,像這樣'$ name =「」; $ address =「」;'? – user3389082

+0

我編輯了我的答案以顯示傳入變量的示例。你在哪裏調用函數驗證?另外一方面,您不應該使用http://www.php.net/mysql_real_escape_string來轉義您的查詢。你永遠不知道人們會用表格發佈什麼。 – mathius1

+0

我試過你的答案mathius,但是當我保存它時,它保留在頁面上,我的所有輸入仍然在文本字段上。我的'功能的validate();'改爲'funtion test_input();' – user3389082