PHP/MySQL "UPDATE" question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    #1

    PHP/MySQL "UPDATE" question

    A beginner in this area, I have been able to read a record from a
    MySQL database and populate an HTML form (wow!). Now, my goal is to
    allow the user to edit the contents of the form and then update the
    record in MySQL.

    The problem is, as soon as the "Update" button (type="submit") is
    pressed, all of the data disappear from the form. How can that be
    prevented?

    Here is my code:

    <body>
    <?php

    // Select the Music database
    mysql_select_db ("music") or die("Could not connect to Music
    database");

    // Perform SQL query
    $sql = "select * from cdcollection where Artist = '$txtArtistSear ch'";
    $result = mysql_query($sq l) or die("SQL Select failed");
    $row = mysql_fetch_arr ay($result,MYSQ L_ASSOC);

    ?>

    <?php

    if ($cmdUpdate) {
    // Select the Music database
    mysql_select_db ("music") or die("Could not connect to Music
    database");

    $sql = "UPDATE cdcollection SET Notes='$txtNote s' WHERE Artist =
    '$txtArtistSear ch'" ;
    $result = mysql_query($sq l) or die("SQL Update failed");
    }

    ?>

    <form method="post" action="<?php echo $PHP_SELF;?>">

    <p>Artist&nbsp; &nbsp;&nbsp;
    <input type="text" name="txtArtist " size="19" value="<?php echo
    $row['Artist'] ?>"> </p>
    <p>Title&nbsp;& nbsp;&nbsp;&nbs p;
    <input type="text" name="txtTitle" size="39" value="<?php echo
    $row['Title'] ?>"></p>
    <p>Genre&nbsp;& nbsp;
    <input type="text" name="txtGenre" size="13" value="<?php echo
    $row['Genre'] ?>"></p>
    <p>Rating&nbs p;
    <input type="text" name="txtRating " size="3" value="<?php echo
    $row['Rating'] ?>"></p>
    <p>Notes&nbsp;& nbsp;
    <input type="text" name="txtNotes" size="84" value="<?php echo
    $row['Notes'] ?>"></p>
    <p>

    <input type="button" value="Search" name="cmdSearch ">&nbsp;&nb sp;
    <input type="submit" value="Update" name="cmdUpdate "></p>

    </form>

    </body>

    </html>

    Thanks!!
    Mark Jones
  • Rahul Anand

    #2
    Re: PHP/MySQL &quot;UPDATE&qu ot; question

    himilecyclist@y ahoo.com (Mark) wrote in message news:<5e55e3c8. 0312050854.70d4 2343@posting.go ogle.com>...[color=blue]
    > A beginner in this area, I have been able to read a record from a
    > MySQL database and populate an HTML form (wow!). Now, my goal is to
    > allow the user to edit the contents of the form and then update the
    > record in MySQL.
    >
    > The problem is, as soon as the "Update" button (type="submit") is
    > pressed, all of the data disappear from the form. How can that be
    > prevented?
    >
    > Here is my code:
    >
    > <body>
    > <?php
    >
    > // Select the Music database
    > mysql_select_db ("music") or die("Could not connect to Music
    > database");
    >
    > // Perform SQL query
    > $sql = "select * from cdcollection where Artist = '$txtArtistSear ch'";
    > $result = mysql_query($sq l) or die("SQL Select failed");
    > $row = mysql_fetch_arr ay($result,MYSQ L_ASSOC);
    >
    > ?>
    >
    > <?php
    >
    > if ($cmdUpdate) {
    > // Select the Music database
    > mysql_select_db ("music") or die("Could not connect to Music
    > database");
    >
    > $sql = "UPDATE cdcollection SET Notes='$txtNote s' WHERE Artist =
    > '$txtArtistSear ch'" ;
    > $result = mysql_query($sq l) or die("SQL Update failed");
    > }
    >
    > ?>
    >
    > <form method="post" action="<?php echo $PHP_SELF;?>">
    >
    > <p>Artist&nbsp; &nbsp;&nbsp;
    > <input type="text" name="txtArtist " size="19" value="<?php echo
    > $row['Artist'] ?>"> </p>
    > <p>Title&nbsp;& nbsp;&nbsp;&nbs p;
    > <input type="text" name="txtTitle" size="39" value="<?php echo
    > $row['Title'] ?>"></p>
    > <p>Genre&nbsp;& nbsp;
    > <input type="text" name="txtGenre" size="13" value="<?php echo
    > $row['Genre'] ?>"></p>
    > <p>Rating&nbs p;
    > <input type="text" name="txtRating " size="3" value="<?php echo
    > $row['Rating'] ?>"></p>
    > <p>Notes&nbsp;& nbsp;
    > <input type="text" name="txtNotes" size="84" value="<?php echo
    > $row['Notes'] ?>"></p>
    > <p>
    >
    > <input type="button" value="Search" name="cmdSearch ">&nbsp;&nb sp;
    > <input type="submit" value="Update" name="cmdUpdate "></p>
    >
    > </form>
    >
    > </body>
    >
    > </html>
    >
    > Thanks!!
    > Mark Jones[/color]

    Hi,

    You should update the record before fetching from database.
    your code should be: -

    // Start of script
    <?php

    // Select the Music database
    mysql_select_db ("music") or die("Could not connect to Music
    database");

    // Update the database if form posted
    if (!empty($_POST['cmdUpdate'])
    {
    $sql = "UPDATE cdcollection SET Notes='$txtNote s' WHERE Artist =
    '$txtArtistSear ch'" ;
    $result = mysql_query($sq l) or die("SQL Update failed");
    }


    // Fecth from database

    $sql = "select * from cdcollection where Artist = '$txtArtistSear ch'";
    $result = mysql_query($sq l) or die("SQL Select failed");
    $row = mysql_fetch_arr ay($result,MYSQ L_ASSOC);

    ?>

    <html>
    <body>
    <form method="post" action="<?php echo $PHP_SELF;?>">

    <p>Artist&nbsp; &nbsp;&nbsp;
    <input type="text" name="txtArtist " size="19" value="<?php echo
    $row['Artist'] ?>"> </p>
    <p>Title&nbsp;& nbsp;&nbsp;&nbs p;
    <input type="text" name="txtTitle" size="39" value="<?php echo
    $row['Title'] ?>"></p>
    <p>Genre&nbsp;& nbsp;
    <input type="text" name="txtGenre" size="13" value="<?php echo
    $row['Genre'] ?>"></p>
    <p>Rating&nbs p;
    <input type="text" name="txtRating " size="3" value="<?php echo
    $row['Rating'] ?>"></p>
    <p>Notes&nbsp;& nbsp;
    <input type="text" name="txtNotes" size="84" value="<?php echo
    $row['Notes'] ?>"></p>

    <p><input type="button" value="Search" name="cmdSearch ">&nbsp;&nb sp;
    <input type="submit" value="Update" name="cmdUpdate "></p>

    </form>

    </body>
    </html>

    // End of Script

    regards,

    rahul

    Comment

    • Mark

      #3
      Re: PHP/MySQL &quot;UPDATE&qu ot; question

      I'm still stumped on this one. I forwarded my code to you, Rahul.

      As soon as I click the "Update" button (type="submit") , ALL the
      variables are cleared, including those on the form, the ones on the
      calling HTML form and any that I declare inside the script. This is
      true regardless of the order of the fetch and update logic within the
      code.

      How can I execute an SQL "Update" without losing everything? I assume
      the data loss is caused by the button type="submit".

      Thanks again!


      rahulanand_bis@ rediffmail.com (Rahul Anand) wrote in message news:<628e2f7b. 0312060106.4132 bfb9@posting.go ogle.com>...[color=blue]
      > himilecyclist@y ahoo.com (Mark) wrote in message news:<5e55e3c8. 0312050854.70d4 2343@posting.go ogle.com>...[color=green]
      > > A beginner in this area, I have been able to read a record from a
      > > MySQL database and populate an HTML form (wow!). Now, my goal is to
      > > allow the user to edit the contents of the form and then update the
      > > record in MySQL.
      > >
      > > The problem is, as soon as the "Update" button (type="submit") is
      > > pressed, all of the data disappear from the form. How can that be
      > > prevented?
      > >
      > > Here is my code:
      > >
      > > <body>
      > > <?php
      > >
      > > // Select the Music database
      > > mysql_select_db ("music") or die("Could not connect to Music
      > > database");
      > >
      > > // Perform SQL query
      > > $sql = "select * from cdcollection where Artist = '$txtArtistSear ch'";
      > > $result = mysql_query($sq l) or die("SQL Select failed");
      > > $row = mysql_fetch_arr ay($result,MYSQ L_ASSOC);
      > >
      > > ?>
      > >
      > > <?php
      > >
      > > if ($cmdUpdate) {
      > > // Select the Music database
      > > mysql_select_db ("music") or die("Could not connect to Music
      > > database");
      > >
      > > $sql = "UPDATE cdcollection SET Notes='$txtNote s' WHERE Artist =
      > > '$txtArtistSear ch'" ;
      > > $result = mysql_query($sq l) or die("SQL Update failed");
      > > }
      > >
      > > ?>
      > >
      > > <form method="post" action="<?php echo $PHP_SELF;?>">
      > >
      > > <p>Artist&nbsp; &nbsp;&nbsp;
      > > <input type="text" name="txtArtist " size="19" value="<?php echo
      > > $row['Artist'] ?>"> </p>
      > > <p>Title&nbsp;& nbsp;&nbsp;&nbs p;
      > > <input type="text" name="txtTitle" size="39" value="<?php echo
      > > $row['Title'] ?>"></p>
      > > <p>Genre&nbsp;& nbsp;
      > > <input type="text" name="txtGenre" size="13" value="<?php echo
      > > $row['Genre'] ?>"></p>
      > > <p>Rating&nbs p;
      > > <input type="text" name="txtRating " size="3" value="<?php echo
      > > $row['Rating'] ?>"></p>
      > > <p>Notes&nbsp;& nbsp;
      > > <input type="text" name="txtNotes" size="84" value="<?php echo
      > > $row['Notes'] ?>"></p>
      > > <p>
      > >
      > > <input type="button" value="Search" name="cmdSearch ">&nbsp;&nb sp;
      > > <input type="submit" value="Update" name="cmdUpdate "></p>
      > >
      > > </form>
      > >
      > > </body>
      > >
      > > </html>
      > >
      > > Thanks!!
      > > Mark Jones[/color]
      >
      > Hi,
      >
      > You should update the record before fetching from database.
      > your code should be: -
      >
      > // Start of script
      > <?php
      >
      > // Select the Music database
      > mysql_select_db ("music") or die("Could not connect to Music
      > database");
      >
      > // Update the database if form posted
      > if (!empty($_POST['cmdUpdate'])
      > {
      > $sql = "UPDATE cdcollection SET Notes='$txtNote s' WHERE Artist =
      > '$txtArtistSear ch'" ;
      > $result = mysql_query($sq l) or die("SQL Update failed");
      > }
      >
      >
      > // Fecth from database
      >
      > $sql = "select * from cdcollection where Artist = '$txtArtistSear ch'";
      > $result = mysql_query($sq l) or die("SQL Select failed");
      > $row = mysql_fetch_arr ay($result,MYSQ L_ASSOC);
      >
      > ?>
      >
      > <html>
      > <body>
      > <form method="post" action="<?php echo $PHP_SELF;?>">
      >
      > <p>Artist&nbsp; &nbsp;&nbsp;
      > <input type="text" name="txtArtist " size="19" value="<?php echo
      > $row['Artist'] ?>"> </p>
      > <p>Title&nbsp;& nbsp;&nbsp;&nbs p;
      > <input type="text" name="txtTitle" size="39" value="<?php echo
      > $row['Title'] ?>"></p>
      > <p>Genre&nbsp;& nbsp;
      > <input type="text" name="txtGenre" size="13" value="<?php echo
      > $row['Genre'] ?>"></p>
      > <p>Rating&nbs p;
      > <input type="text" name="txtRating " size="3" value="<?php echo
      > $row['Rating'] ?>"></p>
      > <p>Notes&nbsp;& nbsp;
      > <input type="text" name="txtNotes" size="84" value="<?php echo
      > $row['Notes'] ?>"></p>
      >
      > <p><input type="button" value="Search" name="cmdSearch ">&nbsp;&nb sp;
      > <input type="submit" value="Update" name="cmdUpdate "></p>
      >
      > </form>
      >
      > </body>
      > </html>
      >
      > // End of Script
      >
      > regards,
      >
      > rahul[/color]

      Comment

      Working...