D
D
danyfir2017-10-02 15:12:27
PHP
danyfir, 2017-10-02 15:12:27

How to update database data with php?

The task is to update the "payment" line in the database to the value "Yes" under certain conditions:
meta_value='1328'

According to my logic:
1) We connect to the database via mysql_connect
2) in $result we enter a query to the database to select meta_value=' 1328' (checked, the selection 'SELECT * FROM nametable WHERE meta_value='1328'' works and the necessary rows are returned)
3) through while we create an array of rows issued from the query in paragraph 2
4) in $buf we enter all the post_id (ids) of these rows
5) via 'update_post_meta($buf, 'payment', 'Yes')' we update the payment status to Yes (because wordpress is used) by substituting $buf as the ID of the updated post.

As a result, I wrote the code, but it does not fulfill its function ... Can you tell me where I messed up? Or offer an alternative option (I don’t know Mysql well, and php more or less, therefore, preferably, in more detail with the new version).

$conn = mysql_connect('localhost', 'login', 'password');
 mysql_select_db ("namebd",$conn );
$result = mysql_query ("SELECT * FROM nametable WHERE meta_value='1328'");
    while ($row = $result->fetch_assoc()) {
    $buf = $row['post_id'];
    update_post_meta($buf, 'payment', 'Да');
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vyrkmod, 2017-10-02
@danyfir

We look at "return values" here , slap ourselves on the forehead and instead of "$result->fetch_assoc()" we write "mysql_fetch_assoc($result)". In general, the mysql extension has been deprecated a long time ago, mysqli or PDO should be used instead.

R
rooless, 2017-10-02
@rooless

is the mysql extension still supported?
Try to work with mysli, and it is better with pdo.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question