0

I am working on a php code as shown below which scans the directory ($src_dir) and list all mp4 files.

$src_dir = ('\\\ABCD-ST-001\Audio_Test\podcast\incoming_folder'); 

$mp4_files = preg_grep('~\.(mp4)$~', scandir($src_dir));

print_r(array_values($mp4_files));  // LineA

Here is the O/P obtained from Line#A:

Array ( [0] => 36031P.mp4 [1] => hello.mp4 )

I am getting two values at Index 0 and Index 1. The 1st is 36031P.mp4 and 2nd is hello.mp4

After that I have wrote a script which insert data in table Podcast_Export.

$db->exec("INSERT INTO Podcast_Export ('House#', 'Status') VALUES ('array_values($mp4_files)', 'Go')");   /* have to place this question on SO*/

On running the above script, I am getting the following data inside the table which is not I want.

array_values(Array) Go
array_values(Array) Go

Problem Statement:

I am wondering what changes I should make in the script above so that it inserts the following data inside the table:

36031   Go
hello   Go
5
  • Are you accessing SQLite with PDO exec() or SQLite exec()? Commented Jun 19, 2019 at 15:00
  • @KIKOSoftware I have used the following code to make a connection with SQLite databse class MyDB extends SQLite3 { function __construct() { $this->open('database/Podcast.db'); } } $db = new MyDB(); if(!$db) { echo $db->lastErrorMsg(); } else { echo "Opened database successfully<br>"; } Commented Jun 19, 2019 at 15:10
  • Let me know if this helps. Commented Jun 19, 2019 at 15:10
  • I don't know who downvoted my question. I showed my effort as well. Commented Jun 19, 2019 at 15:20
  • Most downvotes are a hit-and-run thing, so I doubt your comment will ever be read by the perpetrator. Just ignore downvotes, they're a fact of live, you can't please everybody. I'll upvote to compensate. Did you know that upvotes are free? Commented Jun 19, 2019 at 15:23

1 Answer 1

1

You are inserting a string value 'array_values($mp4_files)' , you just need to remove quotes around this value. something like:

$db->exec("INSERT INTO Podcast_Export ('House#', 'Status') VALUES (array_values($mp4_files), 'Go')");

But, if you delete the quotes around array_values then it will insert following array as a string Array ( [0] => 36031P.mp4 [1] => hello.mp4 )

so, its better to use loop here with $mp4_files array and insert your data.

Side Note:

I suggest you to use different name for House# column something like house_no.

Sign up to request clarification or add additional context in comments.

7 Comments

I have used the following code in order to insert data in the database and it works. foreach ($mp4_files as $value) { $db->exec("INSERT INTO Podcast_Export ('House#', 'Status') VALUES ('".$value."', 'Go')"); }
The issue which is now is that when I refresh the page it calls the script again and it will insert in the table many times.
@flash: as we dont know from where you get this variable $src_dir i think u need to add one more query before insert , check either data available of not... this is another question
@flash: i suggest you to create one another quesiotn with more detail from where you getting data, and close this task , marking acceptence, ...
I have edited my question. It will give you more information from where I am getting $src_dir
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.