שלוחת API שמשום מה מריץ את הקוד פעמיים.
-
עשיתי קוד להזנת מונה חשמל וחישוב לדאטה בייס;
<?php // Sanitize and validate input $ApiPhone = filter_input(INPUT_GET, 'ApiPhone', FILTER_SANITIZE_STRING); if (!$ApiPhone) { die("Invalid input"); } $new_counter = filter_input(INPUT_GET, 'a', FILTER_VALIDATE_INT); if (!$new_counter) { die("Invalid input"); } // Connect to the database $db = new mysqli('localhost', '?', '?!', '?'); // Check for errors if ($db->connect_error) { die("Connection failed: " . $db->connect_error); } //the table name $table = 'Electricity_meter'; // Prepare and execute the SQL query using a prepared statement $sql = "SELECT `sum_counter`, `prev_data` FROM `$table` WHERE `ApiPhone` = ? ORDER BY `prev_data` DESC LIMIT 1"; $stmt = $db->prepare($sql); if (!$stmt) { die("Error preparing query: " . $db->error); } $stmt->bind_param("s", $ApiPhone); // bind the ApiPhone variable to the prepared statement $stmt->execute(); $result = $stmt->get_result(); // Check for errors if (!$result) { die("Error executing query: " . $db->error); } // Fetch the results $row = $result->fetch_assoc(); if (!$row) { die("No results found"); } // Perform the calculation $result1 = 0; if ($new_counter >= $row['sum_counter']) { $result1 = ($new_counter - $row['sum_counter']) * 0.6155; } if ($result1 < 0) { $result1 = 0; } // Calculate the number of days between the two dates $prev_date = strtotime($row['prev_data']); if (!$prev_date) { die("Invalid date format"); } $today = strtotime(date('Y-m-d')); if (!$today) { die("Invalid date format"); } $num_days = floor(($today - $prev_date) / 86400); // Calculate the result $result2 = $num_days * 3.6; $result3 = round($result1 + $result2, 2); // Insert the new data into the table $insert_sql = "INSERT INTO `$table` (`sum_counter`, `prev_data`, `sum_payment`, `ApiPhone` ) VALUES ('$new_counter', NOW(), '$result3', '$ApiPhone')"; $insert_result = $db->query($insert_sql); if (!$insert_result) { die("Error inserting data: " . $db->error); } // Close the database connection $stmt->close(); $db->close(); // Print the result echo "id_list_message=t-סך הכל שעליך לשלם.n-" . str_replace(".", ".t-נקודה.d-", $result3); ?>אולם כאשר אני מפעיל את הקוד בשלוחת API, משום מה השרת מקבל פעמיים את ההפעלה, ומוזן לDB פעמיים נתונים (מה שמבלבל את הנתונים לפעם הבאה).
מישהו מכיר או למאןדהו פתרונים?
-
@לעיתים-נדירות מסתמא זה בגלל השליחה הנוספת בניתוק השיחה. תבדוק אם הפרמטר hangup קיים
https://f2.freeivr.co.il/topic/56/מודול-api-תקשור-עם-מחשבים-וממשקי-נתונים-חיצוניים/2#:~:text=ולהסתיים ללא סלש.-,שליחה בניתוק שיחה,-לאחר שהמשתמש ניתק -
@צדיק-תמים
נכון מאודהסתדר!
שלום! נראה שהשיחה הזו מעניינת אותך, אבל עדיין אין לך חשבון.
נמאס לכם לגלול בין אותם הפוסטים בכל ביקור? כשנרשמים לחשבון, תמיד תחזרו בדיוק למקום שבו הייתם קודם, ותוכלו לבחור לקבל התראות על תגובות חדשות (בין אם במייל, ובין אם בהתראת פוש). תוכלו גם לשמור סימניות ולפרגן ב-upvote לפוסטים כדי להביע הערכה לחברי קהילה אחרים.
בעזרת התרומה שלך, הפוסט הזה יכול להיות אפילו טוב יותר 💗
הרשמה התחברות