this time i try to check before insert the data
but i failed yo do that
this is my code:
<?php
session_start(); ?>
<?php
require_once('Connections/repi.php');
// *** Redirect if username exists
$MM_flag="Hantar";
if (isset($_POST[$MM_flag])) {
$MM_dupKeyRedirect="data_ada_kelas_subjek.php";
$loginUsername = $_POST['kelas'];
if(is_array($_POST['kod_subject'])){
foreach( $_POST['kod_subject'] as $loginUsername2 ){
$LoginRS__query = "SELECT KELAS,KOD_SUBJ FROM kelas_subjek WHERE KELAS='" . $loginUsername . "' AND KOD_SUBJ='" . $loginUsername2 . "' ";
mysql_select_db($database_repi, $repi);
$LoginRS=mysql_query($LoginRS__query, $repi) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
}
}
//if there is a row in the database, the username was found - can not add the requested username
if($loginFoundUser){
$MM_qsChar = "?";
//append the username to the redirect page
if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
$MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
header ("Location: $MM_dupKeyRedirect");
exit;
}
}
include 'db_connect.php';
// build the form action
if($_POST['Hantar'] == "Hantar"){
$kelas = $_POST['kelas'];
if(is_array($_POST['kod_subject'])){
foreach( $_POST['kod_subject'] as $kod ){
$sql = mysql_query("INSERT INTO kelas_subjek (kelas, kod_subj)
VALUES ('$kelas', '$kod')");
}
}
if ($sql) {
include 'berjaya_daftar_kelas_subjek.php';
exit();
} else {
echo mysql_error();
}
}
?>
if all of the value have been in the database,it will go to the link that tells data have been inserted..
but if user inserted 4 value which 3 value have in the database and 1 value not in database.it will insert all the 4 value in the database.. 🙂