How to make one form to add or change, search the database?

How to do a search in the database for that file, either add or edit? Not to do of several forms. If there is certainly such an option
spoiler
<!doctype html>
<html lang="EN">
the <head>
<title>List all customers and their presence in the database of website</title>
<meta charset="utf-8">
</head>
the <body>
<?php
 // Database connection
 include ('config.php');

 //If the variable name passed
 if (isset($_POST["name"])) {
 //If it is update request, updating
 if (isset($_GET['red_id'])) {
 $sql = mysqli_query($link, "UPDATE `users` SET `name` = '{$_POST['name']}',`category` = '{$_POST['category']}',`region` = '{$_POST['region']}',`money` = '{$_POST['money']}',`do_money` = '{$_POST['do_money']}',`area` = '{$_POST['area']}',`do_area` = '{$_POST['do_area']}',`etaj` = '{$_POST['etaj']}',`do_etaj` = '{$_POST['do_etaj']}',`tip-doma` = '{$_POST['tip-doma']}',`phone` = '{$_POST['phone']}',`premich` = '{$_POST['premich']}',`time` = '{$_POST['time']}' WHERE `id`={$_GET['red_id']}");
 } else {
 //Otherwise, insert the data, putting them in the query
 $sql = mysqli_query($link, "INSERT INTO `users` (`name`, `category`, `region`, `money`, `do_money`, `area`, `do_area`, `etaj`, `do_etaj`, `tip-home`, `phone`, `premich`, `time`) VALUES ('{$_POST['name']}', '{$_POST['category']}', '{$_POST['region']}', '{$_POST['money']}', '{$_POST['do_money']}', '{$_POST['area']}', '{$_POST['do_area']}', '{$_POST['etaj']}', '{$_POST['do_etaj']}', '{$_POST['tip-doma']}', '{$_POST['phone']}', '{$_POST['premich']}', '{$_POST['time']}')");
}

 //If the insert was successful
 if ($sql) {
 echo '<script type="text/javascript"> ';
 echo '$.jGrowl("Successfully!", { 
 theme: "green"});';
 } else {
 echo '$.jGrowl("an error Occurred:' . mysqli_error($link) . '", { 
 theme: "red"
});';
}
 echo '</script>';
}

 if (isset($_GET['del_id'])) { //check whether the variable
 //delete the row from the table
 $sql = mysqli_query($link, "DELETE FROM `users` WHERE `id` = {$_GET['del_id']}");
 if ($sql) {
 echo '<script type="text/javascript"> ';
 echo '$.jGrowl("User deleted!", { 
 theme: "green"});';
 } else {
 echo '$.jGrowl("an error Occurred:' . mysqli_error($link) . '", { 
 theme: "red"
});';
}
 echo '</script>';
}

 //If the passed variable red_id, it is necessary to update the data. To start we'll get them from the database
 if (isset($_GET['red_id'])) {
 $sql = mysqli_query($link, "SELECT * FROM `users` WHERE `id`={$_GET['red_id']}");
 $product = mysqli_fetch_array($sql);
}
$dt=date('d-my');
 if (isset($_GET['red_id'])) {
 $edit = 'edit customer';
 }else {
 $add = 'Add client database';
}

 // Search database

 ?> 
 <form action="" method="post">
 <div class="resultat">
 <div class="box-header">
 <div class="title"><?= $add ?><?= $edit ?> <?= isset($_GET['red_id']) ? $product['name'] : "; ?></div>
</div>
 <div class="box-content">
<table>
the <tr>
<td>Name:</td>
 <td><input class="styler" type="text" name="name" value="<?= isset($_GET['red_id']) ? $product['name'] : "; ?>"></td>
</tr>
the <tr>
<td>Phone:</td>
 <td><input class="styler" required id="phone1" type="text" name="phone" value="<?= isset($_GET['red_id']) ? $product['phone'] : "; ?>"></td>
</tr>
the <tr>
<td>Category:</td>
<td>
 <select class="StyleSelectBox" name="category" x-autocompletetype="name">
 <option value=""></option>

 <option value="village House" <?= $product['category']=='village House' ? 'selected' : "; ?>>in the village</option>
</select>
</td>
</tr>
the <tr>
<td>District:</td>
<td>
 <select class="StyleSelectBox" name="region" x-autocompletetype="name">
 <option value=""></option>
 <option value="Center" <?= $product['region']=='Center' ? 'selected' : "; ?>>Center</option>
</select>
</td>
</tr>
the <tr>
 <td>Budget ( € ):</td>
 <td><input placeholder="from" class="styler" type="text" name="money" value="<?= isset($_GET['red_id']) ? $product['money'] : "; ?>"> <input placeholder="to" class="styler" type="text" name="do_money" value="<?= isset($_GET['red_id']) ? $product['do_money'] : "; ?>"></td>
</tr>
the <tr>
 <td>Area (m2):</td>
 <td><input placeholder="from" class="styler" type="text" name="area" value="<?= isset($_GET['red_id']) ? $product['area'] : "; ?>"> <input placeholder="to" class="styler" type="text" name="do_area" value="<?= isset($_GET['red_id']) ? $product['do_area'] : "; ?>"></td>
</tr>
the <tr>
<td>Floor:</td>
<td>
 <input placeholder="from" class="styler" type="text" name="etaj" value="<?= isset($_GET['red_id']) ? $product['etaj'] : "; ?>"> <input placeholder="to" class="styler" type="text" name="do_etaj" value="<?= isset($_GET['red_id']) ? $product['do_etaj'] : "; ?>">
</td>
</tr>
the <tr>
 <td>house Type:</td>
<td>
 <select class="StyleSelectBox" name="tip-home" x-autocompletetype="tip-home">
 <option value=""></option>
 <option value="Monolithic" <?= $product['tip-home']=='Monolithic' ? 'selected' : "; ?>>Monolithic</option>
</select>
</td>
</tr>
the <tr>
<td>note:</td>
 <td><textarea class="styler" rows="5" cols="40" name="premich"><?= isset($_GET['red_id']) ? $product['premich'] : "; ?></textarea></td>
</tr>
 <tr style="display:none;">
<td>date:</td>
 <td><input class="styler" type="text" name="time" value="<?= $dt ?>"></td>
</tr>
the <tr>
 <td colspan="2"><input class="btn btn-success btn-sm" type="submit" value="Save"><input type="reset" class="btn btn-danger btn-sm" onclick="window.location='all_users(new).php'" value="Cancel"></td>
</tr>
</table>
</div>
</div>
</form>
 <div class="resultat">
 <div class="box-header">
 <div class="title">Search specialist in database</div>
</div>
 <div class="box-content1">
 <form method="post">
 What you're looking for:<br> <input name="name"> <br>
 <input type=submit value="Search">
</form>
</div>
</div>
<div class="resultat">
 <div class="box-header">
 <div class="title">List all customers and their presence in the database of website</div>
</div>
 <div class="box-content">
 <table id="example" class="display nowrap" style="width:100%">
<thead>
the <tr>
<th>#</th>
<th>Name</th>
<th>Phone</th>
<th>Category</th>
<th>District</th>
 <th>Budget</th>
 <th>Budget prior</th>
<th>Area</th>
<th>Floor</th>
 <th>house Type</th>
<th>note</th>
<th>date</th>
<th>Manage</th>
</thead>
</tr>
<?php
 $sql = mysqli_query($link, 'SELECT * FROM `users`);
 while ($result = mysqli_fetch_array($sql)) {
 echo '<tr>' .
 "<td>{$result['id']}</td>" .
 "<td>{$result['name']}</td>" .
 "<td>{$result['phone']}</td>" .
 "<td>{$result['category']}</td>" .
 "<td>{$result['region']}</td>" .
 "<td>{$result['money']} RUB</td>" .
 "<td>{$result['do_money']} RUB</td>" .
 "<td>{$result['area']}</td>" .
 "<td>{$result['etaj']}</td>" .
 "<td>{$result['tip-home']}</td>" .
 "<td>{$result['premich']}</td>" .
 "<td>{$result['time']}</td>" .
 "<td><a href='?red_id={$result['id']}' title='Edit'><i class='fa fa-pencil' aria-hidden='true'></i></i></a> 
 <a href='?del_id={$result['id']}' title='Remove'><i class='fa fa-trash' aria-hidden='true'></i></a></td>" .
'</tr>';
}
?>
<tfoot>
the <tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</tfoot>
</table>
 </div><a class="btn btn-success btn-sm" href="?add=new">Add new client</a>
</div>
</div>
</body>
</html>
April 4th 20 at 13:05
1 answer
April 4th 20 at 13:07
To do creation on post method, editing on patch method, obtaining for the get method.
and it is possible a little example? add there's a change there, but realise I can not understand( - zola.Herzog commented on April 4th 20 at 13:10
@zola.Herzog, https://webdevkin.ru/posts/backend/restful-servis-... only instead of ajax to do input with the name _method in which to put the value of the method (put, for example). - aidan_Wilderman27 commented on April 4th 20 at 13:13
@aidan_Wilderman27, example please? - zola.Herzog commented on April 4th 20 at 13:16

Find more questions by tags PHPMySQL