2013-04-04 59 views
1

我已經有會話工作來顯示子信息,我現在想要能夠編輯該信息並更新數據庫。我試過每個YouTube視頻和網站,但沒有使用$ _SESSION,它們都使用$ _POST。當數據作爲會話傳遞時更新mysql數據庫

<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']'; 
?> 
+0

使用會話更新行類似於更新行,任何值 – samayo 2013-04-04 11:55:08

+0

那麼代碼是什麼?我已經嘗試了一切,沒有任何工作。 – StephSetch 2013-04-04 11:57:45

+0

您沒有運行更新查詢'$ u' – samayo 2013-04-04 11:58:22

回答

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

某處,你就必須聲明,$ _SESSION [「學校」]包含一個名稱爲「學校」 textarea的價值。你不能只是希望PHP把POST變量放到session變量

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']; 
+0

這或多或少是我要發佈的內容。如果要編輯數據,則需要將其放入表單中,然後使用POST或GET來傳遞更新的數據,然後在更新記錄之前使用PHP收集數據。 – ragebunny 2013-04-04 12:02:52

0

更新用於數據庫的$_SESSION end grab $_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']; 
... 
0

$_POST都和$_SESSION僅是陣列。唯一讓他們特別的是他們是全球性的。因此,如果您的代碼適用於$_POST,則只需替換相應的$_SESSION值即可。

你真正的問題是,你是提交表單。表格可以使用getpost方法提交。沒有session方法。所以你的值到達你的php腳本中的$_POST變量。在運行更新之前,您可以將它們複製到$_SESSION變量,但實際上沒有理由。他們需要在$_SESSION的唯一原因是,如果需要使用提交的值重新顯示錶單,則可以輸出它們。