简体   繁体   中英

can't update MySQL table by PHP session

I can't update the MySQL table by value get from PHP session. I tested the valued got by PHP session is ok, and I also find there is the problem in $sql_query1 in my code , but I can't fix it.

Here is the code:

MySQL schema↓

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>

You are missing the single quotes to the values. Use the below update SQL and HTML to get success or failure message.

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

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