// connect to database
	  
	  $con = mysql_connect('localhost', 'root', '');
	  
	  if (!$con) {
    	die('Could not connect: ' . mysql_error());
		}
		echo 'Connected successfully';

	  
	   $db_selected = mysql_select_db("template", $con);

	   if (!$db_selected)
  		{
  		die ("Can't use template : " . mysql_error());
  		}

	  	  
	  
	  // cek login
session_start();	  
	  //undefined index: login
if (isset($_SESSION['login'])){
		echo "Anda tidak berhak mengakses halaman ini.";
		exit();
	}

	//SIMPAN DATA
	if (isset($_REQUEST['simpan'])){
		$kategori = mysql_real_escape_string($_REQUEST['kategori']);
		$id = $_REQUEST['id'];
		
		if (empty($id))
			$sqlstr = "INSERT INTO kategori_berita VALUES('".$kategori."')"  or die(mysql_error());
		else
			$sqlstr = "UPDATE kategori_berita SET kategori = '".$kategori."' WHERE id =".$id;

These are the errors:

Connected successfully
Notice: Undefined index: kategori in C:\xampp\htdocs\php_template2\category_manager.php on line 97
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=
Notice: Undefined variable: id in C:\xampp\htdocs\php_templa' at line 1


Line 97: $kategori = mysql_real_escape_string($_REQUEST);

What's wrong with these errors: I am trying to save information and would like to see it in table, yet the error appears and the saved information does not appears on the table.

Dani AI

Generated

Brief diagnosis and what to fix (short)

Several separate problems are visible in the thread. The useful points from and are correct: check the form field names and use isset() before reading them. Other replies suggesting raw text like vaules(...) are incorrect and will produce syntax errors. The practical causes here are: passing an array into the escaping function, building SQL without executing it, constructing a WHERE clause when id is empty (produces a syntax error), and echoing an uninitialised variable into the form. Also the session check logic appears reversed, so authenticated users may be blocked incorrectly.

How to fix it (concise example and rules)

  • Check the form method and input names; use $_POST['kategori'] (or filter_input) and always test isset() before using the value.
  • Initialise variables used in the HTML value attribute to avoid "undefined variable" notices; escape when printing (htmlspecialchars).
  • Always execute the SQL you build and prefer prepared statements to avoid injection. Do not rely on mysql_* functions (they are deprecated); use mysqli or PDO.

Minimal mysqli example (replace your current mysql_* flow)

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost','user','pass','template');

session_start();
if (! isset($_SESSION['login'])) { exit('Not authorized'); }

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['simpan'])) {
  $kategori = trim($_POST['kategori'] ?? '');
  $id = isset($_POST['id']) && $_POST['id'] !== '' ? (int) $_POST['id'] : null;

  if ($kategori === '') { /* handle validation error */ }

  if ($id) {
    $stmt = $mysqli->prepare("UPDATE kategori_berita SET kategori = ? WHERE id = ?");
    $stmt->bind_param('si', $kategori, $id);
  } else {
    $stmt = $mysqli->prepare("INSERT INTO kategori_berita (kategori) VALUES (?)");
    $stmt->bind_param('s', $kategori);
  }
  $stmt->execute();
}

Quick debugging checklist

  • Use var_dump($_POST) to verify the submitted fields and names.
  • If you still see SQL syntax errors, echo the final SQL (or use a prepared-statement log) and try it directly in the database client.
  • Use htmlspecialchars() when printing form values.
  • Migrate from mysql_* to mysqli or PDO to be compatible with current PHP and to use prepared statements.

Recommended Answers

All 5 Replies

There is no need to mention the values like ".$kategori." You just declare it as
mysql_query(insert into kategori_berita vaules('$$kategori'); if it is a string if not so just remove quotes.
And you didn't used query so the server will not understand which query should be executed please try the above syntax.

I suppose you are misspelled the form fields "kategori","id".
Check out your previous page for correct spelling.

It is clear , if you post the previous page code.

I wonder why in my category box is written:

<br /><b>Notice</b>: Undefined variable: kategori in <b>C:\xampp\htdocs\php_template2\category_manager.php</b> on line <b>122</b><br />
_______________
Kategory [_______________]


line 122: <input name="kategori" type="text" id="kategori" value="<?php echo $kategori;?>"/>

This has been described in your other post. $_REQUEST['kategori'] may not have a value, use isset() . Also, $_REQUEST is deprecated, use $_GET

try using


mysql_query(insert into kategori_berita vaules('$$kategori');

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.