input_berita.php

$id = isset($_POST['id']) ? $_POST['id'] : '';  
$confirmation = isset($_POST['confirmation']) ? $_POST['confirmation'] : '';  
$kategori = isset($_POST['kategori']) ? $_POST['kategori'] : ''; 
$isiBerita = isset($_POST['isiBerita']) ? $_POST['isiBerita'] : '';
$judul = isset($_POST['judul']) ? $_POST['judul'] : ''; 
	
	
	//Load berita
	if (!empty($_REQUEST['id'])){
		$result = mysql_query("SELECT * FROM berita WHERE id =".$_REQUEST['id']) or die(mysql_error());
		$data = mysql_fetch_array($result);
		$id = $data['id'];
		$judul = $data['judul'];
		$isiBerita = $data['isi_berita'];
	}
	
	//Simpan berita dan konfirmasi kepada subscriber
	if (isset($_REQUEST['ok'])){
		$judul = $_REQUEST['judul'];
		$news = $_REQUEST['news'];
		$idKategori = $_REQUEST['kategori'];
		$tanggal = date('Y-m-d');
		if (empty($_REQUEST['id']))
			$sqlstr = "INSERT INTO berita(id_kategori, judul, isi_berita, tanggal)VALUES(".$idKategori.",'".$judul."','".$news."','".$tanggal."')";
		else
			$sqlstr = "UPDATE berita SET judul='".$judul."', isi_berita='".$news."' WHERE id=".$_REQUEST['id'];
		$result = mysql_query($sqlstr) or die(mysql_error());
		
		//Jika mode edit, maka tidak akan dikirimkan konfirmasi kepada subscriber
if (empty($_REQUEST['id']))	kirimEmail($idKategori, $judul, $news);
$confirmation = ($result) ? "Data telah tersimpan." : "Gagal menyimpan data.";	
	}
	?>

<div align="center">
		<div style="width:800px;text-align:left;">
		<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
		<link href="ckeditor/content.css" rel="stylesheet" type="text/css"/>
		<?php echo $confirmation;?>
		<form method="post" action="<?php $_SERVER['PHP_SELF']?>">
			<input type="hidden" name="id" value="<?php echo $id; ?>"/>
			<input type="hidden" name="kategori" value="<?php echo $_REQUEST['kategori']; ?>"/>
			<table>
				<tr>
					<td>Judul</td>				
					<td><input size="50px" type="text" name="judul" value="<?php echo $judul; ?>"/></td>
				</tr>
				<tr>
					<td valign="top">Isi berita</td>				
					<td>
						<textarea cols="60" rows="10" id="news" name="news"><?php echo $isiBerita;?></textarea>
						<script type="text/javascript">
							var editor = CKEDITOR.replace('news');
						</script>					</td>
				</tr>
				<tr>				
					<td><input type="submit" name="ok" value="Simpan"/></td>
			  </tr>
			</table>
		</form>
		</div>
	</div>

I just filled in the following form:

Judul [_________________________]

Isi berita [ ]
[ ]
[ ]
[ ]
[___________________________]


[ Simpan ]


If I filled in those form and then press "Simpan" (translate: Save)

The following error appears:

Connected successfully
Duplicate entry '0' for key 'PRIMARY'

Why duplicates, If I filled in a new data why id does not autogenerate a different number?

Thanks.

Probably because you id column in your table is not set to auto increment.

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.