Adding a record to the table

Created a new table where there will be a list of users and their points.
I make an entry in the table in this way

global $DB;
$DB->Query("update rub_guess_photo set POINTS=POINTS+0.02 where USER_ID='$userid' limit 1;");

I output in this way:

global $DB;
$DB->Query("SELECT USER_ID FROM rub_guess_photo ORDER BY POINTS DESC LIMIT 10"); 
while ($row = mysql_fetch_array($sql)) {
print_r($row[1]); 
}

But for some reason the entry is not added and displayed. I manually created an entry in this table, tried to display – empty. Where is the error in the first and second cases?


Answer 1, authority 100%

Adding entries happens like this:

INSERT INTO tabe1 (field1, field1) values ('value1','value2');

And UPDATE is an update

Here is an example of adding and outputting:

<?php
 $connect = mysql_connect('localhost','root','12345');
  mysql_select_db('08ist');
  $name=strip_tags($_POST["name"]);
  $data=strip_tags($_POST["data_rojd"]);
  $sql = "INSERT INTO student (name, data_rojdeniya) values ('$name','$data');";
  mysql_query($sql,$connect);
  $sql="SELECT * FROM student";
  $result=mysql_query($sql,$connect);
     print "<table border=1>\n";
      print "<tr>\n";
     while ($field = mysql_fetch_field($result)) {
        print "<th>$field->name</th>\n";
      }
      print "</tr>\n\n";
     while($row = mysql_fetch_assoc($result)) {
        print "<tr>\n";
         foreach ($row as $col=>$val) {
            print "<td>$val</td>\n";
          }
        print "</tr>\n\n";
     }
    print "</table>\n";
?>

Check whether there is such a record in the table or not, you can do this:

$sql="SELECT name,data_rojdeniya FROM student WHERE name='$name' AND data_rojdeniya='$data';";
  $result=mysql_query($sql,$connect);
  $num_rows = mysql_num_rows($result);
     if($num_rows>0)
     {
      echo " ";//   
     }
     else
     {
      //
      }

Answer 2

6.4.8 REPLACE syntax

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
VALUES (expression,...),(...),...
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
SELECT ...
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
SET col_name=expression, col_name=expression,...

The REPLACE statement works exactly like INSERT, except that if the old record in the given table has the same UNIQUE or PRIMARY KEY index value as the new one, then the old record will be deleted before the new one is inserted. See section 6.4.3 Syntax of the INSERT statement.

In other words, the REPLACE command does not provide access to the replacement entry. In some older versions of MySQL, such access was sometimes possible, but it was a defect that has already been fixed.

When using the REPLACE command, the mysql_affected_rows() function will return a value of 2 if the old row has been replaced by the new one. This is explained by the fact that first one row is inserted into the table, and then the second row is deleted – a duplicate.

This makes it easy to determine whether the REPLACE command has done a job of adding or replacing a line. It is enough just to check what number the function mysql_affected_rows() returned – 1 (row added) or 2 (replaced).

Note that if UNIQUE or PRIMARY KEY indexes are not used, then REPLACE does not make sense, as it works just like INSERT.

Source.

Add will remove the duplicate in one request.