Click here to Skip to main content
14,691,731 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I need to take a list of folder name to my database how can I do that?

I code like that but it takes only the last data from folder list. what is wrong?

Also I want : if there is same project name. it doesnt get it twice and give me alert for " Already exsist that name"

Thanks.

What I have tried:

<?php

 if($_POST["action"] == "fetch")

 $connect = mysqli_connect("localhost", "root", "Pasword", "folder");


  $folder = array_filter(glob('aaa/*'), 'is_dir');


   foreach($folder as $name)

    $sql = "INSERT INTO folder (folder_name)  VALUES ('$name')   ";  
    if(mysqli_query($connect, $sql))  


?>
Posted
Updated 16-Apr-20 23:51pm
v3
Comments
Richard MacCutchan 17-Apr-20 5:01am
   
Your foreach and if statements are incomplete.
Haluk Aydogan 17-Apr-20 7:37am
   
What is the correct form of the code?
Richard MacCutchan 17-Apr-20 9:03am
   
You need to learn the language properly: PHP: foreach - Manual[^].
Haluk Aydogan 17-Apr-20 9:17am
   
Here is learning and helping platform I think... if I know or I can learn from anywhere else : so why we are talking here about that?

I wrote down here to learn already...

So please if you dont answer about how to code or how can I solve the problem with excat codes.. Dont write down my question.. Thanks
Richard MacCutchan 17-Apr-20 9:52am
   
You cannot learn a programming language by posting questions here. You need to study it in detail before you even think of writing code.

And the answer you are looking for is something like the following:
foreach($folder as $name)
{
    $sql = "INSERT INTO folder (folder_name)  VALUES ('$name')   ";  
    if(mysqli_query($connect, $sql)) 
    {
        // so if the sql call succeeds, you need to do something with the data
    }
    else
    {
        // and if it fails - then what?
    }
} // and this is the end of the foreach 
Haluk Aydogan 17-Apr-20 16:24pm
   
Thanks.. And you can belive in that, I am trying to learn coding but not from book. My style is like that:

I need something that I know what will I do...and now researching internet about how can I do thoses...

so with that way I am learning when I do what I needs.
I only ask when I really cant do that, if u can see that: this is my first question... BUt I did what I need half of them
Richard MacCutchan 18-Apr-20 3:20am
   
And probably the very worst way to do it. Unless you understand the complete language you are relying on code that could have been written by anyone, who may not fully understand what they are doing.
Haluk Aydogan 18-Apr-20 3:58am
   
Yes I think you are right... Maybe both need .. First try to do something from internet and than I think I must read book. so When I am reading book I can understand more... you are right really.
Richard Deeming 21-Apr-20 13:48pm
   
$sql = "INSERT INTO folder (folder_name)  VALUES ('$name')   ";

Your code is almost certainly vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
Haluk Aydogan 2-May-20 3:20am
   
First of all thanks... Secondly, can you please write down what I must not do and what I must do clearly?
Richard Deeming 5-May-20 6:27am
   
PHP: Prepared statements and stored procedures - Manual[^]
PHP: mysqli::prepare[^]
PHP: mysqli_stmt::bind_param[^]
$connect = mysqli_connect("localhost", "root", "Pasword", "folder");
$stmt = $mysqli_prepare("INSERT INTO folder (folder_name)  VALUES (?)");
mysqli_stmt_bind_param($stmt, 's', $folder_name);

$folder = array_filter(glob('aaa/*'), 'is_dir');
foreach($folder as $name) {
    $folder_name = $name;
    mysqli_stmt_execute($stmt);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900