简体   繁体   中英

PHP Update MySQL table using HTML form

I am trying to update my MySQL table by using a form in an HTML page.

This is my PHP code

    <?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

<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.

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?

in the form you have to change like this

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

the php change with below code.

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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