Grow your CSS skills. Land your dream job.

Passing SQL data between pages

  • # February 19, 2014 at 9:38 pm

    I’m working on a project that has a form page, a list page, and a print page. I’m having trouble passing data between them. The form page inserts a record into mySQL, and adds the record title to the left side of the page. I don’t know how to use the title as a link to display the record in the form. The list page displays the records, with an edit link after each record. I can’t seem to get data to populate the form when the edit link is selected. I have code for updating and deleting a record, but I need to get the record back into the form to do that. It should be obvious that I am new at this, but I’m learning.

    Here’s the code on the form page for listing the titles:

    <?php
    
    //create a database connection
    mysql_connect("localhost","root","root") or die("Error:".mysqlerror());
    //select database
    mysql_select_db("chordcharts");
    
    //create the query
    $result = mysql_query("select * from tunes");
    
    //return the array and loop through each row
    while ($row = mysql_fetch_array($result))
    {
    ?>
    
    <div>&nbsp;&nbsp;&nbsp;<a href=""><?php echo $row['title'];?></a></div>
    
    </ul>
    <?php 
    } 
    ?>
    

    Here’s the code for the list page:

    <?
    $objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
    $objDB = mysql_select_db("chordcharts");
    $strSQL = "SELECT * FROM tunes";
    $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
    ?>
    <table width="600" border="1">
      <tr>
        <th width="91"> <div align="center">ID </div></th>
        <th width="91"> <div align="center">Title </div></th>
        <th width="98"> <div align="center">Chords </div></th>
        <th width="198"> <div align="center">Lyrics </div></th>
        <th width="30"> <div align="center">Edit </div></th>
      </tr>
    <?
    while($objResult = mysql_fetch_array($objQuery))
    {
    ?>
      <tr>
        <td><div align="center"><?=$objResult["id"];?></div></td>
        <td><?=$objResult["title"];?></td>
        <td><?=$objResult["chords"];?></td>
        <td><?=$objResult["lyrics"];?></td>
        <td align="center"><a href="light.php?id=<?=$objResult["title"];?>">Edit</a></td>
      </tr>
    <?
    }
    ?>
    </table>
    <?
    mysql_close($objConnect);
    ?>
    

    Thanks for any help and advice.

    # February 20, 2014 at 10:37 am

    OK – Johnny 5 need more input:

    1st – if you have a record in SQL, there’s no need to pass it from one page to another really. You can just look up the data when you need it and display it.
    2nd – could you narrow down precisely what the issue is? If you give me a narrow view of where the problem is, I’ll be happy to help.

    Advice:

    Using the mysql functions can potentially be dangerous, and sometimes depreciated. It’s likely that in your use-case it doesn’t really matter, but if you want to learn here’s a decent article about comparing mysql, mysqli and PDO: http://code.tutsplus.com/tutorials/php-database-access-are-you-doing-it-correctly–net-25338

    Typically speaking, when you SELECT data, you’ll get it back as an associative array (if you declare it at the time, or you set the defaults to do so). So you’ll want to do some reading up on foreach loops and how to master that. That might be all you need to populate the form.

    DRY – don’t repeat yourself. Working with server-side code, you definitely have no excuses for not using include, require, et al. Once you get the basics down of using simple includes to store functions like database lookups, you can start looking into object oriented PHP which has gotten so much better over the last few releases. Decent overview: http://www.killerphp.com/tutorials/object-oriented-php/

    # February 20, 2014 at 1:41 pm

    +1 @shaneisme

    You can take a look at the way I used PDO in this Gist https://gist.github.com/alenabdula/9083666 and extract the parts you’re interested.

    Hope that helps

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".