SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy how to retrieve from database for different page using one php file???

    hi!!

    i now using different php file to retrieve data from the same database table as i have different page that have to retrieve data, but inside the code, only one line change, did you know how to combine all the code into one php file instead of using different php file that is about the same code.

    this is my route1.php-->

    require('db.php');

    mysql_connect(MACHINE, USER, '');
    mysql_select_db(DBNAME);

    $route_id = $_POST['route_id'];
    $from = $_POST['from'];
    $route = $_POST['route'];
    $to = $_POST['to'];
    $description = $_POST['description'];

    $sql = "INSERT INTO route ( 'route_id', 'from', 'route', 'to', 'description')VALUES ( 'NULL', '$from', '$route', '$to', '$description')";
    //$i = 1;

    $sql = "SELECT * FROM route WHERE route_id = '1'";
    $res = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($res)
    while($row = mysql_fetch_array($res)){
    echo "<tr>";
    echo "<td>";
    echo "From" . "&nbsp;" . $row[from]. "," . "&nbsp;";
    echo "you can take" . "<BR>" . $row[route];
    echo "&nbsp;" . "to" . "&nbsp;" . $row[to];
    echo "<BR>" . $row[description], ".";
    echo "</table>";
    }


    this is my drop down menu for the main page

    <p class="style2">Please select : </p>


    <form name="jump">

    <select name="menu" size="3">
    <option value="http://localhost/route1.php">BP</option>
    <option value="http://localhost/route2.php">Sen</option>
    <option value="http://localhost/route3.php">NS</option>
    <option value="http://localhost/route4.php">CG</option>
    </select>
    <input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="GO">
    </p>


    when i click on the option, it will bring me the page where it will retrieve the things i insert from my database.
    but i only change the number on $sql = "SELECT * FROM route WHERE route_id = '1'"; to 2,3 or 4 depend on my row in database.
    for example, option Sen is route_id = '2'

    but i have many page that use the change code but different route_id, if i change one by one, it will take very long and will be take even longer to change then if my database change

    so, how to made the retrieve part into 1 php file??
    please help, as i try this very long but yet i dont know how to do!!
    thank!!!

  2. #2
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change:
    PHP Code:
    $sql "SELECT * FROM route WHERE route_id = '1'"
    To:
    PHP Code:
    $menu = (int) @$_POST['menu'];
    $sql "SELECT * FROM route WHERE route_id = $menu"
    And on your form replace all options, like:
    Code:
    <option value="http://localhost/route1.php">BP</option>
    To:
    Code:
    <option value="1">BP</option>
    Note: you don't have to use quotes around integer values

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But it have error in the route.php page ....it said

    Parse error: parse error, unexpected T_WHILE in C:\Program Files\xampp\xampp\htdocs\fyp\routetest2.php on line 55

    and line 55 is --> while($row = mysql_fetch_array($res)){

    how?

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,805
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    you are missing a closing ; at the end of the line

    Welcome to the forums
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HEY!! thank for the help!!! my page able to retrieve different data for different page!!!

    but i have create a box below the page for me to add in route directly frm there.

    <html>
    <head><b>add route</b></head>
    <FORM METHOD = 'POST' ACTION = 'add_route.php'><br>

    <table width='100' height='50' border='1'>

    <td width='5'>Name input type = 'text' name='name'><BR>
    Email/URLinput type = 'text' name='email'><BR>
    Route: <textarea name = 'message'></textarea><BR>
    Description: <textarea name = 'description'></textarea><BR>

    <br>
    <br>
    <input type = 'submit' value= 'POST'><BR>
    </td>
    </table>
    </form>

    </html>

    <?php
    mysql_connect(MACHINE, USER, '');
    mysql_select_db(DBNAME);

    // check if submitted, then read info and add to DB
    if (isset($_POST['add_route'])) {
    // validation, check if name and message are filled in
    if ($_POST['add_route']!= "" && $_POST['name'] != "")

    $name=$_POST['name'];
    $email=$_POST['email'];
    $add_route=$_POST['add_route'];
    $route_description=$_POST['route_description'];
    $time= time();

    echo "<table border=1>";
    echo "<tr=50>";
    $sql = "INSERT INTO routeinput (name, email, add_route, time) VALUES ('$name', '$email', '$add_route', '$time')";
    // $sql = "INSERT INTO route_input (name, email, add_route, route_description, time) VALUES ('$name', '$email', '$add_route', '$route_description', '$time')";

    mysql_query($sql);

    $sql1 = "SELECT * FROM routeinput";
    $result = mysql_query($sql1);

    while ($row = mysql_fetch_array($result)) {

    $email = $row['email'];
    //$route_description = $row['route_description'];
    $add_route= $row['add_route'];
    $id = $row['id'];
    $time = date('H:i');

    echo "<hr>";
    echo $name;
    echo "<br>";
    echo $email;
    echo "<br>";
    echo $add_route;
    echo "<br>";
    //echo $route_description;
    //echo "<br>";
    echo $time;
    echo "<br>";
    }
    echo "</table>";
    }
    ?>


    but when i press submit, the page doesnt load.
    do you know what happen???
    and i test out this in a new page.....but can i write this code in the same page as my previous code and using the same database table or using a different database like what i doing now???
    if i use select 2 database in one php file, will it mislead the computer?? and will it able to display the right database of what i enter???

  6. #6
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    until nw i still duno how to submit it using the form and insert into database.....cn help??

  7. #7
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i still duno how to insert it if the id is all assign and by using 1 php file
    anyone can help????

    thank!!

  8. #8
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xiao_dolp View Post
    hi!!
    but i have many page that use the change code but different route_id, if i change one by one, it will take very long and will be take even longer to change then if my database change

    so, how to made the retrieve part into 1 php file??
    please help, as i try this very long but yet i dont know how to do!!
    thank!!!
    Where does the information for each page jump selection come from ? Do the values come out of a data base ? You can load these values into a select box dynamically. Tell me how you get the route id and the name you want to use in your select box.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  9. #9
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is part of my new code now
    Code:
    <?php
    
    require('db.php');
    
    mysql_connect(MACHINE, USER, '');
    mysql_select_db(DBNAME);
    
    $id = $_POST['id'];
    $from = $_POST['from'];
    $where = $_POST['where'];
    $to = $_POST['to'];
    $description = $_POST['description'];
    
    $sql = "INSERT INTO where ( 'id', 'from', 'where', 'to', 'description')VALUES ( 'NULL', '$from', '$where', '$to',  '$description')";
    
    $menu = (int) @$_POST['menu'];
    
    $sql = "SELECT * FROM where WHERE id = $menu";
    
    
    $res = mysql_query($sql) or die(mysql_error());
    //$row = mysql_fetch_array($res);
    while($row = mysql_fetch_array($res)){
    echo "<tr>";
    echo "<td>";
    echo "From" . "&nbsp;" . $row[from]. "," . "&nbsp;";
    echo "Please" . "<BR>" . $row[route];
    echo "&nbsp;" . "to" . "&nbsp;" . $row[to];
    echo "<BR>" . $row[description], ".";
    echo "</table>";
    }
    ?>
    
    
        <h3>Feel free to add!!!</h3>
        <h3>&nbsp;</h3>
        <h3> Route:<br />
            <textarea name="where" cols="50" rows="5" height="55"></textarea>
            <br />
            <p> </p>
        </h3>
        <h3>Description:<br />
            <textarea name="description" cols="50" rows="5" height="55"></textarea>
            <br />
            <br />
            <input type="submit" value="Submit" name='sub' />
        </h3>
    <p class="style2">Where you want: </p>
     Please select
               <form name="jump" action="where.php" method="post">
                           
     
      <select name="menu" size="3">
          <option value="1">Bp</option>
        <option value="2">S</option>
    <option value="3">CT</option>
    <option value="4">BG</option>
    
      </select>
      <input type="submit" value="GO">
    </p>
    i have create a box in the where.php page and this page echo out all my information frm the database.
    and i have many different page like Bp.php or S.php and have the same menu to select frm
    What i want is that when i select S in menu, it will go to where.php to retrieve frm database the thing frm s and when i type in the information for s in the box, it will oni display s information.

    and for example, after that i click BG and it will also go where.php, but when i insert BG information in the box, when i submit, it will oni show BG information

    cn this be done???

  10. #10
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What type of s information and BP information do you want to display ?
    To have a page load values based on a chosen variable is easy. Create a frm_handler page that takes the selected value form the POST array and assign it to a session variable. Have the page load based on a query which checks for this session variable and if not set uses a default value.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  11. #11
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for example, in s i want to insert data that display route from place 1 to place 2 but in BP i want to display place 3 to place 4

    what is the code i shd use?


Bookmarks

Posting Permissions

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