2016-08-03 132 views
1

我想通過使用HTML頁面中的表單來更新我的MySQL表。PHP更新使用HTML表格的MySQL表

這是我的PHP代碼

<?php 
ob_start(); 
session_start(); 
require_once 'dbconnect.php'; 

if(!isset($_SESSION['client'])) { 
    header("Location: homepage_login.php"); 
    exit; 
} 
// select loggedin users detail 
$res=mysql_query("SELECT * FROM clients WHERE client_id=".$_SESSION['client']); 
$userRow=mysql_fetch_array($res); 

if(isset($_POST['btn-book'])) { 

$sql="UPDATE appointments SET date='$slot_date', line='$slot_line', reason='$reason' WHERE id='$id'"; 
$result=mysql_query($sql); 

if($result){ 
echo "Successful"; 
} 

else { 
echo "ERROR"; 
} } 

?> 

而我的HTML表單

<form action"" method"post"> 

     <p>Date: <input type="text" id="datepicker" name="date"></p> 

     <br> 
     <br> 
     Select a line: 
     <ol id="selectable" name="line"> 
    <li class="ui-widget-content">Line 1</li> 
    <li class="ui-widget-content">Line 2</li> 
    <li class="ui-widget-content">Line 3</li> 
    <li class="ui-widget-content">Line 4</li> 
    <li class="ui-widget-content">Line 5</li> 
    <li class="ui-widget-content">Line 6</li> 
    <li class="ui-widget-content">Line 7</li> 
</ol> 
<br> 
<br> 

    <p>Reason for appointment: <input type="text" name="reaosn"></p> 

     <div class="form-group"> 
      <button type="submit" class="btn btn-block btn-primary" name="btn-book">Book</button> 
      </div> 

      </form> 

這些是由該方式在同一頁上。所以我需要發生的是,當有人填寫表單並點擊提交按鈕時,PHP代碼將更新我的MySQL表格在特定的已經創建的記錄上。

我不確定我是否需要指定要更新的記錄,或者如果我完全搞砸了實際更新我的表。

所以,我的問題是:

如何使用HTML表單和PHP代碼更新我的表中特定的記錄?

+0

在SQL更新查詢。只需經過..http://www.w3schools.com/php/php_mysql_update.asp –

+0

我不確定實際問題是什麼?你有什麼錯誤嗎? – Epodax

+0

我沒有收到任何錯誤,這只是我的代碼不會更新我的表中的記錄 – Isabella

回答

0

你必須改變這樣的

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

php的變化與下面的代碼的形式。

<?php 
ob_start(); 
session_start(); 
require_once 'dbconnect.php'; 

if(!isset($_SESSION['client'])) { 
header("Location: homepage_login.php"); 
exit; 
} 
// select loggedin users detail 
$res=mysql_query("SELECT * FROM clients WHERE client_id='$_SESSION[client]'"); 
$userRow=mysql_fetch_array($res); 
$id=$userRow['client_id']; 

if(isset($_POST['btn-book'])) { 

$slot_date= $_POST['date']; 
$reason=$_POST['reason']; 
$slot_line=$_POST['line']; 

$sql="UPDATE appointments SET date='$slot_date', line='$slot_line', reason='$reason' WHERE id='$id'"; 
$result=mysql_query($sql); 

if($result){ 
    echo "Successful"; 
}else { 
    echo "ERROR"; 
} 
} 

?> 
+0

**後添加**危險**:此代碼[易受XSS影響](https:// www .owasp.org/index.php/XSS)用戶輸入(如PHP_SELF)在插入HTML文檔之前需要轉義! – Quentin

+0

感謝您的幫助,但是現在當我點擊提交時,我的頁面顯示無法找到網址?我不確定它是否試圖找到一個我沒有創建/定義的新頁面或其他東西? – Isabella

+0

已更改。謝謝(你的)信息。 @Quentin –

0

只是呼應所有的形式

爲e.g值:<p>Date: <input type="text" id="datepicker" name="date"></p><p>Date: <input type="text" id="datepicker" value="<?php echo $result['date']; ?> " name="date"></p>