在数据作为会话传递时更新mysql数据库

时间:2013-04-04 11:52:40

标签: php mysql database session

我已经让会话工作以显示子信息,我现在希望能够编辑该信息并更新数据库。我已经尝试了每个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 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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'];

答案 2 :(得分: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'];
...

答案 3 :(得分:0)

$_POST$_SESSION都只是数组。使它们与众不同的唯一因素是它们是全球性的。因此,如果您的代码适用于$_POST,则只需替换相应的$_SESSION值。

您真正的问题是,您提交的是表格。表单可以使用getpost方法提交。没有session方法。因此,您的值将在$_POST变量中到达您的php脚本。然后,您可以在运行更新之前将它们复制到$_SESSION变量,但实际上并没有充分的理由。他们需要在$_SESSION中的唯一原因是,如果需要使用提交的值重新显示表单,则可以输出它们。