douxu2467 2016-05-11 22:05
浏览 10

在PHP MYSQL中使用POST FORM的简短方法

I am using angularjs php form post to insert or update row. Currently I have working example below and I want to know if this can be written in shorter way so that it can used in both UPDATE and INSERT scenario. I want to submit the whole FORM not just the defined value. Thanks for help.

function update($con){
$postdata = file_get_contents("php://input", true);
$request = json_decode($postdata);

$idx = $request->idx;
$images = $request->images;
$collection = $request->collection;
$title = $request->title;
$description = $request->description;
$height = $request->height;
$width = $request->width;
$length = $request->length;
$weight = $request->weight;
$price = $request->price;
$availability = $request->availability;
$active = $request->active;
$method = $request->method;
$searchkeys = $request->searchkeys;
$materialused = $request->materialused;
if(is_array($images)){
    $implodeImg = implode(',', $images);
}else{
    $implodeImg = $images;
}

$sqlIn = "UPDATE prodList_1 SET 
idx = '$idx',
images = '$implodeImg',
collection = '$collection',
title = '$title',
description = '$description',
height = '$height',
width = '$width',
length = '$length',
weight = '$weight',
price = '$price',
availability = '$availability',
active = '$active',
method = '$method',
searchkeys = '$searchkeys',
materialused = '$materialused'
WHERE idx = '$idx'";

if (mysqli_query($con, $sqlIn)) {
  echo "Record Updated successfully";
} else {
  echo "Error: " . $sqlIn . "<br>" . mysqli_error($con);
}

}
  • 写回答

2条回答 默认 最新

  • doudao1837 2016-05-11 22:25
    关注

    I've never used mysqli and probably will never after doing this but I believe it would be something like the following:

    function update($con) {
        $postdata = file_get_contents("php://input", true);
        $request = json_decode($postdata);
    
        $idx = $request->idx;
        $images = $request->images;
        $collection = $request->collection;
        $title = $request->title;
        $description = $request->description;
        $height = $request->height;
        $width = $request->width;
        $length = $request->length;
        $weight = $request->weight;
        $price = $request->price;
        $availability = $request->availability;
        $active = $request->active;
        $method = $request->method;
        $searchkeys = $request->searchkeys;
        $materialused = $request->materialused;
    
        if (is_array($images)) {
            $implodeImg = implode(',', $images);
        }else{
            $implodeImg = $images;
        }
    
        // using INSERT ... ON DUPLICATE KEY UPDATE syntax
        // make sure idx is PRIMARY KEY
        $sql = "
            INSERT INTO table 
            (idx, images, collection, title, description, height, 
             width, length, weight, price, availability, active, 
             method, searchkets, materialused) 
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            ON DUPLICATE KEY UPDATE 
            images = VALUES(images), collection = VALUES(collection), 
            title = VALUES(title), description = VALUES(description), 
            height = VALUES(height), width = VALUES(width), 
            length = VALUES(length), weight = VALUES(weight), 
            price = VALUES(price), availability = VALUES(availability), 
            active = VALUES(active), method = VALUES(method), 
            searchkeys = VALUES(searchkeys), materialused = VALUES(materialused)
        ";
    
        // never trust user-submitted data
        // use prepared statement
        $stmt = mysqli_prepare($con, $sql);
    
        $stmt->bind_param(
            $stmt,
            'sssssssssssssss',
            $idx, $implodeImg, $collection, $title, $description, 
            $height, $width, $length, $weight, $price, $availability, $active,
            $method, $searchkeys, $materialused
        );
    
        mysqli_stmt_execute($stmt);
    
        if (mysqli_stmt_affected_rows($stmt)) {
            echo "Record Updated successfully";
        } else {
            echo "Error: " . $sqlIn . "<br>" . mysqli_error($con);
        }
    }
    

    So.. in terms of your question whether this can be written in a shorter way? Not really. However, you can kill two birds with one stone using INSERT ... ON DUPLICATE KEY UPDATE syntax.

    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序