0

I designed a query to do "Rest api" between C # and MySQL, which is the following function:

 function index ()
    {
    $ hadi = "SELECT
      wp_postmeta1.post_id,
      wp_postmeta1.meta_key,
      wp_postmeta1.meta_value
    FROM wp_postmeta1
    WHERE wp_postmeta1.meta_key = '_sku'
    OR wp_postmeta1.meta_key = '_stock'
    OR wp_postmeta1.meta_key = '_price'
    ORDER BY wp_postmeta1.post_id
    ";
        $ res = query ($ hadi);
    while ($ row = mysqli_fetch_assoc ($ res))
    $ rows [] = $ row;
    return $ rows;
    }

To test this message:

http://www.kodakamoz.com/stud.php?method=index

Index Test: Indicates an acceptable display

[{"post_id": "1927", "meta_key": "_ stock", "meta_value": "27"}, {"post_id": "1927", "meta_key": "_ price", "meta_value": "15000 "}, {" post_id ":" 1927 "," meta_key ":" _ sku "," meta_value ":" 00302016 "}, {" post_id ":" 1928 "," meta_key ":" _ price "," meta_value ": "2200"}, {"post_id": "1928", "meta_key": "_ stock", "meta_value": "10"}, {"post_id": "1928", "meta_key": "_ sku", "meta_value" ":" 00301421 "}, {" post_id ":" 1929 "," meta_key ":" _ sku "," meta_value ":" 00301056 "}, {" post_id ":" 1929 "," meta_key ":" _ price ", "meta_value": "5500"}, {"post_id": "1929", "meta_key": "_ stock", "meta_value": "10"},

and also

function edit ($ post_id)
{
    $ res = query ("SELECT
  wp_postmeta1.post_id,
  wp_postmeta1.meta_key,
  wp_postmeta1.meta_value
FROM wp_postmeta1
WHERE wp_postmeta1.meta_key = '_sku'
OR wp_postmeta1.meta_key = '_stock'
OR wp_postmeta1.meta_key = '_price'
HAVING `post_id` = $ post_id
ORDER BY wp_postmeta1.post_id ");
$ rows = [];
while ($ row = mysqli_fetch_assoc ($ res))
$ rows [] = $ row;
return $ rows;
}

To test this message:

http://www.kodakamoz.com/stud.php?method=edit&post_id=1928

Edit Test: Displays an acceptable display

[{"post_id": "1928", "meta_key": "_ price", "meta_value": "2200"}, {"post_id": "1928", "meta_key": "_ stock", "meta_value": "10 "}, {" post_id ":" 1928 "," meta_key ":" _ sku "," meta_value ":" 00301421 "}]

Now for function update I wrote this method which is error

function update ()
{
    $ post_id = $ _ GET ['post_id'];
$ meta_key = $ _GET ['meta_key'];
$ meta_value = $ _GET ['meta_value'];

    $ res = query ("UPDATE` wp_postmeta1`
SET `meta_value` = $ meta_value
WHERE `wp_postmeta1`.`post_id` = $ post_id
AND `wp_postmeta1`.`meta_key` = '_stock'
AND `wp_postmeta1`.`meta_value` =` meta_value`
LIMIT 1 ");
  if ($ res)
    return "1 record updated";
else return "error";
}

To test this message:

http://www.kodakamoz.com/stud.php?method=update&post_id=1928

update test: error What is your way

Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108
  • 3
    I'm confused. You tagged your question with C#, but the code looks very much like PHP. Which language are you using? In any case, you should read up on using prepared statements with MySQL. – Tim Biegeleisen Nov 08 '19 at 03:23
  • I'm going to do between C # Sharp and MySQL rest api But now I'm doing php programming – Hadi Fazilat Nov 08 '19 at 03:32

1 Answers1

-2

Just yesterday I learned about something called "Primitive Obession". That it can be beneficial to use pretty flat classes for ID's, primary keys and other important values to avoid mixups. Particular WebService Calls might benefit from it, due to the weak Typisation inherent in the Web Part.

Another thing I usually do is concurrency control. I prefer Optimistic Concurrency (do not lock, just test for changes during update). In SQL I got the Rowversion column to avoid Update Race Conditions. I just retreive it with all the rest and it is a thing to add to the Updates where clause. Maybe throw an SQL Exception if there is a missmatch.

According to this answer, timestamp can be used similary to rowversion with the right switches. Do keep in mind that a exceptionally fast query and update (an automatic process) can still run into/cause a timestamp colission. There was wisdom in having SQL rowversion be a simple running counter.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • What is your solution? What query to write – Hadi Fazilat Nov 08 '19 at 03:41
  • @HadiFazilat I am a bit to rusty in PHP to write a Query for it. IIRC, it used those wierd prepared statements rather then parametized queries. I honestly only saw this question because of the C# tag. – Christopher Nov 08 '19 at 05:41