Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 45
  1. #1
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    A database from which users can make choices and save them

    Hello folks I am new to php and I have been trying to put together a database that a user can search and choose from the results. I have managed to make this script by copying code from google searches and trial and error.
    The script so far has been tested and works.
    The hard part is the code for choosing from the results, I have tried some things but I have been far from the mark, the thing is I can't get my head around the problem, if the first field is a number which is unique to each row, how can I pick that up in a php argument.
    I have tried making the first field an href link to send that number to a different table which would collect the results of the users choices, but I'm just not sure what to put in the code.
    Could someone throw me a lifeline here I've searched for hours on google to find any code that looks like it would work with no luck.


    PHP Code:
    // Get the search variable from URL
      
      $var = @$_GET['a'] ;
      $trimmed1 = trim($var); //trim whitespace from the stored variable
          
      $var = @$_GET['b'] ;
      $trimmed2 = trim($var);
      
      $var = @$_GET['c'] ;
      $trimmed3 = trim($var);
      
      $var = @$_GET['d'] ;
      $trimmed4 = trim($var);
      
      $var = @$_GET['e'] ;
      $trimmed5 = trim($var);
      
      $var = @$_GET['f'] ;
      $trimmed6 = trim($var);


     //connect to your database
    mysql_connect("localhost","root",""); //(host, username, password)

    //specify database 
    mysql_select_db("a2149809_MV") or die("Unable to select database"); //select which database we're using

    // Build SQL Query  
    $query = "SELECT *  FROM `table` WHERE `field1` LIKE \"%$trimmed1%\" AND `field2` LIKE \"%$trimmed2%\" AND `field3` LIKE \"%$trimmed3%\" 
      AND `field4` LIKE \"%$trimmed4%\" AND `field5` LIKE \"%$trimmed5%\" AND `field6` LIKE \"%$trimmed6%\" order by `field1`";  
      
    $result=mysql_query($query);
    $num=mysql_num_rows($result);

    mysql_close();



        <table width="100%" border=2 cellspacing=2 cellpadding=2>

        

    <tr><form name="form" action="" method="get">

        

    <td colspan="6"><input type="submit" name="Submit" value="Search" />

        

    </td>

        

    </tr>

        

    <tr>

        

    <td><input type="text" name="a" value="" size="4" /></td>

        

    <td><input type="text" name="b" value="" size="40" /></td>
            <td><input type="text" name="c" value="" size="3"  /></td>

        

    <td><input type="text" name="d" value="" size="10" /></td>

        

    <td><input type="text" name="e" value="" size="10" /></td>

        

    <td><input type="text" name="f" value="" size="10" /></td>

        

    </form></tr>

        


        


        

             

    <?php
    $i
    =0;
    while (
    $i $num) {

    $f1=mysql_result($result,$i,"Field1");
    $f2=mysql_result($result,$i,"Field2");
    $f3=mysql_result($result,$i,"Field3");
    $f4=mysql_result($result,$i,"Field4");
    $f5=mysql_result($result,$i,"Field5");
    $f6=mysql_result($result,$i,"Field6");

    ?>

                     <tr>
                         <td><?php echo $f1?></td>
                         <td><?php echo $f2?></td>

        

                 <td><?php echo $f3?></td>

        

                 <td><?php echo $f4?></td>

        

                 <td><?php echo $f5?></td>

        

                 <td><?php echo $f6?></td>

        

        

    </tr>


        

        


    <?php
    $i
    ++;
    }
    ?>
    </table>

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    I'm not sure what you mean by "pick that up in a PHP argument". I assume you mean variable, as argument is what you pass a function, and I don't see any function you're referring to here. With the indiscript field names, its difficult to figure out what you're trying to do, thus hard to say what you want. I strongly suspect your DB is setup poorly, but you mentioned you're new, so its excusable, but you'd be surprised how an efficient DB setup makes a lot of problems easier. There are a few things you can fix in the code immediately, such as

    PHP Code:
    <?php
    $i
    =0;
    while (
    $i $num) {

    $f1=mysql_result($result,$i,"Field1");
    $f2=mysql_result($result,$i,"Field2");
    $f3=mysql_result($result,$i,"Field3");
    $f4=mysql_result($result,$i,"Field4");
    $f5=mysql_result($result,$i,"Field5");
    $f6=mysql_result($result,$i,"Field6");

    ?>

                     <tr>
                         <td><?php echo $f1?></td>
                         <td><?php echo $f2?></td>

        

                 <td><?php echo $f3?></td>

        

                 <td><?php echo $f4?></td>

        

                 <td><?php echo $f5?></td>

        

                 <td><?php echo $f6?></td>

        

        

    </tr>


        

        


    <?php
    $i
    ++;
    }
    ?>
    Ignoring the overly numerous white spaces, this is an inefficient way to pull data. You're doing 6 function calls on the result, when you only need one:

    PHP Code:
    <?php while ($row mysql_fetch_array($result)) { ?>
    <tr>
        <td><?php echo $row['Field1']; ?></td>
        <td><?php echo $row['Field2']; ?></td>
        <td><?php echo $row['Field3']; ?></td>
        <td><?php echo $row['Field4']; ?></td>
        <td><?php echo $row['Field5']; ?></td>
        <td><?php echo $row['Field6']; ?></td>
    </tr>
    <?php ?>
    So anyway, again, whats the exact issue?

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    I really would suggest you read this before doing any more DB query code:

    http://uk3.php.net/manual/en/functio...ape-string.php

  • #4
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx Keleth and MattF for your replies, I have swapped the code you suggested Keleth and it works the same as before with tidier code, great.

    What I wanted to know was how to allow a user to choose any row of the search results, each row is a single item but I have separated each field into separate tds when I suppose I could have had them in one td.
    It's actually Artist, Title, Disc, Era, Date Added, Aspect Ratio.
    The search is set this way so you could search for all "Beatles" tracks with "love" in the title. The search was like that so I just copied that format to the results.
    If the user could choose a row and have this row added to another table or just print it in another page, like an 'add to cart' button.

    Thanks for any suggestions I hope I have made my problem clear.

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    That's exactly what your primary key column should be about. A unique identifier for any row. In this case, I don't see you having a column as an identifier. People do often use incremental integer, so its always different. If you had that, all you'd have to do is pass that integer and you'd know what row they're referring to.

  • #6
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So Keleth if I had a primary key column what would the code be to pass that to another page or table?, If I could see an example of this i might be able to change it for my own needs but I'm unsure of what to search for..

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    You'd have a table something like the following:

    id (auto_increment)
    field1 (varchar)
    field2 (varchar)
    field3 (varchar)
    primary_key(id)

    That way, the id will automatically increment for each entry you add and the id is the primary key, so you can only ever have one row with that id. You'd then just select the id in your search query and pass that to the next query when you want the specific results for that entry alone.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well, there's no "code to pass it on"... once you have that number, you can do with it as you like. You can pass it on as a GET variable, have some other scheme which works on it.

  • #9
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have created a new field called id with auto increment, so I may be able to figure out how to send the information to a page or table but I'm unclear as to how to get that info from the database, and use it.

    I've put 'id' into the $query
    PHP Code:
    $query "SELECT *  FROM `music vidz` `id` 
    WHERE `Artist` LIKE \"%$trimmed1%\" 
    AND `Title` LIKE \"%$trimmed2%\" 
    AND `Disc` LIKE \"%$trimmed3%\" 
    AND `Era` LIKE \"%$trimmed4%\" 
    AND `Date Added` LIKE \"%$trimmed5%\" 
    AND `AR` LIKE \"%$trimmed6%\" order by `Artist`"

    And then tried $id = $newArray['id']; here:
    PHP Code:
    $var = @$_GET['a'] ;
      
    $trimmed1 trim($var); 
          
      
    $var = @$_GET['b'] ;
      
    $trimmed2 trim($var);
      
      
    $var = @$_GET['c'] ;
      
    $trimmed3 trim($var);
      
      
    $var = @$_GET['d'] ;
      
    $trimmed4 trim($var);
      
      
    $var = @$_GET['e'] ;
      
    $trimmed5 trim($var);
      
      
    $var = @$_GET['f'] ;
      
    $trimmed6 trim($var);
      
      
    $id $newArray['id']; 
    I was then going to make a href to print the variable $id but as you will know it doesn't work, can you help me there?

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Where are you getting $newArray from?

  • #11
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes I know it's rubbish, I am trying anything and everything
    I now have
    PHP Code:
    $var = @$_GET['x'] ; $id = ($var) ; 
    PHP Code:
    $query2 "SELECT *  FROM `music vidz` `id`"
    and
    td><?php echo $row['id']; ?></td>
    <td><?php echo $row['Artist']; ?></td>
    <td><?php echo $row['Title']; ?></td>
    <td><?php echo $row['Disc']; ?></td>
    <td><?php echo $row['Era']; ?></td>
    <td><?php echo $row['Date Added']; ?></td>
    <td><?php echo $row['AR']; ?></td>

    which shows no errors
    just not sure what to do with 'x'

  • #12
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Heh, yah, I'll be honest, I have no idea what's going on anymore. First, the query you put up there makes no sense :P

    What are you trying to do now, show us the code, and tell us the bug.

  • #13
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Heh, yah, I'll be honest, I have no idea what's going on anymore. First, the query you put up there makes no sense :P

    What are you trying to do now, show us the code, and tell us the bug.

  • #14
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here is all the code without the header and host etc

    PHP Code:


      
      $var = @$_GET['a'] ;
      $trimmed1 = trim($var); 
          
      $var = @$_GET['b'] ;
      $trimmed2 = trim($var);
      
      $var = @$_GET['c'] ;
      $trimmed3 = trim($var);
      
      $var = @$_GET['d'] ;
      $trimmed4 = trim($var);
      
      $var = @$_GET['e'] ;
      $trimmed5 = trim($var);
      
      $var = @$_GET['f'] ;
      $trimmed6 = trim($var);
      
      $var = @$_GET['x'] ;
      $id = ($var) ;
      

     
    // Build SQL Query  
    $query = "SELECT *  FROM `music vidz` 
    WHERE `Artist` LIKE \"%$trimmed1%\" 
    AND `Title` LIKE \"%$trimmed2%\" 
    AND `Disc` LIKE \"%$trimmed3%\" 
    AND `Era` LIKE \"%$trimmed4%\" 
    AND `Date Added` LIKE \"%$trimmed5%\" 
    AND `AR` LIKE \"%$trimmed6%\" order by `Artist`"; 

    $query2 = "SELECT *  FROM `music vidz` `id`";
     
    $result=mysql_query($query);
    $num=mysql_num_rows($result);
     
     

    mysql_close();
    ?>



        <table width="100%" border=2 cellspacing=2 cellpadding=2>
        <tr><form name="form" action="" method="get">
        <td colspan="7"><input type="submit" name="Submit" value="Search" />
        <input type="reset" value="Reset" /></td>
        </tr>
        <tr>
        <td>id</td>
        <td>Artist
        <input type="text" name="a" value="" size="40" /></td>
        <td>Title
        <input type="text" name="b" value="" size="40" /></td>
        <td>Disc
        <input type="text" name="c" value="" size="3" maxlength=3 /></td>
        <td>Era
        <select name="d">
        <option value=""></option>
        <option value="50s">50s</option>
        <option value="60s">60s</option>
        <option value="70s">70s</option>
        <option value="80s">80s</option>
        <option value="90s">90s</option>
        <option value="00s">00s</option>
        <option value="10s">10s</option>
        </select></td>
        <td>Date
        <input type="text" name="e" value="" size="10" maxlength="10" /></td>
        <td>A/R
        <select name="f">
        <option value=""></option>
        <option value="4:3">4:3</option>
        <option value="16:9">16:9</option></select></td>
        </form></tr>
        
             

    <?php while ($row mysql_fetch_array($result)) { ?>
    <tr>
        <td><?php echo $row['id']; ?></td>
        <td><?php echo $row['Artist']; ?></td>
        <td><?php echo $row['Title']; ?></td>
        <td><?php echo $row['Disc']; ?></td>
        <td><?php echo $row['Era']; ?></td>
        <td><?php echo $row['Date Added']; ?></td>
        <td><?php echo $row['AR']; ?></td>
        
    </tr>
    <?php ?> 
    </table>

  • #15
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Ok, I see your code now, though I don't understand what some of it does (and a bunch of it is semantically wrong... a form inside a tr for example).

    SELECT * FROM `music vidz` `id` is supposed to do what?

    In fact, the only thing I'm not getting is what you want to do with the ID at the top?


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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