Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

chèn dữ liệu được thu thập dưới dạng Khóa ngoại và SQLSTATE [23000]:Vi phạm ràng buộc toàn vẹn:1048

Có vẻ như có nhiều vấn đề trong mã của bạn hoặc cách bạn đang cố gắng thực hiện mọi thứ. Vui lòng kiểm tra mã bên dưới. Nó sẽ hoạt động. Tôi đã thêm một số bình luận nội tuyến. Vui lòng kiểm tra chúng:

<?php


// Define database connection parameters
$hn      = 'localhost';
$un      = 'root';
$pwd     = '';
$db      = 'ringabell';
$cs      = 'utf8';

// Set up the PDO parameters
$dsn  = "mysql:host=" . $hn . ";port=3306;dbname=" . $db . ";charset=" . $cs;
$opt  = array(
                    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
                    PDO::ATTR_EMULATE_PREPARES   => false,
                   );
// Create a PDO instance (connect to the database)
$pdo  = new PDO($dsn, $un, $pwd, $opt);

// Retrieve specific parameter from supplied URL
$data = array();


try {

    $stmt = $pdo->query('SELECT acc_id FROM account_info ORDER BY acc_id DESC LIMIT 1');
    $data = $stmt->fetchAll(PDO::FETCH_OBJ);
    // You do not need to return response from here
    // echo json_encode($data); 
    // var_dump($data);


    $sql= "INSERT INTO patient_info(acc_id, p_fname, p_lname, p_gender, p_condition, p_birthdate, p_emergencycontact)    
                            VALUES(:acc_id, :p_fname, :p_lname, :p_gender, :p_condition, :p_birthdate, :p_emergencycontact)";


    $stmt    = $pdo->prepare($sql);

    // the $p_fname, $p_lname, $p_gender etc variables in your code were never initiated. You would get
    // notice for this if you had all error_reporting on. I am not sure from where you intend to get this info;
    // so, I just added some dummy data.
    $p_fname = 'Patient first name';
    $p_lname = 'Patient last name';
    $p_gender = 'm';
    $p_condition = 'condition';
    $p_birthdate = '1999-01-01';
    $p_emergencycontact = 'Contact';

    $stmt->bindParam(':p_fname', $p_fname, PDO::PARAM_STR);
    $stmt->bindParam(':p_lname', $p_lname, PDO::PARAM_STR);
    $stmt->bindParam(':p_gender', $p_gender, PDO::PARAM_STR);
    $stmt->bindParam(':p_condition', $p_condition, PDO::PARAM_STR);
    $stmt->bindParam(':p_birthdate', $p_birthdate, PDO::PARAM_STR);
    $stmt->bindParam(':p_emergencycontact', $p_emergencycontact, PDO::PARAM_STR);

    // You do not have any $acc_id variable in your code. To get data from your fetch you need to do 
    // like this:
    $stmt->bindParam(':acc_id', $data[0]->acc_id, PDO::PARAM_STR); 


    $stmt->execute();

    header('Access-Control-Allow-Origin: *');

    // If you want to get the acc_id in your client side through the AJAX call, combine both
    // mesage and data in the same JSON object.
    echo json_encode(
        array(
            'message' => 'Congratulations the record was added to the database'
            'data' => $data
        )
   ); 
} catch(PDOException $e) {
    // make sure to send the proper status code
    http_response_code(500);
    // even error should be sent back as in json so that your javascript client can
    // easily parse it
    echo json_encode(
        array(
            'error' => $e->getMessage()
        )
    );
}
?>



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL group_concat với select bên trong select

  2. OperationalError:(2002, Không thể kết nối với máy chủ MySQL cục bộ thông qua socket '/var/run/mysqld/mysqld.sock' (2))

  3. Cách sao chép bảng trong MySQL

  4. Tăng kích thước nhập mysql

  5. Kiểm tra kết quả trống (PHP, PDO và MySQL)