pcristian43 Posted May 29, 2014 Share Posted May 29, 2014 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"; } } } Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 29, 2014 Share Posted May 29, 2014 So are you looking to update an existing entry? If so, try using update instead of insert: http://dev.mysql.com/doc/refman/5.0/en/update.html Quote Link to comment Share on other sites More sharing options...
pcristian43 Posted May 29, 2014 Author Share Posted May 29, 2014 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"; } } } Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 29, 2014 Share Posted May 29, 2014 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. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 30, 2014 Share Posted May 30, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.