www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP,PDO/error:Illegal string offset,probelm in foreach & array

  1. #1
    Join Date
    Sep 2013
    Posts
    18

    PHP,PDO/error:Illegal string offset,probelm in foreach & array

    Hello everyone, I have a page named index.php. in this page I have 3 inputs & a table. the purpose is that to send the data from these inputs to both database(mine easyPHP) & the table I've created in this page,as what we have the inbox tables in emails or Gmail(that it can generate the rows of the table with different content as much as is needed). Here I have 2 problems:


    1) What I need is to remain the input form after clicking the send button (or submit button),but Here after clicking this it'll be disappear!

    2) I have a problem with showing the data I've fetched(for table content) & it errors me in each cell of table that: "Warning: Illegal string offset 'title' in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\projects\TeamWork\index.php on line 99"

    now here is the code:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>TeamWork task</title>
        <link type="text/css" rel="stylesheet" href="twStyle.css">
    </head>


    <?php
     session_start
    ();
    include(
    'connection.php');

    //    $email=$_SESSION['email'];
    //    $name="SELECT FirstName, LastName From teamwork_members where Email ='$email'";
    //    $query=$conn->prepare($name);
    //    $query->execute();
    //    $result = $query->fetchall();
    //    print_r($query->errorInfo());
       
    echo "welcome"." ".$_SESSION['FirstName']." ".$_SESSION['LastName'] ;

    ?>

    <?php
    if ( empty( $_POST ) ){
    ?>

        <body>

            <form method="post">

                <label for="uid">to whom?</label><br/>
                <input type="text" name="uid"><br/>
                <label for="title">title:</label><br/>
                <input type="text" name="title"><br/>
                <label for="content_task">content_task:</label><br/>
                <textarea type="text" name="content_task"></textarea><br/>

                <button type="submit"  value="submit" name="send">send</button>

            </form>

        </body>
    </html>

    <?php
    } else {



        try {

            if((isset(
    $_POST['send'])) && (!empty($_POST['send'])))
            {
                
    $form $_POST;
                
    $uid $_SESSION['UID'];
                
    $title $form'title' ];
                
    $content_task $form'content_task' ];
                
    $sql "INSERT INTO task (
                              uid, title, content_task )
                            VALUES (
                              :uid, :title, :content_task)"
    ;

                
    $query $conn->prepare$sql );
                
    $query->execute( array(':uid'=>$uid':title'=>$title':content_task'=>$content_task) );
                
    // print_r($query->errorInfo());
            
    }
        }
        catch(
    PDOException $e)
        {
            echo 
    $e->getMessage();
        }
    }

    ?>

    <html>
        <body>
            <table border="1 solid #000000">
                <tr>
                    <td>row</td>
                    <td>title</td>
                    <td>content of task</td>
                </tr>
                <?php
                    $mytask 
    "SELECT COUNT(`id`) as numbers, id, uid, title, content_task FROM `task`" ;

                    
    $query=$conn->prepare ($mytask);
                    
    $query->execute( );
                    
    $task_panel $query->fetchAll();

                    
    $row 0;
                        foreach(
    $task_panel as $task){
                            
    $row +=;
                        }
                
    ?>
                            <tr>
                                <td><?php $row ?></td>
                                <td><?php $task[0]['title']?></td>
                                <td><?php $task[0]['content_task']?></td>
                            </tr>

            </table>
        </body>
    </html>
    Thanks everyone

  2. #2
    Join Date
    Sep 2013
    Posts
    18
    since I'm trying & searching I've got a little bit success. but still I have the problem number (1) , but did some changes according to the problem number (2) {but still I have another errors in problem number (2)!}

    what I've changed is the code below:
    PHP Code:
    $query=$conn->prepare ($mytask);
    $query->execute( );

    $row 0;
               
    foreach(
    $query->fetchAll(PDO::FETCH_ASSOC) as $task
    or for better view:

    PHP Code:
    <html>
        <body>
            <table border="1 solid #000000">
                <tr>
                    <td>row</td>
                    <td>title</td>
                    <td>content of task</td>
                </tr>
                <?php
                    $mytask 
    "SELECT COUNT(`id`) as numbers, id, uid, title, content_task FROM `task`" ;

                    
    $query=$conn->prepare ($mytask);
                    
    $query->execute( );
    //                $task_panel = $query->fetchAll();

                    
    $row 0;
                        foreach(
    $query->fetchAll(PDO::FETCH_ASSOC) as $task){
                            
    $row +=;
                        }
                
    ?>
                            <tr>
                                <td><?php $row ?></td>
                                <td><?php $task[1]['title']?></td>
                                <td><?php $task[1]['content_task']?></td>
                            </tr>

            </table>
        </body>
    </html>
    *Note* The full code is in the first post.

    however I could handle the error "Illegal string offset" , I have another error :"Notice: Undefined offset: 1 in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\projects\TeamWork\index.php on line 99" that it seems it's related to the way I've declared this:

    PHP Code:
    <?php $task[1]['title']?>
    but since I'm novice I'm stuck at using the right syntax in PHP & PDO.

  3. #3
    Join Date
    Sep 2013
    Posts
    18
    again I've tried myself , at first the first step I use the code below:

    PHP Code:
    print_r ($query->fetchAll(PDO::FETCH_ASSOC)); 
    to know the structure of this array,to figure out what changes are needed for "$task[1]['title']", & then I could see but the thing I wounder is that after writing that print_r my error changed to "undefined variable: task"!!! & again by removing or commenting print_r ,the error again changed to that :"undefined offset:0"!!

    but the last thing I've changed is that I removed the first index of array $task,as you see in the code below:

    PHP Code:
    <tr>
        <td><?php $row ?></td>
        <td><?php $task['title']?></td>
        <td><?php $task['content_task']?></td>
    </tr>
    and fortunately the result was that I didn't get anymore errors(it's while I've commented the print_r)

    but unfortunately I have no content in table ,it's all empty! + still problem(1){look at post #1)

    again I need help thanks

  4. #4
    Join Date
    Sep 2013
    Posts
    18

    resolved

    well, it seems that I'm asking & answering myself. my problem has been solved last week but I didn't get time to post here. the problem was that I has used "COUNT id" in INSERT query, that it causes to create a 2D array, that wasn't useful & didn't work correctly. & for problem #1, I remember that I had written somewhere "if(empty($_POST['something']))" then show the inputs. maybe I have removed the condition & then it worked

    Thanks everyone, I'll never forget your help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles