Mysql脚本使用php更新值

时间:2014-06-02 23:25:05

标签: php mysql

我在这里有一个代码,每当我运行它时,它运行没有错误,但它不会更新Mysql中的值。

请告知错误。

感谢。

这是代码。

<?php
session_start();
$loginuser = $_SESSION['result'];
$con=mysqli_connect("localhost","root","","leavecalendar");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
if ('$_POST[department]' == 'Shared') {
  //$sql="UPDATE `employee` SET `leavecount` = '$_POST[lbalance]', sickleave = 1 WHERE        `department` = '$_POST[department]'";
  $sql = "UPDATE `employee` SET `leavecount` = 6.44, sickleave = 5 WHERE `department` =       \'Shared\'";
echo ($sql);
if (!mysqli_query($con,$sql))
 {
 die('Error: ' . mysqli_error($con));
 }
echo "1 record added";

 }

header("location:manageemployeeleaves.php");
mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:1)

在您当前的代码中,删除'$_POST[department]'上的引号,因为它被解释为一个简单的字符串。考虑这个例子:

<?php

session_start();
$loginuser = $_SESSION['result'];
$con=mysqli_connect("localhost","root","","leavecalendar");
// Check connection
if(mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if($_POST['department'] == 'Shared') {
    $sql = "UPDATE `employee` SET `leavecount` = '6.44', sickleave = 5 WHERE `department` = 'Shared'";
    if(!mysqli_query($con,$sql)) {
        die('Error: ' . mysqli_error($con));
    }
}

mysqli_close($con);    
header("Location: manageemployeeleaves.php");

?>
  

注意:由于您使用的是 mysqli ,因此建议您使用 mysqli_stmt::bind_param ,而不是直接在查询语句中使用您的变量。