I need a little help....
The form displays the data, but when I update any field it updates every field. Can someone please help me fix the issue?



if(!$_GET['action']){ //if no action in url. eg : update.php?action=bleh
//get the information for the form
$query = "select id,artist,track from bridal_songs";
$result = mysql_query($query);
//check result to  see if it's empty or not. if not, print form.
        echo 'There are currently nothing to see here.'; // if nothing. let em know.
} else {
                while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { //while there is something to return, loop.

echo '
      <form action="'.$SERVER['PHP_SELF'].'?action=update" method="post">
     <table border=1 cellpadding=5 cellspacing=2>
       <th colspan=2 align=center>Update Colors</th></tr>
           Track:<br />
           <input type="text" size="30" name="track" value="'.$row['track'].'">
       Artist:<br />
       <input type="text" size="40" name="artist" value="'.$row['artist'].'">

       <tr><td colspan=2 align=center>
        <input type="submit" value="Update">
 } // close while loop brace
} // close else brace
} //close if no action brace
// if action = update, check fields, and update the db.
// check for errors
$err = false; // currently, there is no errors.
$errmsg = '<b>Error<b><br>'; // empty error msg. msg with no current errors.
// check to see if title field is empty or not. if so, make error.
        $err = true; // there is now an error.
        $errmsg .= 'artist is a required field and must be filled out.<br>'; // append an error msg to the end of $errmsg
// check to see if content field is empty or not. if so, make error.
        $err = true; // there is now an error.
        $errmsg .= 'track is a required field and must be filled out.<br>'; // append an error msg to the end of $errmsg

// if there's an error ( $err = true ), let them know and exit.
if($err) { echo $errmsg; exit; }
if(!$err){ // if no errors, do the foloowing
        // store the fields into an easier writeable variable. so you dont have to type $_POST[''] to use it everytime.
        $artist = $_POST['artist'];
        $track = $_POST['track'];

        //connect to the mysql server
        //insert the new info into the database
        $query = "update bridal_songs set artist='".$artist."'";
        $result = mysql_query($query) or die(mysql_error());

        $query = "update bridal_songs set track='".$track."'";
        $result = mysql_query($query) or die(mysql_error());

        echo '<a href="'.$_SERVER['PHP_SELF'].'"> return </a>';
} // close if action = update brace
10 Years
Discussion Span
Last Post by nav33n

Thats because you dont have a condition in your update query.

update bridal_songs set artist='someone';
update bridal_songs set track='something';

would update all the artists with 'someone' and all the tracks with 'something'. Have some condition like,

update bridal_songs set artist='someone' where id='1';
update bridal_songs set track='something' where id='1';
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.