简体   繁体   English

PHP使用HTML表单更新MySQL表

[英]PHP Update MySQL table using HTML form

I am trying to update my MySQL table by using a form in an HTML page. 我试图通过使用HTML页面中的表单来更新我的MySQL表。

This is my PHP code 这是我的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";
} }

?>

And my HTML form 还有我的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>

These are on the same page by the way. 顺便提一下,它们在同一页面上。 So what I need to happen is for when someone fills out the form and hits the submit button, the PHP code will update my MySQL table on a specific already made record. 所以我需要发生的是当有人填写表单并点击提交按钮时,PHP代码将在特定的已经创建的记录上更新我的MySQL表。

I'm not sure if I somehow need to specify what record I want to update or if I've just completely messed up actually updating my table. 我不确定我是否需要指定我想要更新的记录,或者我是否完全搞砸了实际更新我的表格。

So, my questions is: 所以,我的问题是:

How do I update a specific record in my table using a HTML form and PHP code? 如何使用HTML表单和PHP代码更新表格中的特定记录?

in the form you have to change like this 在表格中你必须像这样改变

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

the php change with below code. 用以下代码改变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";
 } 
}

?>

Just echo all the values on form 只需回显表单上的所有值

for eg: <p>Date: <input type="text" id="datepicker" name="date"></p> into <p>Date: <input type="text" id="datepicker" value="<?php echo $result['date']; ?> " name="date"></p> 例如: <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM