删除时的数组到字符串转换

时间:2018-03-08 12:04:24

标签: php mysql sql class dynamic

您好我之前使用的动态删除查询因此我知道它有效,但出于某种原因,当我想要删除用户时,它一直在说数组到字符串转换,你们可以帮助我。

动态删除查询:

public function deleteRow($tabel,$columns,$values)
{
      $this->setQuery('DELETE FROM '.$tabel.' WHERE '.$columns .' = '. $values);

      $this->setStmt($this->getConn()->prepare($this->getQuery()));
      $this->setStmt($this->getStmt()->execute());
      return $this->getQuery();
}

然后我有一个填写用户信息并执行查询的函数:

public function MedewerkerDelete()
{
    $query = new Query();

    $tabel = 'medewerkers'; //welke tabellen zijn er nodig in dit geval is dat customers die hebben we nodig om gebruikers te registreren.
    $columns = array('medewerker_naam', 'medewerker_achternaam','account_id'); //welke gegevens moeten er in de customers tabel komen die komen in deze array te staan alle kolommen dus.

    $tabel2 = 'account';
    $columns2 = array('account_wachtwoord', 'account_email','account_rol');

    echo '

        <form method = "POST"> <!--hier komt het formulier waarmee alle gegevens worden doorgestuurd.-->
        <p>Medewerker Verwijderen</p>
        <label for="medewerker_naam">Naam</label>
        <input type="text" name="medewerker_naam" value=" ' . $_SESSION['vNaam'] . '  "><br>
        <label for="medewerker_achternaam">Achternaam</label>
        <input type="text" name="medewerker_achternaam" value=" ' . $_SESSION['aNaam'] . '  "><br>
        <label for="account_wachtwoord">Wachtwoord</label>
        <input type="password" name="account_wachtwoord"><br>
        <label for="account_email">Email</label>
        <input type="text" name="account_email" value=" ' . $_SESSION['eMail'] . '  "><br>
        <label for="account_rol">Rol</label>
        <input type="text" name="account_rol">
        <input type="submit" name="submit_medewerkers_delete" value="delete">

        </form>

    ';


    if (isset($_POST['submit_medewerkers_delete'])) //als het formulier is ingevuld en verzonden wordt zet dan alle waardes die zijn ingevuld in een array dat is dus de $values.
    {
        $values2 = array(md5($_POST['account_wachtwoord']), $_POST['account_email'],$_POST['account_rol']);
        $query->deleteRow($tabel2, $columns2, $values2);
        $last_id = $query->getConn()->lastInsertId();
        $values = array($_POST['medewerker_naam'],$_POST['medewerker_achternaam'],$last_id); //hier staan alle ingevulde waardes zoals naam is pieter.
        $query->deleteRow($tabel,$columns,$values); //als alles is ingevuld en verstuurd zorg er dan voor dat alle benodigde gegevens zoals tabel kolom en ingevulde waardes worden meegegeven aan de createquery functie en voer het vervolgens uit.
        var_dump($values);
        var_dump($query->getQuery());
    }

}

希望你们能帮助我。我无法弄明白。

3 个答案:

答案 0 :(得分:0)

嗯...为了使函数deleteRow()能够工作,变量$columns$values必须是字符串。 你传递数组。 见
$values2=array(md5($_POST['account_wachtwoord']),$_POST['account_email'],$_POST['account_rol']); $columns2 = array('account_wachtwoord', 'account_email','account_rol');

答案 1 :(得分:0)

您将$ columns和值作为数组发送:

$columns = array('medewerker_naam', 'medewerker_achternaam','account_id');
$values2 = array(md5($_POST['account_wachtwoord']), $_POST['account_email'],$_POST['account_rol']);

然后你尝试用字符串连接这个数组:

$this->setQuery('DELETE FROM '.$tabel.' WHERE '.$columns .' = '. $values);
$values2 = '(' . md5($_POST['account_wachtwoord']) . ', ' . $_POST['account_email'] . ',' . $_POST['account_rol'] . ')';

这是错误的。尝试将$ columns和$ columns2设置为字符串:

$columns = '(medewerker_naam, medewerker_achternaam, account_id)';

答案 2 :(得分:0)

由于您有可变数量的列,因此具有条件,这意味着动态构建语句...

function deleteRow($tabel,$columns,$values)
{
    $conditions = array();
    foreach ( $columns as $ind => $column ) {
        $conditions[] = $column.'="'.$values[$ind].'"';
    }
    $query = 'DELETE FROM '.$tabel.' WHERE '.implode(' and ',$conditions);

    $this->setQuery($query);
    $this->setStmt($this->getConn()->prepare($this->getQuery()));
    $this->setStmt($this->getStmt()->execute());
    return $this->getQuery();
}

这样做是建立每个单独条件的列表,所以像account_email="some@email.address"之类的东西,然后使用implode()将它们全部加在一起,将and放在每个条件之间。然后将其内置到整个查询中......

DELETE FROM account WHERE account_wachtwoord="a" and account_email="b" and account_rol="c"

也可以使用绑定值,具体取决于其他方法的工作方式......

function deleteRow($tabel,$columns,$values)
{
    $conditions = array();
    foreach ( $columns as $ind => $column ) {
        $conditions[] = $column.'=?';
    }
    $query = 'DELETE FROM '.$tabel.' WHERE '.implode(' and ',$conditions);

    $this->setQuery($query);
    $this->setStmt($this->getConn()->prepare($this->getQuery()));
    $this->setStmt($this->getStmt()->execute($values));
    return $this->getQuery();
}