使用 php 和单选按钮更新数据
Posted
技术标签:
【中文标题】使用 php 和单选按钮更新数据【英文标题】:update data using php and radio buttons 【发布时间】:2020-02-23 10:12:02 【问题描述】:我的表单似乎没有更新。它只是忽略了php代码。我使用了不同形式的相同 php 代码,从未遇到任何问题。我不知道是不是因为我现在正在使用单选按钮。
if (isset($_POST['submitinjury']))
$query = "UPDATE injury SET neck=?,
upperback=?,
lowerback=?,
chest=?,
shoulder=?,
arm=?,
hand=? WHERE userid=?";
$stmt = mysqli_prepare($db, $query);
mysqli_stmt_bind_param($stmt, 'ssssssss', $_POST['neck'], $_POST['upperback'], $_POST['lowerback'], $_POST['chest'], $_POST['shoulder'], $_POST['arm'], $_POST['hand'], $_SESSION['id']);
mysqli_stmt_execute($stmt);
exit(header('location: home.php'));
表格。这是在 new.php 文件中。
<form action="new.php" method="post" >
<table>
<tr>
<th style="font-size:18px" >Any previous injuries y</th>
</tr>
<tr>
<td>Previous neck injury/surgery?</td>
<td></td>
<td></td>
<td><label class="container">Yes
<input type="radio" name="neck" value="yes" >
<span class="checkmark"></span>
</label></td>
<td><label class="container">None
<input value="none" type="radio" checked="checked" name="neck">
<span class="checkmark"></span>
</label></td>
</tr>
<tr>
<td>Previous upperback injury/surgery?</td>
<td></td>
<td></td>
<td><label class="container">Yes
<input type="radio" name="upperback" value="yes" >
<span class="checkmark"></span>
</label></td>
<td><label class="container">None
<input value="none" type="radio" checked="checked" name="upperback">
<span class="checkmark"></span>
</label></td>
</tr>
<tr>
<td>Previous lowerback injury/surgery?</td>
<td></td>
<td></td>
<td><label class="container">Yes
<input type="radio" name="lowerback" value="yes" >
<span class="checkmark"></span>
</label></td>
<td><label class="container">None
<input value="none" type="radio" checked="checked" name="lowerback">
<span class="checkmark"></span>
</label></td>
</tr>
<tr>
<td>Previous chest injury/surgery?</td>
<td></td>
<td></td>
<td><label class="container">Yes
<input type="radio" name="chest" value="yes" >
<span class="checkmark"></span>
</label></td>
<td><label class="container">None
<input value="none" type="radio" checked="checked" name="chest">
<span class="checkmark"></span>
</label></td>
</tr>
<tr>
<td>Previous shoulder injury/surgery?</td>
<td><label class="container">Both
<input value="both" type="radio" name="shoulder">
<span class="checkmark"></span>
</label></td>
<td><label class="container">Left
<input value="left" type="radio" name="shoulder">
<span class="checkmark"></span>
</label></td>
<td><label class="container">Right
<input value="right" type="radio" name="shoulder">
<span class="checkmark"></span>
</label></td>
<td><label class="container">None
<input value="none" type="radio" checked="checked" name="shoulder">
<span class="checkmark"></span>
</label></td>
</tr>
<tr>
<td>Previous arm injury/surgery?</td>
<td><label class="container">Both
<input value="both" type="radio" name="arm">
<span class="checkmark"></span>
</label></td>
<td><label class="container">Left
<input value="left" type="radio" name="arm">
<span class="checkmark"></span>
</label></td>
<td><label class="container">Right
<input value="right" type="radio" name="arm">
<span class="checkmark"></span>
</label></td>
<td><label class="container">None
<input type="radio" checked="checked" value="none" name="arm">
<span class="checkmark"></span>
</label></td>
</tr>
<tr>
<td>Previous hand/wrist injury/surgery?</td>
<td><label class="container">Both
<input value="both" type="radio" name="hand">
<span class="checkmark"></span>
</label></td>
<td><label class="container">Left
<input value="left" type="radio" name="hand">
<span class="checkmark"></span>
</label></td>
<td><label class="container">Right
<input value="right" type="radio" name="hand">
<span class="checkmark"></span>
</label></td>
<td><label class="container">None
<input type="radio" checked="checked" value="none" name="hand">
<span class="checkmark"></span>
</label></td>
</tr>
</table>
<button type="submit" name="submitinjury" >Submit</button>
</form>
点击按钮后,它只是刷新页面,数据库中没有任何更新。
【问题讨论】:
【参考方案1】:看来,问题是由session
引起的。尝试添加session_start()
。
if (isset($_POST['submitinjury']))
session_start();
$query = "UPDATE injury SET neck=?,
upperback=?,
lowerback=?,
chest=?,
shoulder=?,
arm=?,
hand=? WHERE userid=?";
$stmt = mysqli_prepare($db, $query);
mysqli_stmt_bind_param($stmt, 'ssssssss', $_POST['neck'], $_POST['upperback'], $_POST['lowerback'], $_POST['chest'], $_POST['shoulder'], $_POST['arm'], $_POST['hand'], $_SESSION['id']);
mysqli_stmt_execute($stmt);
exit(header('location: home.php'));
【讨论】:
session_start();在页面开头添加 请在代码上加上session_start(),这样大家就知道了。还有有什么错误吗?尝试在页面顶部添加ini_set('display_errors', 1);
和error_reporting(E_ALL);
,以确定错误。
预计参数 1 为 mysqli_stmt,bool 鉴于这是我得到的错误
你的会话ID是整数吗?然后将ssssssss
更改为sssssssi
。还要确保neck
、upperback
、lowerback
、chest
、shoulder
、arm
和hand
是数据库中数据类型的字符串。
数据库中的一切都是varchar以上是关于使用 php 和单选按钮更新数据的主要内容,如果未能解决你的问题,请参考以下文章