1. Download our Official Android App: Forums for Android!

PHP Help

Discussion in 'Computers' started by Pryomancer, Nov 5, 2010.

  1. Pryomancer

    Pryomancer Android Enthusiast
    Thread Starter
    98

    Mar 29, 2010
    673
    57
    98
    Student
    Southampton, England
    I've got a problem with some PHP code I'm writing.

    The aim is for the user to enter the id of a song from a database.
    The page then reloads with the information of that song displayed automatically in a form. The user can then edit what they need and submit it. That form passes the info to another page which executes the sql update query.

    I've got the first two pages done, where the user enters an id, and then the page reloads to display the autofilled form.
    But it doesn't actually update the records. I've been stuck on it for a number of hours now, I can't find the problem.

    Here's the code for the page that reads in the id and displays the autofilled form:

    PHP:
    1. <?php
    2. //Connect to database
    3. $con = mysql_connect("localhost", "root");
    4.  
    5.  
    6. $a = $_POST["id"];
    7.  
    8. $result = mysql_query ("select * from hits where id='$a'");
    9. while($row = mysql_fetch_array($result))
    10. {
    11.     echo
    12.     "<table><br /><br />
    13.    <a href=\"updatedetails.html\">Back to Update Details</a>
    14.    
    15.    <p>
    16.    <tr>
    17.    <td> ID </td>
    18.    <td> Song </td>
    19.    <td> Artist </td>
    20.    <td> Year </td>
    21.    <td> Amount </td>
    22.    <td> Price </td>
    23.    <td> Genre </td>
    24.    </tr>
    25.    
    26.    <tr>
    27.    <td> $row[ID] <br /><br /></td>
    28.    <td> $row[song] <br /><br /></td>
    29.    <td> $row[artist] <br /><br /></td>
    30.    <td> $row[year] <br /><br /></td>
    31.    <td> $row[amount] <br /><br /></td>
    32.    <td> $row[price] <br /><br /></td>
    33.    <td> $row[genre] <br /><br /></td>
    34.    </tr>
    35.  
    36.    </p>
    37.    </table>";
    38.    
    39. }
    40. ?>
    41.  
    42.  
    43. <?php
    44. $a = $_POST["id"];
    45. //Connect to database
    46. $con = mysql_connect("localhost", "root");
    47.  
    48.  
    49. $result1 = mysql_query ("select * from hits where id='$a'");
    50. $row1 = mysql_fetch_array($result1) ?>
    51.  
    52.  
    53. <table>
    54. <form method="post" action="updatedetails2.php">
    55. <tr><td>ID:</td> <td><input name="id" type="hidden" id="id" value="<?php echo "$row1[ID]";?>"></td><td><?php echo "$row1[ID]";?><br /></td></tr>
    56. <tr><td>Song:</td> <td><input name="song" type="text" id="song" value="<?php echo "$row1[song]";?>"><br /></td></tr>
    57. <tr><td>Artist:</td> <td><input name="artist" type="text" id="artist" value="<?php echo "$row1[artist]";?>"><br /></td></tr>
    58. <tr><td>Year:</td> <td><input name="year" type="text" id="year" value="<?php echo "$row1[year]";?>"><br /></td></tr>
    59. <tr><td>Amount:</td> <td><input name="amount" type="text" id="amount" value="<?php echo "$row1[amount]";?>"><br /></td></tr>
    60. <tr><td>Price:</td> <td><input name="price" type="text" id="price" value="<?php echo "$row1[price]";?>"><br /></td></tr>
    61. <tr><td>Genre:</td> <td><input name="genre" type="text" id="genre" value="<?php echo "$row1[genre]";?>"></td></tr>
    62. <tr><td><input type="submit" value="Confirm"/></td></tr>                                                    
    63. </form>
    64. </table>
    This code outputs:
    [​IMG]



    This is the PHP that handles the information from this form, and executes the sql:

    PHP:
    1. <?php
    2. //Connect to database
    3. $con = mysql_connect("localhost", "root");
    4.  
    5.  
    6. $id = $_POST["id"];
    7. $song = $_POST["song"];
    8. $artist = $_POST["artist"];
    9. $year = $_POST["year"];
    10. $amount = $_POST["amount"];
    11. $price = $_POST["price"];
    12. $genre = $_POST["genre"];
    13.  
    14. mysql_query("UPDATE hits SET song=$song, artist=$artist, year=$year, amount=$amount, price=$price, genre=$genre WHERE id=$id");
    15.  
    16. echo "Records updated.<br /><br />";
    17. echo "<a href=\"updatedetails.html\">Back to Update Details</a>";
    18.  
    19.  
    20. ?>
    The code runs, I don't get any errors, it displays the page saying 'Records updated.' So as far as I can tell the process is not erroneous. It just doesn't update the database.

    Can anyone help?
     

    Advertisement

  2. Pryomancer

    Pryomancer Android Enthusiast
    Thread Starter
    98

    Mar 29, 2010
    673
    57
    98
    Student
    Southampton, England
    So it turns out I just had to use quotes around the variables in the sql update. So simple... Yet I did that before and it didn't work. It must have been something else making it not work that I then fixed.
     

Share This Page

Loading...