$_SERVER['PHP_SELF'];

Alles, was PHP betrifft, kann hier besprochen werden.

$_SERVER['PHP_SELF'];

Postby dennisl » 09. July 2009 14:09

Hi,

I would like to know whether "$_SERVER['PHP_SELF'];" can be used more than one time in a script? This is because I have 2 forms in a script therefore I used "self reference" twice but it is not working.

Thank you.
dennisl
 
Posts: 10
Joined: 06. July 2009 11:23

Re: $_SERVER['PHP_SELF'];

Postby Nobbie » 09. July 2009 14:18

>I would like to know whether "$_SERVER['PHP_SELF'];" can be used more than one time in a script?

Yes. As often as you want.

>This is because I have 2 forms in a script therefore I used "self reference" twice but it is not working.

What means "is not working"? You should show us the script and the error messages or whatever is going wrong.
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: $_SERVER['PHP_SELF'];

Postby dennisl » 10. July 2009 09:28

Hi thanks for your reply,

The objective of this script is to let user to first select a particular Cient ID, then the client profile should be generated automatically. The user can edit and update the client profile.

It seems that everything is fine except that when I try to edit and then click on the 'UPDATE' button, no changes are made.

The below is my script, thanks:

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Member Search</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>Search Members' Profile</h2>

<?php
require_once('db_connection.php');

    if (isset($_POST['submit'])) {

    // Grab the score data from the POST
    $target = $_POST['target'];


    //Make sure the search term is not empty
    if (!empty($target))  {

    // Connect to the database
    $dbc = mysqli_connect(db_host, db_un, db_psw, db_name) or die('Error to connect MySQL');

    // perform search query           
    $query = "SELECT * FROM members WHERE id = '$target'";
    $data = mysqli_query($dbc, $query);
   

    //Loop through the array
    echo '<table>';       
    while($row = mysqli_fetch_array($data)) {
   
    //Display the Members' Profile
   
   
    echo '<form enctype="multipart/form-data" method="post" action="' . $_SERVER['PHP_SELF'] . '">';
    echo '<tr><td>Name:</td><td><input type="text" id="name" name="name" value="' . $row['name'] . '" /></td></tr>';


   
   
    echo '</table>';
    echo '<input type="submit" value="UPDATE" name="update" />';
    echo '</form>';


    if (isset($_POST['update'])) {


    // Grab the update data from the POST
    $name = $_POST['name'];


    // Update Data           
    $query = "UPDATE members SET name = '$name' WHERE id = '$target'";
    mysqli_query($dbc, $query);

    echo 'Changes are made.';

   
   


    mysqli_close($dbc);
    }
    }


    }
   
    else {
    echo 'Error! You must enter search term.';
    }
    }
   
   


?>




<hr />
  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <table>
    <tr>
    <td>
    Target ID:
    </td>
    <td>
    <input type="text" id="target" name="target" value="<?php if (!empty($target)) echo $target; ?>" />
    </td>
    </tr>




</table>
    <hr />
    <input type="submit" value="Search" name="submit" />
  </form>
</body>
</html>
dennisl
 
Posts: 10
Joined: 06. July 2009 11:23

Re: $_SERVER['PHP_SELF'];

Postby Nobbie » 10. July 2009 10:15

The problem ist not $_SERVER['PHP_SELF'], the problem is your script. It is completely wrong program logic and cannot work that way:

You have an if-clause if (isset($_POST['submit'])) { and inside that if-clause you evaluate the same $_POST array for 'update' if (isset($_POST['update'])) { - this is definately a contradiction and never TRUE, as both refer to a submit button (you can only press once at a time).

It looks like you have a wrong idea about FORMS and HTTP Requests are working. I think you should go for some tutorials and/or a good documentian, how HTTP is working. I cannot give you an explanation here, because it's quite a lot of stuff and obviously you are struggling with the basic concepts of HTTP.
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: $_SERVER['PHP_SELF'];

Postby Xardas der Dunkle » 10. July 2009 11:02

The 2nd argument ist you have to clean up your code ...
Indenting ist one of the most important things. Because you can better read the code and can better find forgotten brackets, for example.

I have cleaned up your code and moved the update-section to the correct position:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Member Search</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>Search Members' Profile</h2>

<?php
require_once('db_connection.php');

if (isset($_POST['submit'])) {
    // Grab the score data from the POST
    $target = $_POST['target'];

    //Make sure the search term is not empty
    if (!empty($target))  {
        // Connect to the database
        $dbc = mysqli_connect(db_host, db_un, db_psw, db_name) or die('Error to connect MySQL');

        // perform search query
        $query = "SELECT * FROM members WHERE id = '$target'";
        $data = mysqli_query($dbc, $query);

        //Loop through the array
        echo '<table>';

        while($row = mysqli_fetch_array($data)) {
            //Display the Members' Profile
            echo '<form enctype="multipart/form-data" method="post" action="' . $_SERVER['PHP_SELF'] . '">';
            echo '<tr><td>Name:</td><td><input type="text" id="name" name="name" value="' . $row['name'] . '" /></td></tr>';

            echo '</table>';
            echo '<input type="submit" value="UPDATE" name="update" />';
            echo '</form>';
        }
    } else {
        echo 'Error! You must enter search term.';
    }
}

if (isset($_POST['update'])) {
    // Grab the update data from the POST
    $name = $_POST['name'];

    // Update Data
    $query = "UPDATE members SET name = '$name' WHERE id = '$target'";
    mysqli_query($dbc, $query);

    echo 'Changes are made.';
    mysqli_close($dbc);
}
?>
<hr />
  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <table>
    <tr>
    <td>
    Target ID:
    </td>
    <td>
    <input type="text" id="target" name="target" value="<?php if (!empty($target)) echo $target; ?>" />
    </td>
    </tr>
</table>
    <hr />
    <input type="submit" value="Search" name="submit" />
  </form>
</body>
</html>
User avatar
Xardas der Dunkle
 
Posts: 482
Joined: 09. March 2008 19:40
Location: /var/www

Re: $_SERVER['PHP_SELF'];

Postby dennisl » 10. July 2009 15:34

Hi, thanks for your advice.

I am indeed a newbie. Currently, struggling with PHP and MySQL. I have bought PHP and MySQL books to read and test it by myself.

In that case, I shall try to amend my script again and break it down into 2 separate scripts with the help from GET and see whether it works.

Thanks a lot.
dennisl
 
Posts: 10
Joined: 06. July 2009 11:23

Re: $_SERVER['PHP_SELF'];

Postby dennisl » 13. July 2009 11:28

Hi,

Good day to you.

You have told me that we can't have 2 times of 'submit' in a form, so I have separated 2 forms into 2 separate scripts as follow:

This is search.php where users can pick an ID and then click "submit" button to generate a link. This link will pass all the information to update_profile.php via GET.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Member Search</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>Search Members' Profile</h2>

<?php
require_once('db_connection.php');

    if (isset($_POST['submit'])) {

    // Grab the score data from the POST
    $target_id = $_POST['target_id'];

    //Make sure the search term is not empty
    if (!empty($target_id))  {

    // Connect to the database
    $dbc = mysqli_connect(db_host, db_un, db_psw, db_name) or die('Error to connect MySQL');

    // perform search query           
    $query = "SELECT * FROM members WHERE id = '$target_id'";
    $data = mysqli_query($dbc, $query);
   

    //Loop through the array
    echo '<table>';       
    while($row = mysqli_fetch_array($data)) {
   
    //Display the Members' Profile
   
    echo '<tr><td>ID:</td><td>' . $row['id'] . '</td></tr>';
    echo '<tr><td>Name:</td><td>' . $row['name'] . '</td></tr>';
    echo '<tr><td>&nbsp;</td></tr>';
    echo '<tr><td>&nbsp;</td></tr>';
    echo '<tr><td>&nbsp;</td></tr>';
    echo '<tr><td><a href="update_profile.php?id=' . $row['id'] . '&amp;name=' . $row['name'] . '&amp;nric=' . $row['nric'] . '&amp;age=' . $row['age'] . '&amp;dob_date=' . $row['dob_date'] . '&amp;dob_month=' . $row['dob_month'] . '&amp;dob_year=' . $row['dob_year'] . '&amp;gender=' . $row['gender'] . '&amp;race=' . $row['race'] . '&amp;other_race=' . $row['other_race'] . '&amp;religion=' . $row['religion'] . '&amp;other_religion=' . $row['other_religion'] . '&amp;nationality=' . $row['nationality'] . '&amp;other_nationality=' . $row['other_nationality'] . '&amp;occupation=' . $row['occupation'] . '&amp;address1=' . $row['address1'] . '&amp;address2=' . $row['address2'] . '&amp;address3=' . $row['address3'] . '&amp;address4=' . $row['address4'] . '&amp;post_code=' . $row['post_code'] . '&amp;state=' . $row['state'] . '&amp;house_area_code=' . $row['house_area_code'] . '&amp;house_numbers=' . $row['house_numbers'] . '&amp;office_area_code=' . $row['office_area_code'] . '&amp;office_numbers=' . $row['office_numbers'] . '&amp;hp_numbers=' . $row['hp_numbers'] . '&amp;spouse=' . $row['spouse'] . '">Click here to edit Profile</a></td></tr>';

   
   
    echo '</table>';

    // Clear the score data to clear the form
    $target_id = "";


    mysqli_close($dbc);
    }


    }
   
    else {
    echo 'Error! You must enter search term.';
    }
    }
   
   


?>




<hr />
  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <table>
    <tr>
    <td>
    Target ID:
    </td>
    <td>
    <input type="text" id="target_id" name="target_id" value="<?php if (!empty($target_id)) echo $target_id; ?>" />
    </td>
    </tr>




</table>
    <hr />
    <input type="submit" value="Search" name="submit" />
  </form>
</body>
</html>



This is update_profile.php where all the current data will be displayed into the text fields, users can edit and click "update" button to update the profile.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Update Members' Data</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>Update Members' Profile</h2>



<?php
require_once('db_connection.php');

if (isset($_GET['id']) && isset($_GET['name'])) {
//Grab data from GET

$id = $_GET['id'];
$name = $_GET['name'];


    if (isset($_POST['update'])) {

    // Grab the score data from the POST
    $new_name = $_POST['new_name'];


    //Make sure no terms are left not empty
    if (!empty($new_name)) {

    // Connect to the database
    $dbc = mysqli_connect(db_host, db_un, db_psw, db_name) or die('Error to connect MySQL');

    // Update Data           
    $query = "UPDATE members SET name = '$new_name' WHERE id = '$id'";
    mysqli_query($dbc, $query);

    echo 'Changes are made.';
   

    // Clear the score data to clear the form
    $new_name = "";


    mysqli_close($dbc);
    }
   
    else {
    echo 'Error! No changes are made.';
    }
    }
}

?>






<hr />
  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">


    <table>
    <tr>
    <td>
    Name:
    </td>
    <td>
    <input type="text" id="new_name" name="new_name" value="<?php if (!empty($name)) echo $name; ?>" />
    </td>
    </tr>


 


</table>
    <hr />
    <input type="submit" value="Update" name="update" />
  </form>
</body>
</html>


Again, my problem is everything works fine except that the information is not changed even after I have edited and clicked the "update" button.

Am I doing something terribly wrong again?
I am searching for a lot of tutorial and book.
I just wish to generate a simple edit profile page as my boss told me to do so even I am not working in a web hosting/software firm.

Sorry for any inconvenience caused to you.
Thank you.
dennisl
 
Posts: 10
Joined: 06. July 2009 11:23

Re: $_SERVER['PHP_SELF'];

Postby Xardas der Dunkle » 13. July 2009 13:17

I please you again ... indent your code.

I don't think that this:
Code: Select all
        //Loop through the array
        echo '<table>';       
        while
($row = mysqli_fetch_array($data)) {
[...]
            // Clear the score data to clear the form
            $target_id = "";
            mysqli_close($dbc);
        } 

is correct.

I think it must be something like this:
Code: Select all
        //Loop through the array
        echo '<table>';       
        while
($row = mysqli_fetch_array($data)) {
[...]
        }

        // Clear the score data to clear the form
        $target_id = "";
        mysqli_close($dbc); 


And why enctype="multipart/form-data" ? you don't need this.

Your update-script, it can't work because of this:
Code: Select all
if (isset($_GET['id']) && isset($_GET['name'])) { 


When you submit your update-form this parameters are not set.
You have to do something like this:
Code: Select all
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] . '?id=' . intval($id) . '&amp;name=' . htmlspecialchars($name); ?>">

Or so:
Code: Select all

$id 
= 0;
$name = '';
if (isset($_REQUEST['id']) && isset($_REQUEST['name'])) {
      //Grab data from REQUEST
      $id = $_REQUEST['id']; 
      $name 
= $_REQUEST['name']; 

Code: Select all
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
     <input type="hidden" name="id" value="<?php echo intval($id); ?>" />
     <input type="hidden" name="name" value="<?php echo htmlspecialchars($name); ?>" />


My last point is the security. You should escape the values:
Code: Select all
            $query = "UPDATE members SET name = '" . mysql_real_escape_string($new_name) . "' WHERE id = " . intval($id);


mfG
User avatar
Xardas der Dunkle
 
Posts: 482
Joined: 09. March 2008 19:40
Location: /var/www

Re: $_SERVER['PHP_SELF'];

Postby dennisl » 13. July 2009 14:23

Hi Xardas,

Thanks for your valuable information. ;)

I am sorry that I didn't indent the code. This is due to I wrote my scripts with notepad actually. We do not have tools like dreamweaver ....

I wish to clarify a few minor issues based on your answers given to me.

Code: Select all
if (isset($_GET['id']) && isset($_GET['name'])) {

1) So, meaning I shall get rid of the above code from the script, am I right?


Code: Select all
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] . '?id=' . intval($id) . '&amp;name=' . htmlspecialchars($name); ?>">

2) What if I want to use more information such as age, race, religion, address.....etc? Is it using intval for numbers and htmlspecialchars for alphabet letters as I plug in more information into it?

Code: Select all
enctype="multipart/form-data"

3) Shall I get rid of the above code from update_profile.php as well?

I shall edit both search.php and update_profile.php to see the result.
Thanks and sorry to take up much of your time.
dennisl
 
Posts: 10
Joined: 06. July 2009 11:23

Re: $_SERVER['PHP_SELF'];

Postby Xardas der Dunkle » 13. July 2009 14:57

dreamweaver

You don't need dreamweaver ...

1) So, meaning I shall get rid of the above code from the script, am I right?

You could, but you don't have to. You only have to ensure that the two GET-Parameters are set after you submit the form.

2) What if I want to use more information such as age, race, religion, address.....etc? Is it using intval for numbers and htmlspecialchars for alphabet letters as I plug in more information into it?

o_O. No. You have to insert here only the two GET-Parameters, which you need for the if-condition in the top of your code.
User avatar
Xardas der Dunkle
 
Posts: 482
Joined: 09. March 2008 19:40
Location: /var/www


Return to PHP

Who is online

Users browsing this forum: No registered users and 17 guests