اذهب إلى المحتوى
  • 0

ادخال بيانات عن طريق api

ايمن ميلاد

السؤال

السلام عليكم لدي كود مرفق لماذا عندما ادخل بيانات لاتظهر رسالة created  بينما تظهر رسالة خطا رغم ادخالي صحيح 

<?php 
error_reporting(0);
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Method: POST');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, x-requested-with ');


include('function.php');



$requestMethod=$_SERVER["REQUEST_METHOD"];
if($requestMethod=="POST")
{
   $inputData=json_decode(file_get_contents("php://input"),true);
   if(empty($inputData))
   {
        $storecustomer=stroecustomer($_POST);
   }
   else
   {
    $storecustomer=stroecustomer($inputData);


   }
   echo $stroecustomer;
}
else
{
    $data=[
         'status'=>405,
          'message'=>$requestMethod.' Method Not Allowed',
    ];
    header("HTTP/1.0 405 Method Not Allowed");
    echo json_encode($data);
}
?>

ملف دوال 

<?php
require '../inc/dbcon.php';
function error422($message)
 {
    $data=[
        'status'=>422,
         'message'=>$message ,
   ];
   header("HTTP/1.0 422 Unprocessable Entity");
   echo json_encode($data);
   exit();

}
function  stroecustomer($customerinput) 
{
    global $conn;
    $name=mysqli_real_escape_string($conn,$customerinput['name']);
    $email=mysqli_real_escape_string($conn,$customerinput['email']);
    $phone=mysqli_real_escape_string($conn,$customerinput['phone']);
    if(empty(trim($name)))
    {
        return error422('Enter your name');
    }elseif(empty(trim($email)))
    {
        return error422('Enter your Email');

    }
    elseif(empty(trim($phone)))
    {
        return error422('Enter your phone');
    }
    else
    {
        $query="INSERT INTO users (name,email.phone) VALUES ('$name','$email','$phone') ";
        $resault=mysqli_query($conn,$query);
        if($resault)
        {
            $data = [
                'status' => 201,
                'message' => 'User Created Successfully',
            ];
            header("HTTP/1.0 201 Created");
            return json_encode($data);
        }
        else
        {
            $data = [
                'status' => 500,
                'message' => 'Internal Server Error',
            ];
            header("HTTP/1.0 500 Internal Server Error");
            return json_encode($data);
        }
    }
}
function getcustomerlist()
{
    global $conn;
    $query = "select *from users";
    $query_run = mysqli_query($conn, $query);
    if ($query_run) {

            if(mysqli_num_rows($query_run)>0)
            {
                $res=mysqli_fetch_all($query_run, MYSQLI_ASSOC);
                $data = [
                    'status' => 200,
                    'message' => 'User list Fetched Successfully ',
                    'data'=> $res
                ];
                header("HTTP/1.0 200 OK");
                return json_encode($data);
            }
            else
            {
                $data = [
                    'status' => 404,
                    'message' => 'NO User  Found ',
                ];
                header("HTTP/1.0 404 No User Found");
                return json_encode($data);
            }



    } else {
        $data = [
            'status' => 500,
            'message' => 'Internal Server Error',
        ];
        header("HTTP/1.0 500 Internal Server Error");
        return json_encode($data);
    }
}


    
?>

 

2.png

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

مرحبا , 
 

الكود الذي قدمته يحتوي على بعض الأخطاء. سأقوم بتصحيحها وشرح التعديلات:

في كود الـ PHP الرئيسي:

يجب تغيير echo $stroecustomer; إلى echo $storecustomer; حيث أنك قمت بتخزين نتيجة الدالة في $storecustomer وليس $stroecustomer.

أنت تستخدم دالة stroecustomer بدلاً من storecustomer، قم بتصحيح ذلك.

تعديل الأمر if (empty($inputData)) إلى if (empty($inputData)) { لإغلاق الفاصلة العلوية.

في ملف الدوال:

في استعمال الـ SQL في دالة stroecustomer يجب أن تتأكد من أن أسماء الأعمدة معرفة بشكل صحيح. في حالتك، يبدو أن هناك خطأ في الاسماء. يجب تغيير 'INSERT INTO users (name,email.phone) VALUES ('$name','$email','$phone') " إلى 'INSERT INTO users (name,email,phone) VALUES ('$name','$email','$phone') ".

يمكنك التحقق من رسائل الخطأ في MySQL عند القيام بأي استعلام باستخدام mysqli_error($conn) للتحقق من الأخطاء.

لتحسين أمان قاعدة البيانات، يمكنك استخدام prepared statements بدلاً من تركيب القيم مباشرة في الاستعلام.

يفضل تحديد أسماء الأعمدة المراد استرجاعها في SELECT * بشكل صريح بدلاً من استخدام SELECT *.

بعد تحديث الكود، ينبغي أن يكون لديك شيء مشابه للكود التالي:

 

<?php 
error_reporting(0);
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Method: POST');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, x-requested-with ');

include('function.php');

$requestMethod = $_SERVER["REQUEST_METHOD"];
if ($requestMethod == "POST") {
    $inputData = json_decode(file_get_contents("php://input"), true);
    if (empty($inputData)) {
        $storecustomer = storecustomer($_POST);
    } else {
        $storecustomer = storecustomer($inputData);
    }
    echo $storecustomer;
} else {
    $data = [
        'status' => 405,
        'message' => $requestMethod.' Method Not Allowed',
    ];
    header("HTTP/1.0 405 Method Not Allowed");
    echo json_encode($data);
}
?>

وفي ملف الدوال:

<?php
require '../inc/dbcon.php';
function error422($message)
{
    $data = [
        'status' => 422,
        'message' => $message,
    ];
    header("HTTP/1.0 422 Unprocessable Entity");
    echo json_encode($data);
    exit();
}

function storecustomer($customerinput)
{
    global $conn;
    $name = mysqli_real_escape_string($conn, $customerinput['name']);
    $email = mysqli_real_escape_string($conn, $customerinput['email']);
    $phone = mysqli_real_escape_string($conn, $customerinput['phone']);

    if (empty(trim($name)) || empty(trim($email)) || empty(trim($phone))) {
        return error422('Please enter valid data for all fields.');
    }

    $query = "INSERT INTO users (name,email,phone) VALUES ('$name','$email','$phone') ";
    $result = mysqli_query($conn, $query);

    if ($result) {
        $data = [
            'status' => 201,
            'message' => 'User Created Successfully',
        ];
        header("HTTP/1.0 201 Created");
        return json_encode($data);
    } else {
        $data = [
            'status' => 500,
            'message' => 'Internal Server Error: ' . mysqli_error($conn),
        ];
        header("HTTP/1.0 500 Internal Server Error");
        return json_encode($data);
    }
}

function getcustomerlist()
{
    global $conn;
    $query = "SELECT * FROM users";
    $query_run = mysqli_query($conn, $query);

    if ($query_run) {
        if (mysqli_num_rows($query_run) > 0) {
            $res = mysqli_fetch_all($query_run, MYSQLI_ASSOC);
            $data = [
                'status' => 200,
                'message' => 'User list Fetched Successfully ',
                'data' => $res,
            ];
            header("HTTP/1.0 200 OK");
            return json_encode($data);
        } else {
            $data = [
                'status' => 404,
                'message' => 'No User Found ',
            ];
            header("HTTP/1.0 404 No User Found");
            return json_encode($data);
        }
    } else {
        $data = [
            'status' => 500,
            'message' => 'Internal Server Error: ' . mysqli_error($conn),
        ];
        header("HTTP/1.0 500 Internal Server Error");
        return json_encode($data);
    }
}
?>


 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...