drsxobip501258 2016-05-26 07:32
浏览 22
已采纳

在mysql中使用WHERE条件插入查询

I tried to insert data into mysql table with some where condition. But it makes some error which is

error on queryYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 

I have tried following query,

if(isset($_POST['submit'])){
    $type=$_POST['leave_type'];
    $division=$_POST['division'];
    $number_of_date=$_POST['number_of_date'];

    $resul=mysql_query("SELECT * from employee where (division='$division' || division='all_dpt')") or die("query error".mysql_error());
    $result3 = mysql_fetch_array($resul);
    $emp_division=$result3['division'];
    $id=$result3['emp_id'];
    $annual_additional=$result3['annual_additional'];

    $value=$annual_additional+$number_of_date;

    if(($division==$emp_division || $division='all_dpt') && $type='Annual'){
        $result1=mysql_query("INSERT INTO employee (annual_additional) VALUES ('$value') WHERE emp_id='$id'")or die("error on query".mysql_error());
    }}

How can I fix it, please help !

  • 写回答

4条回答 默认 最新

  • dsaj20411 2016-05-26 07:49
    关注

    IMPORTANT WARNING: mysql_query was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include mysqli_query() and PDO::query()

    FOR EDUCATIONAL PURPOSES: this is how your code should look if you are using mysql_query:

    //you should definitely create a function that sanitizes the users input
    //so that you don't get hacked via sql injection:
    $value = sanitize($value);
    $id= sanitize($id);
    
    $sql = "UPDATE employee SET annual_additional = '$value' 
    WHERE emp_id='$id'";
    if (!result = mysql_query($sql))
    {
        die("query error".mysql_error());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误