R
R
raymon942016-01-15 18:15:45
PHP
raymon94, 2016-01-15 18:15:45

How to update data in mysql when post request is sent?

Good day, there is a direct post request like: domain.ru/form.php?date=2016-01-15 03:00:57&name=toster , which writes id (A_I), time, name to the database.
Everything works as it should, but there was a need for exactly the same request, in the same UPDATE handler (overwrite the name without changing the time and id) data. So, when I access the same link, but changing the name (domain.ru/form.php?date=2016-01-15 03:00:57&name=name) , nothing happens, no data is added, no data is changed, how can this be fixed?
Form code:

require 'connect.php';
$date = trim($_REQUEST['date']);
$name = trim($_REQUEST['name']);

$insert_sql = "INSERT INTO box (date, name)" .
"VALUES('{$date}', '{$name}');";
mysql_query($insert_sql);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
romy4, 2016-01-15
@romy4

read about insert manual, everything is there

S
sivabur, 2016-01-15
@sivabur

mysql_ -Deprecated and will be removed soon. Use mysqli + prepared queries.
replace replace with require_once .

A
Andrey Sedyshev, 2016-01-16
@ musikant777

$insert_sql = "INSERT INTO box (date, name)" .
"VALUES('{$date}', '{$name}')" .
"ON DUPLICATE KEY UPDATE name={$name};";
mysql_query($insert_sql);

The name field in the table must be unique in this case.
Well, as noted above, the mysql_* function has long been deprecated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question