PHP使用HTML表单更新MySQL表 [英] PHP Update MySQL table using HTML form
问题描述
我试图通过在HTML页面中使用表单来更新我的MySQL表。
这是我的PHP代码
<?php
ob_start();
session_start();
require_once'dbconnect.php';
if(!isset($ _ SESSION ['client'])){
header(Location:homepage_login.php);
出口;
}
//选择登录用户详细信息
$ res = mysql_query(SELECT * FROM clients WHERE client_id =。$ _ SESSION ['client']);
$ userRow = mysql_fetch_array($ res);
if(isset($ _ POST ['btn-book'])){
$ sql =更新约会SET date ='$ slot_date',line ='$ slot_line',reason ='$ reason'WHERE id ='$ id';
$ result = mysql_query($ sql);
if($ result){
echo成功;
}
else {
echoERROR;
}}
?>
和我的HTML表单
< form actionmethodpost>
< p>日期:< input type =textid =datepickername =date>< / p>
< br>
< br>
选择一行:
< li class =ui-widget-content>第1行< / li>
< li class =ui-widget-content>第2行< / li>
< li class =ui-widget-content>第3行< / li>
< li class =ui-widget-content>第4行< / li>
< li class =ui-widget-content>第5行< / li>
< li class =ui-widget-content>第6行< / li>
< li class =ui-widget-content>第7行< / li>
< / ol>
< br>
< br>
< p>约会原因:< input type =textname =reaosn>< / p>
< div class =form-group>
< button type =submitclass =btn btn-block btn-primaryname =btn-book> Book< / button>
< / div>
< / form>
顺便说一句,这些内容在同一页上。所以我需要发生的是,当有人填写表单并点击提交按钮时,PHP代码将更新我的MySQL表格在特定的已经创建的记录上。
我不确定我是否需要指定要更新的记录,或者如果我完全搞砸了实际更新我的表。
所以,我的问题是:
如何使用HTML表单更新表格中的特定记录和PHP代码?
在表格中您必须像这样更改
< form method =postaction =same_page.php>
php用下面的代码改变。
<?php
ob_start();
session_start();
require_once'dbconnect.php';
if(!isset($ _ SESSION ['client'])){
header(Location:homepage_login.php);
出口;
}
//选择登录用户详细信息
$ 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约会SET date ='$ slot_date',line ='$ slot_line',reason ='$ reason'WHERE id ='$ id';
$ result = mysql_query($ sql);
if($ result){
echo成功;
} else {
echoERROR;
}
}
?>
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";
}
}
?>
这篇关于PHP使用HTML表单更新MySQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!