Help needed. Errors in php codes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nielsenN
Forum Newbie
Posts: 17
Joined: Wed Nov 16, 2011 6:46 pm

Help needed. Errors in php codes

Post by nielsenN »

Im new in php and now Im developing a simple webpage to insert information. I keep on getting the error "Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\projectli\mainadd1.php on line 29" and "Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, object given in C:\xampp\htdocs\projectli\mainadd1.php on line 31 ". This happens when i tried to insert a data in the database. The data is inserted without problems but I'm trying to do some error handling since I don't want any duplicate data in my database. I hope I can get help here since Ive already searching for solutions elsewhere with no success. :(
Below are the code for the page.

Code: Select all

<?php
if(isset($_POST['submitted'])) {
include('connect.php');

	$id=$_POST['id'];
	$negeri=$_POST['negeri'];
	$daerah=$_POST['daerah'];
	$pkg=$_POST['pkg'];
	$kod_sekolah=$_POST['kod_sekolah'];
	$nama_sekolah=$_POST['nama_sekolah'];
	$ptj=$_POST['ptj'];
	$server=$_POST['server'];
	$pc=$_POST['pc'];
	$nb=$_POST['nb'];
	$mono_laser=$_POST['mono_laser'];
	$color_laser=$_POST['color_laser'];
	$dot_matrix=$_POST['dot_matrix'];
	$lcd=$_POST['lcd'];
	$set_lan=$_POST['set_lan'];
	$jumlah_kos=$_POST['jumlah_kos'];
	$dibayar=$_POST['dibayar'];
	$tanggungan=$_POST['tanggungan'];
	
if($negeri){
if($daerah){	
if($pkg){
if($kod_sekolah){
									
$q1=mysqli_query("SELECT * FROM schools where kod_sekolah = '".$_POST['kod_sekolah']."'");
											
$q2=mysqli_fetch_object($dbcon,$q1);
																						
if($q2->kod_sekolah == $_POST['kod_sekolah']){
											
die('<br><br>There is already "'.$q2->kod_sekolah.'" in the database, please choose another Kod Sekolah.');
												
}
																		
if($nama_sekolah){
																				
$sqlinsert="INSERT INTO schools (id, negeri, daerah, pkg, kod_sekolah, nama_sekolah, ptj, server, pc, nb, mono_laser, 	
								color_laser, dot_matrix, lcd, set_lan, jumlah_kos, dibayar, tanggungan) 
							VALUES ('$id','$negeri', '$daerah', '$pkg', '$kod_sekolah', '$nama_sekolah', '$ptj', '$server', '$pc', '$nb', 	
												'$mono_laser', 		
							'$color_laser','$dot_matrix', '$lcd', '$set_lan', '$jumlah_kos', '$dibayar', '$tanggungan')"; 
							
if(!mysqli_query($dbcon, $sqlinsert)){
die('error inserting new record');
				}
$errormsg = "1 Record Has Been Inserted";
							}
else
$errormsg="Please Complete Nama Sekolah Field";
							}	
else
$errormsg="Please Complete Kod Sekolah Field";
								}	
else
$errormsg="Please Complete PKG Field";
							}	
							
else
$errormsg="Please Complete Daerah Field";
						}	
else	
$errormsg="Please Complete Negeri Field";
				}
							
							
?>


If it helps this are the code for connect.php and the database

Code: Select all


<?php

$dbcon=mysqli_connect("localhost","root","", "project");


?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help needed. Errors in php codes

Post by Celauran »

mysqli_query() does indeed require two parameters.

Code: Select all

$q1 = mysqli_query($dbcon, "SELECT * FROM schools where kod_sekolah = '".$_POST['kod_sekolah']."'");
Also, always validate and sanitize user input before using it in a query. You're just asking for trouble otherwise.
nielsenN
Forum Newbie
Posts: 17
Joined: Wed Nov 16, 2011 6:46 pm

Re: Help needed. Errors in php codes

Post by nielsenN »

After doing the changes in the codes

Code: Select all

$q1 = mysqli_query($dbcon, "SELECT * FROM schools where kod_sekolah = '".$_POST['kod_sekolah']."'");
Now I'm getting this error :(

Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, object given in C:\xampp\htdocs\projectli\mainadd1.php on line 31
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help needed. Errors in php codes

Post by Celauran »

So var_dump($q1) and see if that offers any insight.
nielsenN
Forum Newbie
Posts: 17
Joined: Wed Nov 16, 2011 6:46 pm

Re: Help needed. Errors in php codes

Post by nielsenN »

Thanks! Problems solved!:)
Post Reply