检索和修改数据

时间:2017-09-25 12:39:23

标签: php

我是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页的编辑按钮,此代码调用函数modify:

<?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';
    }
}

问题没有改变任何修改。 对于我已经测试过我的功能的信息,它的效果非常好。 但问题是我想要显示和编辑。 谢谢你的帮助

1 个答案:

答案 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)