2017-09-25 138 views
0

我是PHP的初學者,我想編輯一個表格,但在此之前我想從這個表格中檢索數據並修改它們之後。 這裏是我的表:檢索和修改數據

employe(id_em,cin,nom_em,id_grade,id_affectation,adress)) 

我通過選擇一個ID員工在這裏獲取的數據第一件事是代碼:

<form method="post"action="recherche.php"> 
<select name="id_em" > 
<?php 
include'cone.php'; 
$res= $bdd->query('select * from employe'); 

while ($ligne=$res->fetch()) 
{?> 
    <option > <?php echo $ligne['id_em']; ?> </option> 
<?php } 
$res->closeCursor(); 
?> 
</select> 
<input type="submit" value="chercher"> 
</form> 

點擊按鈕另一個頁面顯示與員工信息後:

<form method="post"action="update.php"> 
<?php 
include'cone.php'; 
error_reporting(E_ALL); 
ini_set ('display_errors', 1); 
try{ 

$requete=$bdd->prepare("select employe.cin,employe.nom_em,grade.libelle,affectation.nom_a,employe.adress from employe,grade,affectation where employe.id_grade=grade.id and employe.id_affectation=affectation.num and employe.id_em=?"); 
$requete->execute(array($_POST["id_em"])); 
if(isset($_POST['id_em'])){ 
while($res1=$requete->fetch()){ 
?> 

<input type="text"name="cin" value="<?php echo $res1["cin"] ?>"> 
</br> 
<input type="text"name="nom_em" value="<?php echo $res1["nom_em"] ?>"> 
</br> 
<input type="text"name="id_grade" value="<?php echo $res1["libelle"] ?>"> 
</br> 
<input type="text"name="id_affectation" value="<?php echo $res1["nom_a"] ?>"> 
</br> 
<input type="text"name="adress" value="<?php echo $res1["adress"] ?>"> 
</br> 
<?php 
}} 
$requete->closecursor(); 
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
} catch (PDOException $e) { 
    die('Erreur : ' . $e->getMessage()); 
} 
?> 
</br> 
</br></br> 

    </select> 
<input type="submit"value="ok"> 

</form> 

我點擊第2頁的編輯按鈕此代碼調用函數修改:

<?php 
    include'manager.php'; 
    $manager=new manager(); 
    $manager->update1(); 
    ?> 

和這裏的功能:

public function update1() { 
$this->makes(); 
    var_dump($_POST['id_em']); 

    if (isset($_POST["cin"]) and isset($_POST["nom_em"]) and isset($_POST["id_grade"]) and 
     isset($_POST["id_affectation"]) and isset($_POST["adress"]) and isset($_POST["id_em"])) 
    { 
     echo 'IF'; 
     $this->st=$this->pdo->prepare("update employe set cin=?, nom_em=?, id_grade=?, id_affectation=?, adress=? where id_em=?"); 
     return $this->st->execute(array($_POST["cin"], $_POST["nom_em"], $_POST["id_grade"], $_POST["id_affectation"], $_POST["adress"], $_POST["id_em"])); 
    } 
    else 
    { 
     echo 'ELSE'; 
    } 
} 

問題沒有什麼變化的任何修改。 的信息我已經測試了我的功能,它工作得很好。 但問題是當我想顯示和編輯。 並感謝您的幫助

+0

你有沒有價值''

+0

2017-09-25 12:49:49

+0

注意:如果'

回答

0

添加到HTML部分的價值選擇:

<option value="<?php echo $ligne['id_em']; ?>" > <?php echo $ligne['id_em']; ?> </option> 

這裏更好的所有文件不僅部分代碼!

這裏第一部分是如何正確地獲取數據:

<?php 
session_start(); 
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING); 


// mysql settings change here !!! 
$mdatabase = 'test'; 
$muser = 'root'; 
$mpass = 'toor'; 
$mhost = 'localhost'; 
$mport = 3306; 

try{ 
    // Connect to database 
    $db = Conn(); 

    // select data 
    $r = $db->query('SELECT * FROM employe'); 
    while($row = $r->fetch(PDO::FETCH_ASSOC)){ 
    // $r->rowCount(); 
     $option = $option . '<option value="'.$row['id_em'].'">'.$row['id_em'].'</option>'; 
    } 
}catch(Exception $e){ 
    print_r($e); 
} 

// Add link function 
function saveLink($link){ 
    global $db; 
    if (!empty($link)) { 
     $link = htmlentities($link,ENT_QUOTES,'UTF-8'); 
     $r = $db->query("INSERT INTO test(content) VALUES('$link')"); 
     // last inserted id if you had auto increment primary key id (int or bigint) 
     $id = $db->lastInsertId(); 
     return $id; 
    }else{ 
     return 0; 
    } 
} 

// connect to mysql with PDO function 
function Conn(){ 
    global $mhost,$mport,$muser,$mpass,$mdatabase; 
    $connection = new PDO('mysql:host='.$mhost.';port='.$mport.';dbname='.$mdatabase.';charset=utf8', $muser, $mpass); 
    // don't cache query 
    $connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
    // show warning text 
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 
    // throw error exception 
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    // don't colose connecion on script end 
    $connection->setAttribute(PDO::ATTR_PERSISTENT, false); 
    // set utf for connection utf8_general_ci or utf8_unicode_ci 
    $connection->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8' COLLATE 'utf8_general_ci'"); 
    return $connection; 
} 

?> 

<form method="post" action="recherche.php"> 
<select name="id_em" > 
<?php 
echo $option; 
?> 
</select> 
<input type="submit" value="chercher"> 

</form> 

// employe(id_em,cin,nom_em,id_grade,id_affectation,adress)