質問

I'm creating an update page for the students who wants to update or edit their information in their profile.. When they edit/update their record i need to validate.. My validation is working properly but it does not save in the database..

<?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");  
}   
}
?>

In common.php, includes session_start(); and everything. I just wonder why, if i update/edit the record it does not save in the database and no display in the next page where their profile is.

正しい解決策はありません

他のヒント

return ends execution of a function. You're returning in the validate() function before you execute the query:

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");  
}

The variables are not set in your function. Please see Variable Scope

You need to pass the variables into the function to use them. Also when calling return in your function it immediately stops the execution of that function. Your update is never triggered.

PHP Return

Not sure what the variable $data holds. And I do not see the call to the validate function

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(); 
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top