PHP: Checkbox to delete database values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kierandes
    New Member
    • Apr 2008
    • 25

    PHP: Checkbox to delete database values

    Hi all,
    I'm pretty new to PHP, not sure where I am going wrong with this code.

    I'm trying to make a database that inserts data from a form to the database (that works and displays data etc), next part is to have a checkbox next to each row that I can tick, then delete that Record.

    Heres a sample of the code
    [code=php]
    {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>" . $row['message'] . "</td>";
    echo "<td>" . $row['time'] . "</td>";
    echo "<td>" . "<input type="checkbox" name="cbox[', $row['id'], ']"/>" . "</td>";
    echo "</tr>";
    }
    [/code]
    I currently get this error from the server
    Code:
    "Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' on line 57"
    that being the checkbox part.

    Any hints guys?
    Last edited by Atli; Apr 3 '08, 02:28 PM. Reason: Added [code] tags.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by kierandes
    Hi all,
    I'm pretty new to PHP, not sure where I am going wrong with this code.

    I'm trying to make a database that inserts data from a form to the database (that works and displays data etc), next part is to have a checkbox next to each row that I can tick, then delete that Record.

    Heres a sample of the code

    {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>" . $row['message'] . "</td>";
    echo "<td>" . $row['time'] . "</td>";
    echo "<td>" . "<input type="checkbox" name="cbox[', $row['id'], ']"/>" . "</td>";
    echo "</tr>";
    }

    I currently get this error from the server

    "Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' on line 57"

    that being the checkbox part.

    Any hints guys?
    You have to escape quotes within quotes with a backslash:
    [php]
    $_badString = " "hello" said the boy";
    $_goodString = " \"hello\" said the boy";
    [/php]

    Hope this helps,
    regards.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi. Welcome!

      Markus is correct. (as usual ;P)

      There is an alternative tho when echoing a large amount of text.
      For example:
      [code=php]
      echo <<<END
      <tr>
      <td>$field1</td>
      <td>{$field2} </td>
      <td>etc...</td>
      </tr>
      END;
      [/code]
      Everything between <<<END and END; is considered a part of the string. Variables are parsed as they are parsed in a normal string.

      Makes it a bit easier when dealing with multiple lines of text.

      Comment

      • kierandes
        New Member
        • Apr 2008
        • 25

        #4
        Ah thanks for that guys. I'll try out the backslash's within the code.

        That Echoing structure looks hell of alot easier then mine, ty for the tip.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by Atli
          Hi. Welcome!

          Markus is correct. (as usual ;P)

          There is an alternative tho when echoing a large amount of text.
          For example:
          [code=php]
          echo <<<END
          <tr>
          <td>$field1</td>
          <td>{$field2} </td>
          <td>etc...</td>
          </tr>
          END;
          [/code]
          Everything between <<<END and END; is considered a part of the string. Variables are parsed as they are parsed in a normal string.

          Makes it a bit easier when dealing with multiple lines of text.
          When using this can you replace END with anything aslong as the last statement is the same?

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Originally posted by markusn00b
            When using this can you replace END with anything aslong as the last statement is the same?
            The top and the bottom lines can't contain any text. They must be
            [code=php]
            echo <<<END
            <text goes here>
            END;
            [/code]
            If there is anything after the <<<END or anything before END; you will get a parse error.

            For example:
            [code=php]
            echo <<<END First
            Second
            Last
            END;
            [/code]
            [code=php]
            echo <<<END
            First
            Second
            Last END;
            [/code]
            These will both fail.

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              Originally posted by Atli
              The top and the bottom lines can't contain any text. They must be
              [code=php]
              echo <<<END
              <text goes here>
              END;
              [/code]
              If there is anything after the <<<END or anything before END; you will get a parse error.

              For example:
              [code=php]
              echo <<<END First
              Second
              Last
              END;
              [/code]
              [code=php]
              echo <<<END
              First
              Second
              Last END;
              [/code]
              These will both fail.
              I think what he means is can you have:
              [code=php]
              echo <<<SOMETHING
              First
              Second
              Last
              SOMETHING;
              [/code]

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by TheServant
                I think what he means is can you have:
                [code=php]
                echo <<<SOMETHING
                First
                Second
                Last
                SOMETHING;
                [/code]
                Yeh, thats exactly what i meant.

                :)

                Comment

                • ak1dnar
                  Recognized Expert Top Contributor
                  • Jan 2007
                  • 1584

                  #9
                  Yes you can put ANYTHING but here is the place we all should start.

                  Comment

                  • TheServant
                    Recognized Expert Top Contributor
                    • Feb 2008
                    • 1168

                    #10
                    I'm sorry to go off topic a bit, but is it better to put everything through the echo like this instead of opening and closing the php parser for displaying html?

                    Comment

                    • dlite922
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1586

                      #11
                      Wondering this same thing too.

                      Originally posted by TheServant
                      I'm sorry to go off topic a bit, but is it better to put everything through the echo like this instead of opening and closing the php parser for displaying html?
                      My thoughts are that if the text is TOO large (leave that up to you), you should open and close the parser.

                      but if its a quick and dirty couple of lines, then you wouldn't notice any difference.

                      Comment

                      • kierandes
                        New Member
                        • Apr 2008
                        • 25

                        #12
                        Hi all, those improvements worked, but I still couldnt delete.

                        I'm trying a new approach but I am going wrong somewhere on this line of code
                        Code:
                        "<a href =\"viewmessage.php\" <?php  DELETE FROM \'quay_messageboard\' WHERE \'quay_messageboard\' , \'id\' =\"$row\" limit 1 ?>; > Delete </a>" ;" . "<td>"
                        I'm trying to make a hyperlink to go with each row that will delete that row when clicked
                        Thanks
                        Last edited by ronverdonk; Apr 5 '08, 12:36 PM. Reason: code tags: 2nd time!!

                        Comment

                        • TheServant
                          Recognized Expert Top Contributor
                          • Feb 2008
                          • 1168

                          #13
                          Originally posted by kierandes
                          Hi all, those improvements worked, but I still couldnt delete.

                          I'm trying a new approach but I am going wrong somewhere on this line of code

                          "<a href =\"viewmessage. php\" <?php DELETE FROM \'quay_messageb oard\' WHERE \'quay_messageb oard\' , \'id\' =\"$row\" limit 1 ?>; > Delete </a>" ;" . "<td>"

                          I'm trying to make a hyperlink to go with each row that will delete that row when clicked
                          Thanks

                          Hmmmm, never seen that before, so not 100% sure it will work. But should your quay_messageboa rd be $quay_messagebo ard? Also, why are you escaping the quotes around the href parameter? You only do that when it is included as a parameter for something like echo().

                          Comment

                          • kierandes
                            New Member
                            • Apr 2008
                            • 25

                            #14
                            Originally posted by TheServant
                            Hmmmm, never seen that before, so not 100% sure it will work. But should your quay_messageboa rd be $quay_messagebo ard? Also, why are you escaping the quotes around the href parameter? You only do that when it is included as a parameter for something like echo().
                            umm I'll give that a try. Ah because I'm learning haha, picked up a good tip on escaping but looks like I used it too much there :)

                            Comment

                            • ronverdonk
                              Recognized Expert Specialist
                              • Jul 2006
                              • 4259

                              #15
                              kierandes: Please enclose your posted code in [code] tags (See How to Ask a Question).

                              This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

                              Please use [code] tags in future.

                              MODERATOR

                              Comment

                              Working...