Click here to Skip to main content
15,445,066 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
5 days ago, I have no problem with inserting data into database, but now I find it some error. It function but no data inserted into the table. Is it any problem with my code?

This is the connection file

connect_error)
	{
		die("Connection Error");
	}
	else{
		echo "successful";
	}
?>

Following is the insertion file

<br>" ;
		$kunci = $_POST['kunci']."".$_POST['blok']."-".$_POST['house']."-".$_POST['room'].$_POST['number'];
		$mbbrstatus = $_POST['statusmbbr'];
		$mbbrcatatan = $_POST['mcatatan'];
		$krstatus = $_POST['statuskr'];
		$krcatatan = $_POST['krcatatan'];
		$apstatus = $_POST['statusap'];
		$apcatatan = $_POST['apcatatan'];
		$katilstatus = $_POST['statusk'];
		$katilcatatan = $_POST['kcatatan'];
		$tilamstatus = $_POST['tstatus'];
		$tilamcatatan = $_POST['tcatatan'];
	
		
		$sql = "INSERT INTO `akuan`(`lokasi`, `mbbr_status`, `mbbr_catatan`,`kerusi_status`,`kerusi_catatan`,`almari_status`,`almari_catatan`,`katil_status`,`katil_catatan`,`tilam_status`,`tilam_catatan`,`jenis_akuan`)
		VALUES ('$kunci', '$mbbrstatus', '$mbbrcatatan','$krstatus','$krcatatan','$apstatus','$apcatatan','$katilstatus','$katilcatatan','$tilamstatus','$tilamcatatan','penerimaan')";
		//echo $sql;
		if ($con->query($sql)===TRUE){
			//echo "Added to database";
			echo"alert('Submitted successfully.')";
			header("location: pelajar.php");
		}
		else{
			echo "Something went wrong.";
		}
	}
	else{
		echo "no submit";
		//redirect to homepage
	}
?>

Below is the form

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>PENGURUSAN ASET SYSTEM KOLEJ KEDIAMAN PAGOH</title>
		<link rel = "stylesheet" href="css/style.css">
		<link href='https://unpkg.com/boxicons@2.1.2/css/boxicons.min.css' rel='stylesheet'>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
	</head>
	<body>
		<div class="banner-area">
			<h2>SISTEM PENGURUSAN ASET KOLEJ KEDIAMAN PAGOH</h2>
		</div>
		<div class="sidebar">
			<div class="logo_content">
				<div class="logo">PASKKP</div>
				
			</div>
			<ul class="nav_list">
				<li>
					<a href="">
						
						Profil
					</a>
				</li>
				<li>
					<a href="penerimaan.html" id="hello">
						Penerimaan aset
					</a>
				</li>
				<li>
					<a href="pemulangan.html">
						Pemulangan aset
					</a>
				</li>
				<li>
					<a href="">
						Keluar
					</a>
				</li>
			</ul>
	</div>

	
	<div class="content">
		<div class = "wrapper">
			<form action ="submit.php" method = "post">
			<h2>BORANG AKUAN PENERIMAAN ASET <br>DI BILIK PELAJAR <br>KOLEJ KEDIAMAN PAGOH,UTHM</h2>
			<p>PERINGATAN/ TINDAKAN: <br><br>
			1.SUSUNAN ASET ADALAH BERPANDUKAN PLAN SUSUN ATUR PERABOT YANG TELAH DISEDIAKAN.
			<br>2. PASTIKAN SEMUA ASET DALAM KEADAAN BAIK. 
			<br>3. LAPORKAN SETIAP KEROSAKAN ASET KE PEJABAT KOLEJ MASING-MASING.  
			<br>4. TIDAK MEROSAKKAN ATAU MELAKUKAN VANDALISMA TERHADAP ASET. 
			<br>5. SILA PASTIKAN ASET YANG DITERIMA ADALAH SAMA DENGAN NOMBOR KUNCI YANG ANDA TERIMA.
		</p>
		<h3>(A) MAKLUMAT PELAJAR</h3>
		<p>NAMA: 
			<br>NO. MATRIK: 
			<br>SEM/ SESI: 
			<br>NO. TEL: 
			<br>NO. KUNCI: 
			  <select name="kunci" id="kunci"required>
			  	<option value="" selected="selected" hidden="hidden">--CHOOSE KKP--</option>
    			<option value="KKP1">KKP1</option>
    			<option value="KKP2">KKP2</option>
   				<option value="KKP3">KKP3</option>
  			  </select>

  			 <select name="blok" id="select1" onchange="getSelectValue(this.value)"required>
  			 	<option value="" selected="selected" hidden="hidden" >--CHOOSE BLOCK--</option>
    			<option value="A1">A1</option>
    			<option value="A2">A2</option>
   				<option value="A5">A5</option>
   				<option value="A6">A6</option>
   				<option value="A10">A10</option>
   				<option value="A11">A11</option>
   				<option value="A12">A12</option>
   				<option value="A13">A13</option>
   				<option value="A14">A14</option>
   				<option value="A15">A15</option>
   				<option value="A16">A16</option>
   				<option value="A17">A17</option>
   				<option value="A18">A18</option>
  			  </select>
			-
			
			<select name="house" id="select2" required>
				<option value="" selected="selected" hidden="hidden" >--CHOOSE HOUSE--</option>
    			<option value="B101">B101</option>
    			<option value="B102">B102</option>
   				<option value="B103">B103</option>
   				<option value="B104">B104</option>
   				<option value="B105">B105</option>
   				<option value="B106">B106</option>
   				<option value="B107">B107</option>
   				<option value="B108">B108</option>
   				<option value="B201">B201</option>
    			<option value="B202">B202</option>
   				<option value="B203">B203</option>
   				<option value="B204">B204</option>
   				<option value="B205">B205</option>
   				<option value="B206">B206</option>
   				<option value="B207">B207</option>
   				<option value="B208">B208</option>
   				<option value="B301">B301</option>
    			<option value="B302">B302</option>
   				<option value="B303">B303</option>
   				<option value="B304">B304</option>
   				<option value="B305">B305</option>
   				<option value="B306">B306</option>
   				<option value="B307">B307</option>
   				<option value="B308">B308</option>
   				<option value="B401">B401</option>
    			<option value="B402">B402</option>
   				<option value="B403">B403</option>
   				<option value="B404">B404</option>
   				<option value="B405">B405</option>
   				<option value="B406">B406</option>
   				<option value="B407">B407</option>
   				<option value="B408">B408</option>
   				<option value="B501">B501</option>
    			<option value="B502">B502</option>
   				<option value="B503">B503</option>
   				<option value="B504">B504</option>
   				<option value="B505">B505</option>
   				<option value="B506">B506</option>
   				<option value="B507">B507</option>
   				<option value="B508">B508</option>
  			  </select>
			-
			<select name="room" id="select3"  required>
				<option value="" selected="selected" hidden="hidden">--CHOOSE ROOM--</option>
    			<option value="A">A</option>
    			<option value="B">B</option>
   				<option value="C">C</option>
   				<option value="D">D</option>
  			  </select>
			<select name="number" id="select4" required>
				<option value="" selected="selected" hidden="hidden">--CHOOSE OCCUPANT--</option>
    			<option value="1">1</option>
    			<option value="2">2</option>
   				<option value="3">3</option>
  			  </select>
			<!--<b>(*Contoh: KKP2A16-B103-C2)</b>-->
		</p>
		<h3>(B) MAKLUMAT ASET</h3><br>
		<table border="1">
			<tr>
				<td>MEJA BELAJAR BERSAMA RAK</td>
				<td><input type="radio" id="mbbrstatus"  name="statusmbbr" value="ada" autocomplete="off" required>ADA</td>
				<td><input type="radio" id="mbbrstatus"  name="statusmbbr" value="tiada" autocomplete="off" required>TIADA</td>
				<td>CATATAN   <input type="text" name="mcatatan" onkeyup="this.value = this.value.toLowerCase();" autocomplete= "off"value="" id="txtName"></td>
			</tr>
			<tr>
				<td>KERUSI PELAJAR</td>
				<td><input type="radio" id="statuskr" name="statuskr" value="ada" autocomplete="off" required>ADA</td>
				<td><input type="radio" id="statuskr" name="statuskr" value="tiada" autocomplete="off" required>TIADA</td>
				<td>CATATAN   <input type="text" name="krcatatan" onkeyup="this.value = this.value.toLowerCase();" autocomplete= "off" value="" id="txtName"></td>
			</tr>
			<tr>
				<td>ALMARI PAKAIAN</td>
				<td><input type="radio" id="statusap" name="statusap" value="ada" autocomplete="off" required>ADA</td>
				<td><input type="radio" id="statusap" name="statusap" value="tiada" autocomplete="off" required>TIADA</td>
				<td>CATATAN   <input type="text"  name="apcatatan" value="" onkeyup="this.value = this.value.toLowerCase();" autocomplete= "off" id="txtName"></td>
			</tr>
			<tr>
				<td>KATIL   (BUJANG/ 2 TINGKAT)</td>
				<td><input type="radio" id="statusk" name="statusk" value="ada" autocomplete="off" required>ADA</td>
				<td><input type="radio" id="statusk" name="statusk" value="tiada" autocomplete="off" required>TIADA</td>
				<td>CATATAN   <input type="text"  name="kcatatan" onkeyup="this.value = this.value.toLowerCase();" autocomplete= "off" value="" id="txtName"></td>
			</tr>
			<tr>
				<td>TILAM</td>
				<td><input type="radio" id="tstatus" name="tstatus" value="ada" autocomplete="off" required>ADA</td>
				<td><input type="radio" id="tstatus" name="tstatus" value="tiada" autocomplete="off" required>TIADA</td>
				<td>CATATAN   <input type="text"  name="tcatatan" onkeyup="this.value = this.value.toLowerCase();" value="" autocomplete= "off" id="txtName"></td>
			</tr>
		</table>
		<br><br>
		<h3>(C) AKUAN PENERIMAAN ASET</h3>
		<br><input type="checkbox" name="agree" required>&emsp;Saya dengan ini mengesahkan telah menyemak dan menerima aset sepertimana yang dinyatakan dalam bahagian (B). <br>Sepanjang tempoh pemilikan aset tersebut saya bertanggungjawab sepenuhnya untuk memastikan aset yang diberikan berada dalam keadaan baik dan sempurna.

		<br>
		<button type="submit" id="submit" name="submit" value="submit">Hantar</button><br>
		<div class="message">
			<div class="success" id="success">Your Data Succcessfully Sent!</div>
			<div class="danger" id="danger">Fields Can't Be Empty!</div>
		</div>
		</form>
		</div>
	</div>
	<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
	<script type="text/javascript">
		
		$("#submit").click(function(){
			var name = $("kunci").val();
			var block = $("select1").val();
			var house = $("select2").val();
			var room = $("select3").val();
			var occupant = $("select4").val();
			var meja = $("mbbrstatus").val();
			var kerusi = $("statuskr").val();
			var almari = $("statusap").val();
			var katil = $("statusk").val();
			var tilam = $("tstatus").val();
			if(name == '' || block == '' || house == '' || room == '' || occupant == '' || meja == '' || kerusi == '' || almari == '' || katil == '' || tilam == ''){

				alert("Plese fill in the empty fields.");
			}
			else{
				alert('Submitted the Form')";
			}
		});
	
	</script>
	</body>
	
</html>


What I have tried:

I have tried various way from youtube but the alert box still cannot function idk why.
Try with id variables when user submit empty value display alert box please fill in the empty fields and alert box ro inform user if submit the form successfully.
Posted
Updated 20-Apr-22 21:57pm
v3
Comments
Richard Deeming 21-Apr-22 3:52am     CRLF
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query. PHP: SQL Injection - Manual[^] PHP: Prepared statements and stored procedures - Manual[^]

1 solution

Quote:
PHP
echo"alert('Submitted successfully.')";
header("location: pelajar.php");
You're sending back an alert statement without an enclosing <script> tag. That will be treated as text, not script.

You're also sending a header telling the browser to ignore any content, and navigate directly to the pelajar.php page instead. So even if you included the <script> tags, the message would not be displayed.

You could try:
PHP
echo "<script>";
echo "alert('Submitted successfully.);";
echo "location.assign('pelajar.php');";
echo "</script>";

But be sure to fix the SQL Injection[^] vulnerabilities in your code first.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900