[ milos 2 @ 14.06.2013. 14:42 ] @
imam problem sa update i delete nad mysql bazom. ja izvucem lepo sve podatke iz baze u jednu tabelu, zajedno sa ID-jem reda. medjutim kada hocu update ili delete da odradim on konstantno uzima samo poslednji ID iz cele tabele.

ovo je kod koji koristim u fajlu izmeni.php

Code:

//update
if (isset($_POST['updateAutor'])) {
        $autor = new Autor;
        $autor->autorId = $_POST['autorId'];
        $autor->ime = $_POST['ime'];
        $autor->prezime = $_POST['Prezime'];
        $autor->web = $_POST['web'];
        $autor->mejl = $_POST['mejl'];
        //var_dump($autor);
         $autorMethods = new autorMethods;
         $cr = $autorMethods->updateAutor($autor);
         $obavestenje = $cr->message;
     }

//delete
if(isset($_POST['deleteAutor'])) {
        $autorMethods = new autorMethods;
        $cr = $autorMethods->deleteAutor($_POST['autorId']);
        $obavestenje = $cr->message;

//kod koji izvlaci iz baze u tabelu sve podatke
<form action='#' method='post'>
    <table border='1'>
        <tr><td>br</td><td>Ime</td><td>Prezime</td><td>WEB</td><td>e-mail</td><td>izbrisi</td><td>izmeni</td></tr>
    <?php
    
    $autorMethods = new autorMethods;
    $crAutor = $autorMethods->selectAutor();
    
    $counter = 1;
    foreach ($crAutor->value as $autor) {
    print "
    
        <tr>
            <td> " . $counter . " <input type='hidden' name='autorId' value='" . $autor->autorId . "' </td>
            <td><input type='text' name='ime' value='" . $autor->ime . "' size='20' /></td>
            <td><input type='text' name='Prezime' value='" . $autor->prezime . "' size='20' /></td>
            <td><input type='text' name='web' value='" . $autor->web . "' size='20' /></td>
            <td><input type='text' name='mejl' value='" . $autor->mejl . "' size='20' /></td>
            <td> <input type='submit' name='deleteAutor' value='delete'> </td>
            <td> <input type='submit' name='updateAutor' value='update'> </td>
        </tr>
    
    ";
    $counter += 1;
    }
    ?>
    </table>
    </form>


kodovi za select, update i delete iz fajla autorMethods.php
Code:

public function updateAutor($autor) {

            $db = new dbConnect;
            $db->dbConnect_open();

            $validation = new Validation;
            $cr = $validation->validate($autor->autorId, TRUE, "numeric", 1, 5, $db->connection);
            if ($cr->success == FALSE) {
                return $cr;
            } else {
                $autor->autorId = $cr->value;
            }
            $cr = $validation->validate($autor->ime, TRUE, "alphabetic-letters-numbers-spaces", 2, 1000, $db->connection);
            if ($cr->success == FALSE) {
                return $cr;
            } else {
                $autor->ime = $cr->value;
            
            
            $cr = $validation->validate($autor->prezime, TRUE, "alphabetic-letters-numbers-spaces", 2, 1000, $db->connection);
            if ($cr->success == FALSE) {
                return $cr;
            } else {
                $autor->prezime = $cr->value;
            }


            $query = "
                UPDATE 
                    autor 
                SET 
                    autorId = '" . $autor->autorId . "', 
                    ime = '" . $autor->ime . "',
                    Prezime = '" . $autor->prezime . "',
                    web = '" . $autor->web . "',
                    mejl = '" . $autor->mejl . "'
                WHERE 
                    autor.autorId = '" . $autor->autorId . "'
            ";
            $result = mysql_query($query, $db->connection);
            
            $cr = new CommandResponse;
            if ($result == TRUE) {
                $cr->success = TRUE;
                $cr->message = "Autor je uspešno izmenjen.";
            } else {
                $cr->success = FALSE;
                $cr->message = "Došlo je do greske prilikom izmene autora.";
            }
            $cr->value = NULL;

            $db->dbConnect_close();

            return $cr;            
        }
    public function deleteAutor($autorId) {

            $db = new dbConnect;
            $db->dbConnect_open();

            $validation = new Validation;
            $cr = $validation->validate($autorId, TRUE, "numeric", 1, 5, $db->connection);
            if ($cr->success == FALSE) {
                return $cr;
            } else {
                $autorId = $cr->value;
            }
            unset($cr);
            $query = "
                DELETE FROM 
                    autor 
                WHERE 
                    autor.autorId = '" . $autorId . "'
            ";
            $result = mysql_query($query, $db->connection);
            
            $cr = new CommandResponse;

            if ($result == TRUE) {
                $cr->success = TRUE;
                $cr->message = "Autor je uspešno izbrisan.";
            } else {
                $cr->success = FALSE;
                $cr->message = "Došlo je do greške prilikom brisanja autora.";
            }
            $cr->value = NULL;

            $db->dbConnect_close();

            return $cr;
            
        }
public function selectAutor($autorId = NULL) {

            $db = new dbConnect;
            $db->dbConnect_open();
            
            if ($autorId == NULL) {

                $query = "
                    SELECT 
                        * 
                    FROM 
                        autor
                ";
                $result = mysql_query($query, $db->connection);
                $cr = new CommandResponse;
                if (mysql_num_rows($result) == 0) {
                    
                    $cr->success = FALSE;
                    $cr->message = "U bazi podataka ne postoji ni jedan autor.";
                    return $cr;
                }

                $autor1 = Array();
                while ($row = mysql_fetch_assoc($result)) {
                    $autor = new Autor;
                    $autor->autorId = $row["autorId"];
                    $autor->ime = $row["ime"];
                    $autor->prezime = $row["Prezime"];
                    $autor->web = $row["web"];
                    $autor->mejl = $row["mejl"];
                    $autor1[] = $autor;
                }
                $cr = new CommandResponse;
                $cr->success = TRUE;
                $cr->message = "Lista svih autora je uspešno izvučena iz baze podataka.";
                $cr->value = $autor1;

                return $cr;
                
            } else {

                $validation = new Validation;
                $cr = $validation->validate($autorId, TRUE, "numeric", 1, 5, $db->connection);
                if ($cr->success == FALSE) {
                    return $cr;
                } else {
                    $autorId = $cr->value;
                }

                $query = "
                    SELECT 
                        * 
                    FROM 
                        autor 
                    WHERE 
                        autor.autorId = '" . $autorId . "'
                ";
                $result = mysql_query($query, $db->connection);

                $cr = new CommandResponse;
                if (mysql_num_rows($result) == 0) {
                    
                    $cr->success = FALSE;
                    $cr->message = "U bazi podataka ne postoji autor sa ovim Id-om.";
                    return $cr;
                }

                $row = mysql_fetch_assoc($result);
                $autor = new Autor;
                $autor->autorId = $row["autorId"];
                $autor->ime = $row["ime"];
                $autor->prezime = $row["prezime"];
                $autor->web = $row["web"];
                $autor->mejl = $row["mejl"];
                $cr = new CommandResponse;
                $cr->success = TRUE;
                $cr->message = "Podaci o autoru su uspešno izvučeni iz baze podataka.";
                $cr->value = $autor;

                return $cr;
                
            }
        }


ova validacija sluzi samo da mi malo pomogne da ne pisem regularne izraze za svako polje za unos.

ovaj var_dump($autor); koji je trenutno zakomentarisan, mi izbacuje uvek poslednji ID sa svim paraametrima tog ID-ja. U dodatku vam saljem i bazu.

molim za pomoc oko navedenog problema.

u napred hvala
[ milos 2 @ 14.06.2013. 15:03 ] @
reseno, sve je bilo u jednoj formi, a u stvari treba svaki red da bude u posebnoj