0

Hi all, I'm having issues with PDO. I'm rather new at using it, I learned php in school and now I'm learning PDO in the work place. Not terribly difficult but I'm running into a looping issue.

I have things set up this way. We have events, documents and usergroups. Documents can be added on their own or uploaded with an event. And each document or event can be accessible by multiple usergroups. Events can have mulitple documents attached to them. And the trouble seems to occur with a for loop inside a for loop.

for($i = 1; $i <= $_POST["files"]; $i++)
    {   
        //echo($i);
        //echo( $_POST["files"]);
        $file = $_POST["filename".$i];
        //echo("File".$file);
        if(trim($file!=""))
        {
            if (!(substr($file,0,7)=="http://") && !(substr($file,0,8)=="https://") &&  !(substr($file,0,6)=="ftp://"))
            {
                $filedir=$mainpath;

                //$filename=str_replace("docs/","",$_POST["filename1"]);
                $filename=$file;

                //check for availability
                $newfilename=checkfilename($filename,$docpath);

                //move file
                $newlocation=$docpath.$newfilename;
                $oldlocation=$temppath.$filename;

                @rename($oldlocation, $newlocation);

                //insert file
                $sql="insert into tblfiles(fname, flink, date) values ('".mysql_real_escape_string($_POST["title"])."', '".$newfilename."', CURDATE())";


            }else{
                //link
                $sql="insert into tblfiles(fname, flink, date) values ('".mysql_real_escape_string($_POST["title"])."', '".$file."', CURDATE())";
            }

            //echo($sql);

            //$query=mysql_query($sql) or die("Query failed : " . mysql_error());
            $sqlstmt = $_SESSION["db1"]->prepare($sql) ;
            $sqlstmt->execute() or die("Query failed : " . mysql_error());

            $id = $_SESSION["db1"]->lastInsertId();
            $filegroup = $_POST["usertype"];
            $count = count($filegroup);
            //echo($count);
            for($j=0; $j < $count; $j++)
            {

                $sql2="insert into tblmultigroups(gid, newsid, fid) values ('".$filegroup[$j]."', '".$newsid."', '".$id."')";
                echo($sql2."<br>");
                $sqlstmt2 = $_SESSION["db1"]->prepare($sql2) ;
                $sqlstmt2->execute();

            }


        }


    }

So the basic idea is it loops through the files array and adds each item to the database, the attachment can either be a file or a website link, if its a file it checks it to make sure that the name doesn't conflict, renames it and moves it from a temp directory (this is some old code not written by me).

The problem lies with $sqlstmt2, which is the part I've written. This part is supposed to loop throught the checkbox array and add each file to the groups table. But it only adds the first file. The loop works fine, when I print out my sql statment I get this:

insert into tblmultigroups(gid, newsid, fid) values ('1', '1', '1')
insert into tblmultigroups(gid, newsid, fid) values ('2', '1', '1')
insert into tblmultigroups(gid, newsid, fid) values ('1', '1', '2')
insert into tblmultigroups(gid, newsid, fid) values ('2', '1', '2')

but the only one that actually executes is the first one. I've been troublshooting this all morning and I just don't see the problem, and I've been trying to get it to output the error like with mysql_error() but the PDO equvilent of that is only giving me

Array ( [0] => 00000 ) 1
Fatal error: Exception thrown without a stack frame in Unknown on line 0

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by dyingatmidnight
0

for($i = 1; $i <= $_POST["files"]; $i++)

The problem looks to be with your outer most loop. Without seeing your HTML, I am guessing this should probably be something like:
for($i = 1, $count = count($_POST['files']); $i <= $count; $i++)

Edited by blocblue

0

as it turns out I had made one of the variables in my table a unique id by accident and it was messing up the insert statement. Once I realized that everything started working. But thaks for the response.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.