INSERT SQL query does not work, inserting values ​​into my database

I am trying to insert some values ​​into my DB, but it is not working, I am trying to understand why it is not working, but I am an amateur php encoder,

This is the code I'm using:

$insert = mysql_query
    ("
    INSERT INTO news(id,title,body,date,by)
    VALUES ('NULL','".$title."','".$body."','".$date."','".$by."')
    ");
    mysql_close($connect);

And the lines I'm trying to insert are: id, title, body, date, by
but it does not appear in the database or on my news page.

Can anyone help me out?

+3
source share
5 answers

by- special keyword. Try swapping column names in labels:

INSERT INTO news(`id`,`title`,`body`,`date`,`by`)
+4
source

I expect id to be your primary key. It must not allow a null value. If it automatically increases, you can try the following:

$insert = mysql_query
    ("
    INSERT INTO news(title,body,date, by)
    VALUES ('".$title."','".$body."','".$date."','".$by."')
    ");

, . . by author.

, . . SQL- PHP?.

+2

, NULL, .

, , mysql_real_escape_string $title, $body ..

$title = mysql_real_escape_string($title);
$body  = mysql_real_escape_string($body);
$date  = mysql_real_escape_string($date);
$by    = mysql_real_escape_string($by);

mellamokb, by date . ( ). ( )

$query = "INSERT INTO `news` (`id`, `title`, `body`, `date`, `by`) VALUES
      (NULL, '" . $title . "', '" . $body . "', '" . $date . "', '" . $by . "');";
if ($insert = mysql_query($query, $connect)) {
    // Success!
} else {
    echo 'MySQL Error: ' . mysql_error($connect); // this will tell you whats wrong
}
mysql_close($connect);
+1

sprintf mysql_real_escape_string SQL-:

$insert = sprintf("INSERT INTO news
                    (id, title, body, date, `by`)
                   VALUES 
                     (NULL,'%s','%s','%s','%s')",
                   mysql_real_escape_string($title),
                   mysql_real_escape_string($body),
                   mysql_real_escape_string($date),
                   mysql_real_escape_string($by));

$result = mysql_query($insert) or die(mysql_error());

, .

, :

  • , , MySQL (. ). , - / ,
  • NULL should not be inside single quotes or will be interpreted as a string; DEFAULTwill be another option if there is a DEFAULT constraint in the column.
  • the column datemust be a DATE, DATETIME, or TIMESTAMP data type, not a string, to use the MySQL Date / Time function , and must use DATE_FORMAT if this value is not the standard MySQL date format.
+1
source

Source: https://habr.com/ru/post/1795815/


All Articles