【问题标题】:can't update MySQL table by PHP session无法通过 PHP 会话更新 MySQL 表
【发布时间】:2016-09-19 03:56:48
【问题描述】:

我无法通过从 PHP 会话中获取的值来更新 MySQL 表。我测试了PHP会话得到的值是可以的,我也发现我的代码中$sql_query1有问题,但我无法修复它。

代码如下:

MySQL 架构↓

CREATE TABLE `test` (
  `class` int(50) DEFAULT NULL ,
  `class_group` int(50) DEFAULT NULL,
  `name` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `addr` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `seq` int(50) NOT NULL,
  `survey` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `survey_status` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `survey_date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `test` (`class`, `class_group`, `name`, `addr`, `seq`, `survey`, `survey_status`, `survey_date`) VALUES
(1, 0, 'Michael  ', 'USA', 10, '', '', '0000-00-00'),
(1, 0, 'Jordan  ', 'CAN', 20, '', '', '0000-00-00');

ALTER TABLE `test`
  ADD PRIMARY KEY (`seq`);

index.php↓

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<body>

  <h2 align="center">Update_System</h2>

<form action="2_update.php" method="get">
  <fieldset>
  <legend><h2>System</h2></legend><br/>
  seq: <input type="text" name="seq"><br/>
  survey: <input type="text" name="survey"></br>
  survey_status: <input type="text" name="survey_status"><br/>
  survey_date: <input type="text" name="survey_date"><br/>
  </fieldset>
  <br>
  <input type="submit" name="submit" value="submit">
</form>
<br>
<form action="1_test_preview.php" method="POST">
<input type="submit" name="submit_preview" value="preview list">
</form>

</body>
</html>

preview.php↓

<!DOCTYPE html>
<html>
<body>
<h2 align="center">Result</h2>

<?php
if(isset($_POST['submit_preview'])){

$conn=mysql_connect("localhost","root","");
if (!$conn){die ("can not connect" . mysql_error());}
mysql_select_db("fs_change",$conn);
mysql_query("SET NAMES 'UTF8'");
mysql_query("set character set 'utf8'");

//set db connection
$sql_query1="select * from test";
$sql_nrow="select count(*) from test";
$result=mysql_query($sql_query1, $conn); 
$row_result=mysql_fetch_assoc($result); 
}
?>
<form id="form1" name="form1">
<table border="1" align="center">
<tr>
<td>class</td>
<td>class_group</td>
<td>name</td>
<td>addr</td>
<td>seq</td>
<td>survey</td>
<td>survey_status</td>
<td>survey_date</td>
</tr>

<?php do { ?>

<tr>
<td><?php echo $row_result['class']; ?></td>
<td><?php echo $row_result['class_group']; ?></td>
<td><?php echo $row_result['name']; ?></td>
<td><?php echo $row_result['addr']; ?></td>
<td><?php echo $row_result['seq']; ?></td>
<td><?php echo $row_result['survey']; ?></td>
<td><?php echo $row_result['survey_status']; ?></td>
<td><?php echo $row_result['survey_date']; ?></td>

<?php } while ($row_result = mysql_fetch_assoc($result));?>
<tr>

</table>
</form> 

<?php
mysql_free_result($result); 
?>
</body>
</html>

update.php↓

<?php
//get the data from session
session_start();
if(isset($_GET['seq'])){$_SESSION['seq']=$_GET['seq'];}
if(isset($_GET['survey'])){$_SESSION['survey']=$_GET['survey'];}
if(isset($_GET['survey_status'])){$_SESSION['survey_status']=$_GET['survey_status'];}
if(isset($_GET['survey_date'])){$_SESSION['survey_date']=$_GET['survey_date'];}



//set variables
    $seq = $_SESSION['seq'];
    $survey = $_SESSION['survey'];
    $survey_status = $_SESSION['survey_status'];
    $survey_date = $_SESSION['survey_date'];

//set db connection  
if(isset($_GET['submit'])){

$conn=mysql_connect("localhost","root","");
if (!$conn){die ("can not connect" . mysql_error());}
mysql_select_db("fs_change",$conn);
mysql_query("SET NAMES 'UTF8'");
mysql_query("set character set 'utf8'");

//set sql query
$sql_query1="UPDATE fs SET survey = $survey, survey_status = $survey_status, survey_date = $survey_date WHERE seq = $seq";
mysql_query($sql_query1, $conn);
mysql_close($conn);
} 
?>

<!DOCTYPE html>
<html>
  <body>
    test sql_query1:
    <?php 
    if ($sql_query1 === TRUE) {
    echo 'Updated Successfully'
    ;}
    else {echo "Update Failed";}
    ?> <br>
    seq:<?php echo $seq?><br>
    survey:<?php echo $survey?><br>
    survey_state:<?php echo $survey_status?><br>
    survey_date:<?php echo $survey_date?><br>
    <input type = "button" name = "return" value = "back" onclick= "history.back()"> &nbsp;
    </form>
  </body>
</html>

【问题讨论】:

  • 您刚刚开始使用 php mysql 组合并且您选择了mysql_ db 扩展。为什么?
  • @Drew 对不起,我听不懂你的意思?我是 php + mysql 初学者,你能告诉我更多吗?
  • if ($sql_query1 === TRUE) { 不会为真,因为变量保存查询而不是查询执行结果。分配给一个变量,然后将 if 条件检查为 $result = mysql_query($sql_query1, $conn); 然后在 if 条件中 if ($result) { 就可以了
  • 查看stackoverflow.com/tags/php/info并搜索PHP 5.5 and removed in PHP 7. Use PDO or MySqli instead.
  • @Sasikumar :感谢您的建议。我试了一下,还是不行。

标签: php mysql session


【解决方案1】:

您缺少值的单引号。使用以下更新 SQL 和 HTML 来获取成功或失败消息。

$sql_query1="UPDATE fs SET survey = '$survey', survey_status = '$survey_status', survey_date = '$survey_date' WHERE seq = '$seq'";

$query_result = mysql_query($sql_query1, $conn);
mysql_close($conn);
} 
?>

<!DOCTYPE html>
<html>
  <body>
    test sql_query1:
    <?php 
    if ($query_result) {
    echo 'Updated Successfully'
    ;}
    else {echo "Update Failed";}
    ?> <br>

【讨论】:

  • 感谢您的建议,现在我已成功更新,但在 MySQL 数据库中,它没有显示我刚刚更新的值。怎么会?
  • 您应该根据插入/更新值的最大可能性检查表列的数据类型和长度。
  • 我的数据库架构如下: seq: int(50)__这是主键调查:varchar(50) 调查状态:varchar(50) 调查日期:varchar(50) 仍然不起作用,为什么?
  • echo $sql_query1="UPDATE fs SET survey = '$survey', survey_status = '$survey_status', survey_date = '$survey_date' WHERE seq = '$seq'"; exit; - 它将打印 SQL 语句。复制打印的 sql 并在 phpmyadmin 中运行并查看输出。
  • 感谢您的帮助,实际上我在为变量添加引号后完成了更新过程。我无法显示正确输出的原因是我在 $sql_query1 变量中输入了我的表名。这对我很有帮助,谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-01-06
  • 1970-01-01
  • 2016-04-06
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
相关资源
最近更新 更多