Linking with form php and mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kempy535
    New Member
    • Dec 2006
    • 14

    Linking with form php and mysql

    Hi Im have aproblem with a site im trying to design. What Im trying to do is when you click on an image it links to another page and displays data that is relevent to its self.
    The images are stored in the mysql data base under a colum called thumb and what im trying to make happen is when you click on the image it submittes its thumb name with a form feature and I can use the post meathod to recive it on another page and display the rest of the data stored in its row.
    The below code sort of works, the only problem is that is selects the last item in the data base with the name gibson and the data in its row. I cant get it to select anything else.
    I could be going about this complitly the wrong way...
    Any help would be great
    this is the code to send;

    <form action='proinfo .php' target ='mainFrame' method='post' >

    <?php
    $result = mysql_query("SE LECT * FROM stocklist WHERE make LIKE 'gibson' ");
    while($row = mysql_fetch_arr ay($result))
    {
    //$proid = $row['thumb'];
    $thumbpic = $row['thumb'];

    echo "<input type='image' src = '$thumbpic'/>";
    echo "<input type='hidden' name ='proid' value ='$thumbpic'/>";

    }

    ?>

    This is the code to display what has been sent;

    $searchd = $_POST["proid"];
    $sql = "SELECT * FROM stocklist WHERE thumb = '$searchd'";
    $result = mysql_query($sq l) or die(mysql_error ());
    while ($row = mysql_fetch_arr ay($result))
    {
    echo $row['title']." ".$row['image']." ".$row['images'];
    echo "<br />";
    }
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Maybe I don't understand your question in full, but you select the 'gibson' yourself! So you cannot complain that only 'gibson' is selected. See this statement
    Code:
    $result = mysql_query("SELECT * FROM stocklist WHERE make LIKE 'gibson' ");
    Ronald :cool:

    Comment

    • kempy535
      New Member
      • Dec 2006
      • 14

      #3
      There are 8 items in the row named gibson and another 10 named somthing else. But the code at the moment only returns the last item with the name gibson. I cant get it to return any of the other 7:(

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        All your form input fields have the name 'proid'. Since there can only be one occurrence in the array ($_POST[ 'proid']) there is only one entry passed, i.e. the last one filled.

        Ronald :cool:

        Comment

        • kempy535
          New Member
          • Dec 2006
          • 14

          #5
          Yep I have just been messing round with it and found that. How can I get round that? I want to click on the image and for it to sends its 'id' information with it? I just cant figure out how to do it:(

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Well, I am not much of a JavaScript and html image submit buff, but I found, by trying, a solution that (at least) works.

            Firstly, the problem for unique names can be solved by using an index variable $i and concatenating that to the proid constant.
            Secondly, the name passed by the image submit button is not the name you give it, but is an internal name (someting like 'proid0_x23').

            So I just thought, why don't I use JS to capture the name before it gets clobbered and pass that name in the hidden input with id 'submitted', so the name of the clicked image statement is passed via the 'id=submitted' input statement.

            Anyway, here is the code and it works for me. Any HTML or JS guru's: go ahead and make it better or lighter.
            [html]<html<head><tit le>Test It</title>
            <script type="text/javascript">
            function getIt(sel) {
            document.getEle mentById("submi tted").value=se l.name;
            }
            </script>
            </head><body>
            <?php
            if (isset($_POST['submitted'])) {
            // connect to server and db
            $ind = $_POST['submitted'];
            $searchd = $_POST[$ind];
            $sql = "SELECT * FROM stocklist WHERE thumb='$searchd '";
            $result = mysql_query($sq l) or die(mysql_error ());
            while ($row = mysql_fetch_arr ay($result)) {
            echo $row['title']." ".$row['image']." ".$row['images']."<br />";
            }
            }
            else {
            echo "<form action='a.php' method='post'>" ;
            // connect to server and db
            $i=0;
            $result = mysql_query("SE LECT * FROM stocklist WHERE make LIKE 'gibson' ");
            while ($row = mysql_fetch_arr ay($result)) {
            $thumbpic = $row['thumb'];
            echo "<input type='image' id='myimage' name='proid$i' onclick='getIt( this)' src='$thumbpic' width='60' height='60' />&nbsp;&nbsp;&n bsp;";
            echo "<input type='hidden' name='proid$i' value='$thumbpi c' />";
            $i++;
            }
            echo "<input type='hidden' id='submitted' name='submitted ' value='1' />";
            echo "</form>";
            }
            ?>
            [/html]
            Ronald :cool:

            Comment

            • kempy535
              New Member
              • Dec 2006
              • 14

              #7
              Oh wow thats brillaint, thank you. I do have a couple of questions tho, Im a networking student a know very little about programming so Im sorry il any of the questions seem simple.
              Do I just have to change 'a.php' to a blank php and the results will get sent there? Or do I need a echo command?

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                I forgot to change the action of the form. Since you are submitting the form to itself you can replace the form statement by
                [php]echo '<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">' ;[/php]
                Ronald :cool:

                Comment

                • kempy535
                  New Member
                  • Dec 2006
                  • 14

                  #9
                  I think some of the " need replacing with ' because the code turns all black when you added the section you wrote. I thought this would be right ,[php]echo "<form action = '<?php echo "$_SERVER['PHP_SELF']"; ?>' method='post'>" ;[/php]
                  but it gives "Parse error: parse error, unexpected T_VARIABLE, expecting ',' or ';' in C:\Program Files\Apache Group\Apache2\h tdocs\Rising sun\Risingsun Gibson.php on line 42" error code.

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Originally posted by kempy535
                    I think some of the " need replacing with ' because the code turns all black when you added the section you wrote. I thought this would be right ,[php]echo "<form action = '<?php echo "$_SERVER['PHP_SELF']"; ?>' method='post'>" ;[/php]
                    but it gives "Parse error: parse error, unexpected T_VARIABLE, expecting ',' or ';' in C:\Program Files\Apache Group\Apache2\h tdocs\Rising sun\Risingsun Gibson.php on line 42" error code.
                    I am too hasty nowadays, this will do it[php]echo "<form action='".<?php echo "$_SERVER['PHP_SELF']"; ?>. "' method='post'>" ;[/php]
                    Ronald :cool:

                    Comment

                    • kempy535
                      New Member
                      • Dec 2006
                      • 14

                      #11
                      It still doesnt like it. all of the code after the last dot turns black. I have tried moving the dotes and have added another set of " before and after each dot that makes the code go the right color.
                      [php] echo "<form action='"."<?ph p echo "$_SERVER['PHP_SELF']"; ?>"." ' method='post'>" ;[/php]
                      But I get the same error as before
                      "Parse error: parse error, unexpected T_VARIABLE, expecting ',' or ';' in C:\Program Files\Apache Group\Apache2\h tdocs\Rising sun\Risingsun Gibson.php on line 42"

                      Comment

                      • ronverdonk
                        Recognized Expert Specialist
                        • Jul 2006
                        • 4259

                        #12
                        It's just not one of my days! Not quite my day at all!
                        Code:
                        echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";
                        Ronald :cool:

                        Comment

                        • kempy535
                          New Member
                          • Dec 2006
                          • 14

                          #13
                          Thanks soooooooooooooo o much it works :D I cant tell you how gratefull I am.

                          Comment

                          • seangates
                            New Member
                            • Dec 2006
                            • 19

                            #14
                            Ronald, it should be valid HTML which would be this:

                            [php]echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">' ;[/php]

                            Double-quotes should be used for HTML attributes. Using single-quotes makes the HTML code invalid in the HTML 4.0, and XHTML 1.0 specs.

                            Plus, Double-quotes are parseable, but using single-quotes is better because they are not parsed for vairables. This speeds up your PHP apps because PHP doesn't have to parse that stuff.

                            Sean

                            Comment

                            • ronverdonk
                              Recognized Expert Specialist
                              • Jul 2006
                              • 4259

                              #15
                              Whoever told you that?

                              Ronald :cool:

                              Comment

                              Working...