s-melnikov
5/31/2016 - 8:25 AM

bild_query.php

<?php

$params = [];

if ($_POST["firstname"]) {
  $params["firstname"] = $_POST["firstname"];
}
if ($_POST["lastname"]) {
  $params["lastname"] = $_POST["lastname"];
}
if ($_POST["nickname"]) {
  $params["nickname"] = $_POST["nickname"];
}
if ($_POST["password"]) {
  $params["password"] = $_POST["password"];
}

function create_update_query($params) {
  $cols = "";
  $values = "";
  foreach ($params as $key => $val) {
    $cols .= ($cols ? "," : "") . "`$key`";
    $values .= ($values ? "," : "") . "'$val'";
  }
  return "UPDATE ($cols) VALUES ($values)";
}

?>

<pre><?php echo create_update_query($params)?></pre>

<form method="post">
  <input type="text" name="firstname" placeholder="First Name"> <br><br>
  <input type="text" name="lastname" placeholder="Last Name"> <br><br>
  <input type="text" name="nickname" placeholder="Nickname"> <br><br>
  <input type="text" name="password" placeholder="Password"> <br><br>
  <button>Submit</button>
</form>