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?

Thanks.

<?php
require_once('core/db.php');

 
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.
if(mysql_num_rows($result)==0){ 
        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>
      <tr>
       <th colspan=2 align=center>Update Colors</th></tr>
      <tr>
       <td>
           Track:<br />
           <input type="text" size="30" name="track" value="'.$row['track'].'">
      </td>
      <td>
       Artist:<br />
       <input type="text" size="40" name="artist" value="'.$row['artist'].'">
      </td></tr>

       <tr><td colspan=2 align=center>
        <input type="submit" value="Update">
       </td></tr></table>
      </form>
     ';
 } // close while loop brace
} // close else brace
 
} //close if no action brace
 
// if action = update, check fields, and update the db.
if($_GET['action']=="update"){
 
// 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.
if(empty($_POST['artist'])){ 
        $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.
if(empty($_POST['track'])){ 
        $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
?>

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 article has been dead for over six months. Start a new discussion instead.