Inserting Data INTO Mysql ERROR Why?

Well Hello guys
i was working in my website and made code i really dont know whats wrong it … it just doesn’t work

<?php
if (isset($_POST[‘submit’])) {

$con = mysql_connect(“localhost”,“root”,"");
if (!$con) {
die("Can not connect: " .mysql_error());
}

mysql_select_db(“topic”,$con);
$sql = “INSERT INTO news (title,text,link) VALUES (’$POST[title]’,’$POST[text]’,’$POST[link]’)”;
mysql_query($sql,$con);
mysql_close($con);

}
?>

im getting thes Errors:

Notice: Undefined variable: POST in C:\xampp\htdocs\admin.php on line 10

Notice: Undefined variable: POST in C:\xampp\htdocs\admin.php on line 10

Notice: Undefined variable: POST in C:\xampp\htdocs\admin.php on line 10

Line 10: $sql = “INSERT INTO news (title,text,link) VALUES (’$POST[title]’,’$POST[text]’,’$POST[link]’)”;

Ehh i fixed it no need help anymore :slight_smile:

$title = $_POST[“title”];
$text = $_POST[“text”];
$link = $_POST[“link”];
$sql = “INSERT INTO news (title,text,link) VALUES (’$title’,’$text’,’$link’)”;

You want to use prepared statements. And update to use at a minimum mysqli_ database functions.

To put it into perspective, try adding this to your database through the form:

title: We aren’t just news anymore!
text: Amazing News Story
link: news.com/12/152

And see what happens.

Your code is obsolete and completely removed from the current version of Php and is vulnerable to an SQL Injection Attack. I demand you get rid of it right now!

You need to use PDO. Here is a tutorial to get you going https://phpdelusions.net/pdo

You can also download my PDO bumpstart Database from my signature.

Sponsor our Newsletter | Privacy Policy | Terms of Service