Jump to content

Query function not working


pcristian43

Recommended Posts

Hello I have a general php insert function that I want to use to insert data into tables regulary, the problem is, each time I use it it creates a new row in the table instead of using certain columns to add information to, here is the function, can you tell me why does it insert a new row each time I use it?

class Query{
function add($table,$cols,$vals){
		include 'conx.php';
		$query = "
			INSERT INTO $table($cols) VALUES('$vals');
		";
		
		$res = mysqli_query($con,$query) ;
		
		if($res){
			echo "Inserted value";
		}else{
			echo "Did not insert value";
		}
	}
}
Link to comment
Share on other sites

I also have other general use functions that don't seem to work properly, here are those as well:

class Query{
	function table($name,$cols){
		include 'conx.php';
		$query = "
			CREATE TABLE $name(
				id int AUTO_INCREMENT PRIMARY KEY NOT NULL,
				".$cols.",
				error varchar(255) NOT NULL,
				date date NOT NULL
			);
		";
		$res = mysqli_query($con,$query) or die(mysqli_error($con));
		
		if($res){
			echo "Table created";
		}else{
			echo "Could not run table";
		}
	}
	function select($table,$col,$val){
		include 'conx.php';
		$query = "
			SELECT $col FROM $table WHERE $col='$val';
		";
		$res = mysqli_query($con,$query);
		
		if($res){
			echo "Selected data";
		}else{
			echo "Could not select data";
		}
	}
	function update($table,$col,$val,$id){
		include 'conx.php';
		$query = "
			UPDATE $table
			SET 
			$col='$val'
			WHERE
			id='$id';
			)
		";
		$res = mysqli_query($con,$query);
		
		if($res){
			echo "Updated value";
		}else{
			echo "Did not update value";
		}
	}
}
Link to comment
Share on other sites

I also have other general use functions that don't seem to work properly...

 

It might help if you let us know what you mean by they don't work properly. Are you getting errors, for example? If so, what are they? What is the unexpected behavior?

 

Based on the code provided, I don't see where $con is defined. You'll need to pass the database connection object to your class and its methods.

Link to comment
Share on other sites

if your conx.php code is creating a new database connection, this is not the way to get a connection into your methods/class, because it is creating a new connection each time you call a class method and the connection is destroyed when the method ends/returns.

 

as cyberRobot mentioned, your main code should create one instance of your database connection and pass it into the instance of your class, usually in the class constructor.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.