April 2, 201115 yr i have 2 mysql queries posting into the same database but different tables, the only problem is is that in the $_SESSION[category] table the adno is not changing but in the table below it pretty much the same called ads is updating the adno, i dont know why its not changing on the other table, everything else is inserting as it should, im not getting any mysql errors either the structure of the adno row is the same in both ads table and $_SESSION[category] table, mysql_query("INSERT INTO $_SESSION[category] (ID, title, firstline, ref1, price1, secondline, ref2, price2, thirdline, adno) VALUES ('', '$_POST[$newtitle]', '$_POST[$newfirstline]', '$_POST[$newref1]', '$_POST[$newprice1]', '$_POST[$newsecondline]', '$_POST[$newref2]', '$_POST[$newprice2]', '$_POST[$newthirdline]', '$_POST[$adno]')")or die(mysql_error()); mysql_query("INSERT INTO ads (id, adno, page) VALUES ('', '$_POST[$adno]', '$_SESSION[category]')")or die(mysql_error()); just wondering if anyone could help me out, Thanks Edited April 2, 201115 yr by web-itec
April 2, 201115 yr Author oops, my bad, i had addressed the adno incorrectly and the only reason the other table was updating in the adno column was because it was updating on the upload page it was redirected to lol
April 14, 201115 yr Just an observation... If this is going to be used or accessible by more people than yourself, you may want to look at adding some SQL injection protection too rather than simply taking user-posted variables and inserting directly into the database... Pass each variable through an escaping mechanism first.
April 15, 201115 yr Author Just an observation... If this is going to be used or accessible by more people than yourself, you may want to look at adding some SQL injection protection too rather than simply taking user-posted variables and inserting directly into the database... Pass each variable through an escaping mechanism first. just out of curiousity of your comment, is it possible for someone to somehow alter the drop down list to sql inject as the category variable is created by a dropdown list but every other input in there is already got preg replace on it to get rid of everything but letters, numbers and fullstop Thanks, Gary
April 15, 201115 yr just out of curiousity of your comment, is it possible for someone to somehow alter the drop down list to sql inject as the category variable is created by a dropdown list but every other input in there is already got preg replace on it to get rid of everything but letters, numbers and fullstop Thanks, Gary Yes they can, they could create there own form that points to ur processing file, and you should be using more then just preg_replace i'd use a simple function like this function clean($string){ return mysqli_real_escape_string($mysqli,trim(strip_tags($string))); }
April 15, 201115 yr Author Yes they can, they could create there own form that points to ur processing file, and you should be using more then just preg_replace i'd use a simple function like this function clean($string){ return mysqli_real_escape_string($mysqli,trim(strip_tags($string))); } what is the difference between mysql_real_escape_string() and mysqli_real_escape_string() Thanks, Gary
April 15, 201115 yr what is the difference between mysql_real_escape_string() and mysqli_real_escape_string() Thanks, Gary mysqli_real_escape_string is for PHP 5 only when ur using the mysqli set of functions which most people do now since everyones upgrading to PHP 5 although mysql_real_escape_string will work in PHP 5 its better to just use mysqli since its better improved ect..
April 15, 201115 yr Author mysqli_real_escape_string is for PHP 5 only when ur using the mysqli set of functions which most people do now since everyones upgrading to PHP 5 although mysql_real_escape_string will work in PHP 5 its better to just use mysqli since its better improved ect.. can i change my functions to the improved mysql by just putting an i on the end of where it says mysql? for example $result = mysql_query($sqlquery); $number = mysql_num_rows($result); mysql_query("INSERT INTO table (ID, number) VALUES ('', '$adno')"); would be the old mysql $result = mysqli_query($sqlquery); $number = mysqli_num_rows($result); mysqli_query("INSERT INTO table (ID, number) VALUES ('', '$adno')"); would be the improved mysql? Thanks, Gary
April 15, 201115 yr can i change my functions to the improved mysql by just putting an i on the end of where it says mysql? for example $result = mysql_query($sqlquery); $number = mysql_num_rows($result); mysql_query("INSERT INTO table (ID, number) VALUES ('', '$adno')"); would be the old mysql $result = mysqli_query($sqlquery); $number = mysqli_num_rows($result); mysqli_query("INSERT INTO table (ID, number) VALUES ('', '$adno')"); would be the improved mysql? Thanks, Gary Yes but it also requires the connection link or it will error out, so should look like this mysqli_query($connectlink,"INSERT INTO table (ID, number) VALUES ('', '$adno')");
April 16, 201115 yr Author Yes but it also requires the connection link or it will error out, so should look like this mysqli_query($connectlink,"INSERT INTO table (ID, number) VALUES ('', '$adno')"); and the connectionlink is just your mysql connection i suppose? and do i only need to put the connection link onto the INSERT mysqli line or do i have to do it on the other lines also like $result = mysqli_query($connectlink,$sqlquery); $number = mysqli_num_rows($connectlink,$result); i didnt even know mysqli existed till you said so im totally new to this haha i appreciate your help though so +1 mate and its like i script in 20th century still aswel or something lol as i use last php version not new one, didnt know about mysqli, i script AS2 not AS3, i use table instead of divs lol i need to get my head in a book again lol Gary
April 16, 201115 yr and the connectionlink is just your mysql connection i suppose? and do i only need to put the connection link onto the INSERT mysqli line or do i have to do it on the other lines also like $result = mysqli_query($connectlink,$sqlquery); $number = mysqli_num_rows($connectlink,$result); i didnt even know mysqli existed till you said so im totally new to this haha i appreciate your help though so +1 mate and its like i script in 20th century still aswel or something lol as i use last php version not new one, didnt know about mysqli, i script AS2 not AS3, i use table instead of divs lol i need to get my head in a book again lol Gary basically anywhere u use mysqli_query i'd really read up on it within the php manual, also to avoid having to include a connection each and every place u could just make a mysql class that looks alittle like this, this class i use on my blog but can be addapted for any site <?php if(!defined('site')){die('No direct access is allowed');} ?> <?php /* @Blogging System Built By:Steve Morgan @Current Page:class.database.php @File Version:1.0.0 @Comment:Handles interaction with mysql, @Copyright:2011 */ class db{ /**Main database instance**/ private static $instance = ""; /**Set default connection as false**/ public $connect = false; /**Main construct, make access private so it can not be called directly**/ private function __construct(){ global $host,$user,$pass,$db; $this->connect = @mysqli_connect($host,$user,$pass,$db) ; if(!$this->connect){ /**Error out failed connection**/ new_bl_error('Failed-Connect'); } } /**Main db instance**/ public static function db_instance(){ if(!self::$instance){ $class = __CLASS__; self::$instance = new $class; } return self::$instance; } /**Perform query**/ public function bl_query($return){ $return = @mysqli_query($this->connect,$return) or die(mysqli_error($this->connect)); if(!$return){ /**Error out failed query**/ new_bl_error('Failed-Query'); } return $return; } /**Count the amount of rows**/ public function bl_rows($return){ return mysqli_num_rows($return); } /**Pull data as associative array**/ public function bl_assoc($return){ return mysqli_fetch_assoc($return); } /**Pull data as non associative array**/ public function bl_array($return){ return mysqli_fetch_array($return); } /**Get id of last inserted record**/ public function bl_id($return){ return mysqli_insert_id($return); } /**Escape data**/ public function bl_escape($return){ return mysqli_escape_string($this->connect,$return); } }
April 16, 201115 yr Author basically anywhere u use mysqli_query i'd really read up on it within the php manual, also to avoid having to include a connection each and every place u could just make a mysql class that looks alittle like this, this class i use on my blog but can be addapted for any site <?php if(!defined('site')){die('No direct access is allowed');} ?> <?php /* @Blogging System Built By:Steve Morgan @Current Page:class.database.php @File Version:1.0.0 @Comment:Handles interaction with mysql, @Copyright:2011 */ class db{ /**Main database instance**/ private static $instance = ""; /**Set default connection as false**/ public $connect = false; /**Main construct, make access private so it can not be called directly**/ private function __construct(){ global $host,$user,$pass,$db; $this->connect = @mysqli_connect($host,$user,$pass,$db) ; if(!$this->connect){ /**Error out failed connection**/ new_bl_error('Failed-Connect'); } } /**Main db instance**/ public static function db_instance(){ if(!self::$instance){ $class = __CLASS__; self::$instance = new $class; } return self::$instance; } /**Perform query**/ public function bl_query($return){ $return = @mysqli_query($this->connect,$return) or die(mysqli_error($this->connect)); if(!$return){ /**Error out failed query**/ new_bl_error('Failed-Query'); } return $return; } /**Count the amount of rows**/ public function bl_rows($return){ return mysqli_num_rows($return); } /**Pull data as associative array**/ public function bl_assoc($return){ return mysqli_fetch_assoc($return); } /**Pull data as non associative array**/ public function bl_array($return){ return mysqli_fetch_array($return); } /**Get id of last inserted record**/ public function bl_id($return){ return mysqli_insert_id($return); } /**Escape data**/ public function bl_escape($return){ return mysqli_escape_string($this->connect,$return); } } Okay mate thanks for the advice
April 18, 201115 yr just out of curiousity of your comment, is it possible for someone to somehow alter the drop down list to sql inject as the category variable is created by a dropdown list but every other input in there is already got preg replace on it to get rid of everything but letters, numbers and fullstop Thanks, Gary Sorry I didn't get to reply earlier - but I see you got a good set of replies so looks like you're on track
April 18, 201115 yr You'll want to validate the session information, as execiting a query based on a session variable isn't good practice. Fopr instance, check that the session data is the correct type, sanitise it and check that category exists in your database. Very good tips
Create an account or sign in to comment