Insert Selected data in database

Basically i want to insert a Foreign key acc_id in patient_info table from account_info table.

I have managed to retrieve the data from my database. Now I want to insert it in another table as Foreign key. I have following code :

 try {
	$stmt = $pdo->query('SELECT acc_id FROM account_info ORDER BY acc_id DESC LIMIT 1');
		while($row  = $stmt->fetch(PDO::FETCH_OBJ))
		{
			// Assign each row of data to associative array
				$data[] = $row;
		}

		// Return data as JSON
			echo json_encode($data);
 }

How can I insert the value in the table?
this is the full code :

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

 // 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
 $key  = strip_tags($_REQUEST['key']);
$data = array();

switch($key)

{
   // Add a new record to the technologies table
  case "create":

     // Sanitise URL supplied value
     $acc_id	  	 	 	= filter_var($_REQUEST['acc_id'], 
FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
	 $p_fname	 	 	 	= filter_var($_REQUEST['p_fname'], 
FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
	 $p_lname	    	 	= filter_var($_REQUEST['p_lname'], 
FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
	 $p_gender	    	 	= filter_var($_REQUEST['p_gender'], 
FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
     $p_condition  	 	 	= filter_var($_REQUEST['p_condition'], 
FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
	 $p_emergencycontact  	= filter_var($_REQUEST['p_emergencycontact'], 
FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
	 $p_birthdate  	 	 	= filter_var($_REQUEST['p_birthdate'], 
FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW); 
 
try{

	$stmt = $pdo->query('SELECT acc_id FROM account_info ORDER BY acc_id DESC LIMIT 1');
	while($row  = $stmt->fetch(PDO::FETCH_OBJ))
		{
			// Assign each row of data to associative array
				$data[] = $row;
		}

		// Return data as JSON
			echo json_encode($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); 
 
		$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);
		$stmt->bindParam(':acc_id', $acc_id, PDO::PARAM_STR); 

		 
        $stmt->execute();

         echo json_encode(array('message' => 'Congratulations the record was added to the database')); 
 
}
     // Catch any errors in running the prepared statement
     catch(PDOException $e)
     {
        echo $e->getMessage();
     }

 break;
}
?>

I am getting this error:

ERROR SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>) 

I am getting the error here which links back to the php file:

load()
{
  this.http.get('http://localhost:10080/ionic/patients.php')
  .map(res => res.json())
  .subscribe(data =>
  {
     this.items = data;
  });
}

edited

just have a look at $data, there’s the value, you get it via array-access

http://php.net/manual/en/language.types.array.php

example #6

but why does it work when i put it outside the switch function ??

What switch function? There is no switch anywhere in the code you posted.

i’am so sorry i must have deleted by mistake i will fix it right away please don’t stop replying though i really need help!

i meant i added the codeeee!

and what did you get when looking at $data? use var_dump()

You echo two json_encoded strings there, you echo the $data array first (but do nothing else with it) and then you echo the success message. Is that intentional? I’m not entirely sure of the point of that first query, as you retrieve the account id into an array, but don’t seem to use it anywhere. And, as there will only be one result from that query, there’s no real need for a while() loop to retrieve the results. But it might be handy to deal with the chance that no results are returned.

yess does it create any problem ?

I don’t know, but as the error message you reported in the first post was to do with JSON-decoding, I wondered if you had overlooked that part.

The error might come from the catch clause as you don’t JSON_encode the error message.

the error show on .map(res => res.json())

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.