-4

I've a problem with my admin web page, wicth doesn't work when i need to do an user's update. That is my main page, where the page should show user's details on selection form:

<?php
if(isset($_POST['submit']))
{
    session_start();
    $data = $_SESSION['data'];
    unset($_GET['data']);
    header("location: modify.php");
}
?>

<html>
<head>
    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'>
        function showUser(str) {
            if (str == "") {
                document.getElementById("txtHint").innerHTML = "";
                return;
            } else {
                if (window.XMLHttpRequest) {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                } else {
                    // code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        document.getElementById("txtHint").innerHTML = this.responseText;
                    }
                };
                xmlhttp.open("GET","getuser.php?q="+str,true);
                xmlhttp.send();
            }
        }
    </script>
</head>
<body>

    <form action="" method="post"> 
    <select name="users" onchange="showUser(this.value)">
      <option value="">Select a person:</option>
      <?php
        require_once('../conf.php');
        $query_list = "SELECT * FROM studentinfo ORDER BY(id) DESC";
        $result_list = mysql_query($query_list) or die('error in query_list');
    //  $n=1;
        while($row = mysql_fetch_array($result_list))
        {
        ?>
        <option value="<?php echo $row[0]; ?>"><?php echo $row[2]; ?></option>
        <?php
    //  $n=$n+1;
        }   
        ?> 
      </select>
    <input type="submit" name="submit"/>
     </form> 
    <br>
    <div id="txtHint">
        <b>listed here...</b>
    </div>
</body>
</html>

there is my getuser.php page:

<html>
<head>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }

        table, td, th {
            border: 1px solid black;
            padding: 5px;
        }

        th {text-align: left;}
    </style>
</head>
<body>

    <?php
        require_once('../conf.php');
        session_start();
        $q = intval($_GET['q']);
        $getuser="SELECT * 
                FROM studentinfo 
                WHERE id = '".$q."'";
        $result = mysql_query($getuser) or die('error in getuser');
        echo "<table>";
        ?>
        <tr>
        <th>Id</th>
        <th>Mac</th>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
        <th>Department</th>
        <th>City</th>
        <th>State</th>
        </tr>
        <?php
        if($row = mysql_fetch_array($result)) {
            $_SESSION['data'] = $row;
            echo "<tr>";
            echo "<td>" . $row[0] . "</td>";
            echo "<td>" . $row[1] . "</td>";
            echo "<td>" . $row[2] . "</td>";
            echo "<td>" . $row[3] . "</td>";
            echo "<td>" . $row[4] . "</td>";
            echo "<td>" . $row[5] . "</td>";
            echo "<td>" . $row[6] . "</td>";
            echo "<td>" . $row[7] . "</td>";
            echo "</tr>";   
        }
        echo "</table>";
    ?>
</body>
</html>

at the end my modify.php page:

<html>
<head>
    <style>
    table {
        width: 100%;
        border-collapse: collapse;
    }

    table, td, th { 
        border: 1px solid black;
        padding: 5px;
    }

    th {text-align: left;}
    </style>
</head>
<body>
    <?php
    require_once('../conf.php');
    session_start();
    $data = $_SESSION['data'];
    //clear get data
    if(isset($_POST['submitbutton']))
    {
            echo "11111111";
            $edit="UPDATE TABLE studentinfo 
                    SET id=$data[0], 
                        mac='$_POST['Mac']', 
                        firstname='$_POST['FirstName']',
                        lastname='$_POST['LastName']', 
                        email='$_POST['Email']', 
                        department='$_POST['Department']', 
                        city='$_POST['City']', 
                        state='$_POST['State']'     
                    WHERE id=$data[0]";
            $result_edit=mysql_query($edit) or die ('edit user failed');
            if(!$result_edit)
                    echo "edit done";
            else
                    echo "edit failed";
    }


    //if(ISSET($_POST['submit']))

    ?>
    <form action="" type="post">
        <table>
            <tr>
                <th>Mac</th>
                <th>FirstName</th>
                <th>LastName</th>
                <th>Email</th>
                <th>Department</th>
                <th>City</th>
                <th>State</th>
                </tr>
            <tr>


                <td>
                <input type="text" name="Mac" value="prova"></input>
                </td>
                <td>
                <input type="text" name="FirstName" value="<?php echo htmlspecialchars($data['firstname']); ?>" />
                </td>
                <td>
                <input type="text" name="LastName" value="<?php echo $data[3]; ?>" />
                </td>
                <td>
                <input type="text" name="Email" value="<?php echo $data[4]; ?>" />
                </td>
                <td>
                <input type="text" name="Department" value="<?php echo $data[5]; ?>" /></td>
                <td>
                <input type="text" name="City" value="<?php echo $data[6]; ?>" />
                </td>
                <td>
                <input type="text" name="State" value="<?php echo $data[7]; ?>" />
                </td>
                <td>
                <input type="submit" name="submitbutton" />
                </td>

            </tr>
        </table>
    </form>
</body>
</html>

Now, it looks to work well until the modify page. i can't understand why in modify.php page i can't save variable on POST for the mysql upload. can someone help my to understant why don't work pls. Thanks.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Look up how to use Sessions properly. The `start_session()` needs to be done before ANYTHING is sent to the output buffer for the browser – RiggsFolly May 10 '17 at 12:59
  • Your input button has no name property did you try putting – Oscar Gallardo May 10 '17 at 12:59
  • `
    ` ... Errrr... *huh?!* That will so not work.
    – Funk Forty Niner May 10 '17 at 13:00
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly May 10 '17 at 13:01
  • 1
    **Way too many errors in this code** An answer would require a complete rewrite and it therefore off topic – RiggsFolly May 10 '17 at 13:02
  • `header("location: modify.php")` - the browser will follow this redirect by making a GET request for modify.php, and therefor you lose all POST data. – CBroe May 10 '17 at 13:02
  • Outputting before header....... the list goes on and on and on............. – Funk Forty Niner May 10 '17 at 13:02

1 Answers1

0

form has no action

<form action="" method="post"> 

change too

<form action="somescript.php" method="post"> 

need to set action to some php script to get the post data

user3005775
  • 306
  • 1
  • 3
  • 14
  • i think its this line specifically – user3005775 May 10 '17 at 13:01
  • Can I suggest that you put your answer in the Answer box, and not across multiple comments after an answer that is hardly an answer – RiggsFolly May 10 '17 at 13:04
  • Also note this line in the OP's code ` – RiggsFolly May 10 '17 at 13:05
  • well with ajax it would change xmlhttp.open("GET","getuser.php?q="+str,true); to xmlhttp.open("POST","getuser.php?q="+str,true);, but this wont solve the OPs problem of getting both get and post data from the same submission, since they have a submit button on the form, i just assume they keep the GET in tthe xmlhttp request, and use an action for the form – user3005775 May 10 '17 at 13:09