1

我已经让会话显示子信息,我现在希望能够编辑该信息并更新数据库。我已经尝试了每个 youtube 视频和网站,但没有使用 $_SESSION 他们都使用 $_POST。

<div class="post">
  <h1 class="title">Child Details: </h1>
    <p class="title"><img src=" <?php echo "".$_SESSION['sourcepath']; ?>" 
    </p>
    <p class="title"><?php echo "".$_SESSION['ChildID']; ?></p>

    <table style="width: 100%">
        <tr>
            <td style="width: 106px">Name</td>
            <td style="width: 252px"><?php       echo "".$_SESSION            ['Firstname']; ?> <?php echo "".$_SESSION['Surname']; ?></td>
            <td style="width: 94px">School</td>
            <td><form method="post">
                <br />
                <textarea name="School" cols="20" rows="5"><?php echo "".$_SESSION['School']; ?></textarea></form>
            </td>
        </tr>
        <tr>
            <td style="width: 106px">Date of Birth</td>
            <td><?php echo "".$_SESSION['DateOfBirth']; ?></td>
            <td style="width: 94px">English</td>
            <td><form method="post">
                <br />
                <textarea name="English" cols="20" rows="5"><?php echo "".$_SESSION['English']; ?></textarea></form>
            </td>
        </tr>
        <tr>
            <td style="width: 106px">Age</td>
            <td><?php echo "".$_SESSION['Age']; ?>;</td>
            <td style="width: 94px">Science</td>
            <td><form method="post">
                <br />
                <textarea name="Science" cols="20" rows="5"><?php echo "".$_SESSION['Science']; ?></textarea></form>
            </td>
        </tr>
        <tr>
            <td style="width: 106px">Address</td>
            <td><form method="post">
                <br />
                <textarea name="Address" cols="20" style="height: 89px"><?php echo "".$_SESSION['Address']; ?></textarea></form>
            </td>
            <td style="width: 94px">Maths</td>
            <td><form method="post">
                <br />
                <textarea name="Maths" cols="20" rows="5"><?php echo "".$_SESSION['Maths']; ?></textarea></form>
            </td>
        </tr>
        <tr>
            <td style="width: 106px">Postcode:</td>
            <td><?php echo "".$_SESSION['PostCode']; ?>;</td>
            <td style="width: 94px">Homework</td>
            <td><form method="post">
                <br />
                <textarea name="Homework" cols="20" rows="5"><?php echo "".$_SESSION['Homework']; ?></textarea></form>
            </td>
        </tr>
        <tr>
            <td style="width: 106px">Contact Number</td>
            <td><form method="post">
                <textarea name="ContactNumber" cols="20" rows="2"><?php echo "".$_SESSION['ContactNumber']; ?></textarea></form>
            </td>
            <td style="width: 94px">Additional</td>
            <td><form method="post">
                <br />
                <textarea name="Additional" cols="20" rows="8"><?php echo "".$_SESSION['Additional']; ?></textarea></form>
            </td>
        </tr>
        <tr>
            <td style="width: 106px">Mother Name</td>
            <td><?php echo "".$_SESSION['MotherName']; ?></td>
            <td style="width: 94px">&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td style="width: 106px">&nbsp;</td>
            <td>&nbsp;</td>
            <td style="width: 94px">&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td style="width: 106px">Last Update</td>
            <td><?php echo "".$_SESSION['TimeStamp']; ?></td>
            <td style="width: 94px">&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td style="width: 106px">&nbsp;</td>
            <td>&nbsp;</td>
            <td style="width: 94px">&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td style="width: 106px">



            </td>
            <td>
            <form method="post" action="updatetest.php">
                <input type="hidden" name="id" value="<?php echo $_SESSION['ChildID']; ?>"/>
                <input name="Submit" type="submit" value="Update" /></form>
            </td>







<?php session_start(); ?>
    <?php       
    $connect = mysql_connect("127.0.0.1" , "root" , "") or die ("Couldnt connect     to            database");
    mysql_select_db("travellerfile") or die ("couldnt find the database");

    $School = $_SESSION['School'];
    $Maths = $_SESSION['Maths'];
    $English = $_SESSION['English'];
    $Science = $_SESSION['Science'];
    $Homework = $_SESSION['Homework'];
    $Additional = $_SESSION['Additional'];
    $id = $_SESSION['ChildID'];

    $q = "SELECT * FROM child WHERE ChildID = $_SESSION[ChildID]";
    $result = mysql_query($q);
    $person = mysql_fetch_array($result);

    $u = "UPDATE child SET Maths= '$_SESSION['Maths']', Science= '$_SESSION['Science']';
?>
4

4 回答 4

1
$_SESSION['School'] = $_POST['School'];
$School = $_SESSION['School'];

您必须在某处声明 $_SESSION['School'] 包含名称为“School”的 Textarea 的值。您不能只期望 PHP 将 POST 变量放入 SESSION 变量中

于 2013-04-04T11:58:16.943 回答
1

例如:

  $_SESSION['ChildID'] = 5;

 <form method="post" action="updatetest.php">
 <input type="hidden" name="id" value="<?php echo $_SESSION['ChildID']; ?>"/>
 <input name="Submit" type="submit" value="Update" /></form>

这将向您发布:

 $_POST['id'] = 5;

所以:

 UPDATE table SET col = $_POST['id'];
于 2013-04-04T11:59:22.317 回答
0

Both $_POST and $_SESSION are merely arrays. The only thing that makes them special is that they are global. So, if you have code that works for $_POST, just substitute in the corresponding $_SESSION values.

What your real issue is, is that you are submitting a form. Forms can submit using get or post methods. There is no session method. So your values are arriving at your php script in the $_POST variable. You could then copy them to the $_SESSION variable before running your update, but there's really no good reason to. The only reason they would need to be in $_SESSION is so that they can be output if the form needs to be re-displayed with the submitted values.

于 2013-04-04T12:12:35.713 回答
0

更新数据库的$_SESSION最终抓取$_POST

...
$School = $_['School'] = $_POST['School'];
$Maths = $_SESSION['Maths'] = $_POST['Maths'];
$English = $_SESSION['English'] = $_POST['English'];
$Science = $_SESSION['Science'] = $_POST['Science'];
$Homework = $_SESSION['Homework'] = $_POST['Homework'];
$Additional = $_SESSION['Additional'] = $_POST['Additional'];
$id = $_SESSION['ChildID'];
...
于 2013-04-04T12:03:14.233 回答