cobusbo Posted September 26, 2014 Share Posted September 26, 2014 Hi I'm trying to insert unique info retrieved to my database but seems like I'm doing something wrong with my quary my current setup is as follow mxit.php <?php $con=mysqli_connect("*****","*******","*******","******"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } mysqli_close($con); ?> <? define('TIMEZONE', 'Africa/Harare'); date_default_timezone_set(TIMEZONE); $ip = $_SERVER["REMOTE_ADDR"]; $post_time = date("U"); $mxitua = $_SERVER["HTTP_X_DEVICE_USER_AGENT"]; $mxitcont = $_SERVER["HTTP_X_MXIT_CONTACT"]; $mxituid = $_SERVER["HTTP_X_MXIT_USERID_R"]; $mxitid = $_SERVER["HTTP_X_MXIT_ID_R"]; $mxitlogin = $_SERVER["HTTP_X_MXIT_LOGIN"]; $mxitnick = $_SERVER["HTTP_X_MXIT_NICK"]; $mxitloc = $_SERVER["HTTP_X_MXIT_LOCATION"]; $mxitprof = $_SERVER["HTTP_X_MXIT_PROFILE"]; if(!isset($mxitid)) { $mxitid = "DEFAULT"; } mysqli_query($con,"INSERT INTO mxit (ip,time,user_agent,contact,userid,id,login,nick,location,profile) VALUES ($ip,$post_time,$mxitua,$mxitcont,$mxituid,$mxitid,$mxitlogin,$mxitnick,$mxitloc,$mxitprof)"); mysqli_close($con); ?> and ive included the above file on my index.php <?PHP include "mxit.php"; ?> but after I've opened up my index page I get an error Warning: mysqli_query(): Couldn't fetch mysqli in /home/vol1_1/mzzhost.com/mzzho_15247412/htdocs/try/mxit.php on line 44Warning: mysqli_close(): Couldn't fetch mysqli in /home/vol1_1/mzzhost.com/mzzho_15247412/htdocs/try/mxit.php on line 45 And another question is how can I check the field contact in my databases and if the name already exists not to add the record to my database? Since I don't want duplicate records... Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/ Share on other sites More sharing options...
cobusbo Posted September 26, 2014 Author Share Posted September 26, 2014 Hi I'm trying to insert unique info retrieved to my database but seems like I'm doing something wrong with my quary my current setup is as follow mxit.php <?php $con=mysqli_connect("*****","*******","*******","******"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } mysqli_close($con); ?> <? define('TIMEZONE', 'Africa/Harare'); date_default_timezone_set(TIMEZONE); $ip = $_SERVER["REMOTE_ADDR"]; $post_time = date("U"); $mxitua = $_SERVER["HTTP_X_DEVICE_USER_AGENT"]; $mxitcont = $_SERVER["HTTP_X_MXIT_CONTACT"]; $mxituid = $_SERVER["HTTP_X_MXIT_USERID_R"]; $mxitid = $_SERVER["HTTP_X_MXIT_ID_R"]; $mxitlogin = $_SERVER["HTTP_X_MXIT_LOGIN"]; $mxitnick = $_SERVER["HTTP_X_MXIT_NICK"]; $mxitloc = $_SERVER["HTTP_X_MXIT_LOCATION"]; $mxitprof = $_SERVER["HTTP_X_MXIT_PROFILE"]; if(!isset($mxitid)) { $mxitid = "DEFAULT"; } mysqli_query($con,"INSERT INTO mxit (ip,time,user_agent,contact,userid,id,login,nick,location,profile) VALUES ($ip,$post_time,$mxitua,$mxitcont,$mxituid,$mxitid,$mxitlogin,$mxitnick,$mxitloc,$mxitprof)"); mysqli_close($con); ?> and ive included the above file on my index.php <?PHP include "mxit.php"; ?> but after I've opened up my index page I get an error And another question is how can I check the field contact in my databases and if the name already exists not to add the record to my database? Since I don't want duplicate records... ok so I removed the mysqli_close($con); at the top connection, but still no records has been added to my database Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/#findComment-1492148 Share on other sites More sharing options...
jazzman1 Posted September 26, 2014 Share Posted September 26, 2014 most likely the column names are spelt incorrectly in the query or incorrect data type you're inserting to db. Have you tried to debug it using a mysqli_error() function. Something like $query = mysqli_query($con,"INSERT INTO mxit (ip,time,user_agent,contact,userid,id,login,nick,location,profile) VALUES ($ip,$post_time,$mxitua,$mxitcont,$mxituid,$mxitid,$mxitlogin,$mxitnick,$mxitloc,$mxitprof)") or die(mysqli_error()); Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/#findComment-1492151 Share on other sites More sharing options...
cobusbo Posted September 26, 2014 Author Share Posted September 26, 2014 most likely the column names are spelt incorrectly in the query or incorrect data type you're inserting to db. Have you tried to debug it using a mysqli_error() function. Something like $query = mysqli_query($con,"INSERT INTO mxit (ip,time,user_agent,contact,userid,id,login,nick,location,profile) VALUES ($ip,$post_time,$mxitua,$mxitcont,$mxituid,$mxitid,$mxitlogin,$mxitnick,$mxitloc,$mxitprof)") or die(mysqli_error()); ok the following error appears Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/vol1_1/mzzhost.com/mzzho_15247412/htdocs/try/mxit.php on line 44 Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/#findComment-1492156 Share on other sites More sharing options...
jazzman1 Posted September 26, 2014 Share Posted September 26, 2014 Try to pass the link identifier as a parameter. .................or die(mysqli_error($con)); Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/#findComment-1492158 Share on other sites More sharing options...
cobusbo Posted September 26, 2014 Author Share Posted September 26, 2014 Try to pass the link identifier as a parameter. .................or die(mysqli_error($con)); Ok this is the error that appears You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.12.51,1411750180,,,,DEFAULT,,,,)' at line 2 Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/#findComment-1492160 Share on other sites More sharing options...
Ch0cu3r Posted September 26, 2014 Share Posted September 26, 2014 Some of the values you are inserting in the query contain strings. String values need to be wrapped in quotes. They should also be sanitized so they are are handled safely in the query. Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/#findComment-1492167 Share on other sites More sharing options...
gizmola Posted September 26, 2014 Share Posted September 26, 2014 Some of the values you are inserting in the query contain strings. String values need to be wrapped in quotes. They should also be sanitized so they are are handled safely in the query. Or better still, use a prepared statement and bind the parameters. See http://php.net/manual/en/mysqli-stmt.bind-param.php $stmt = $con->prepare("INSERT INTO mxit (ip,time,user_agent,contact,userid,id,login,nick,location,profile) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") or die(mysqli_error()); $stmt->bind_param("ssssssssss", $ip, $post_time, ..... etc); $stmt->execute(); printf("%d Row inserted.\n", $stmt->affected_rows); Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/#findComment-1492172 Share on other sites More sharing options...
cobusbo Posted September 26, 2014 Author Share Posted September 26, 2014 Ok I've manage to get my entries to work, but I'm currently experiencing a problem checking for duplicates. I want to use my userid column and check if a duplicate exist and if it does it should just ignore the entry in the background. My current code is <?php $con=mysqli_connect("sql305.mzzhost.com","mzzho_15247412","92295454","mzzho_15247412_mxit"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?> <? define('TIMEZONE', 'Africa/Harare'); date_default_timezone_set(TIMEZONE); $ip = $_SERVER["REMOTE_ADDR"]; $post_time = date("U"); $mxitua = $_SERVER["HTTP_X_DEVICE_USER_AGENT"]; $mxitcont = $_SERVER["HTTP_X_MXIT_CONTACT"]; $mxituid = $_SERVER["HTTP_X_MXIT_USERID_R"]; $mxitid = $_SERVER["HTTP_X_MXIT_ID_R"]; $mxitlogin = $_SERVER["HTTP_X_MXIT_LOGIN"]; $mxitnick = $_SERVER["HTTP_X_MXIT_NICK"]; $mxitloc = $_SERVER["HTTP_X_MXIT_LOCATION"]; $mxitprof = $_SERVER["HTTP_X_MXIT_PROFILE"]; if(!isset($mxitid)) { $mxitid = "DEFAULT"; } $query = mysqli_query($con,"INSERT IGNORE INTO mxit (ip,time,user_agent,contact,userid,id,login,nick,location,profile) VALUES ('$ip','$post_time','$mxitua','$mxitcont','$mxituid','$mxitid','$mxitlogin','$mxitnick','$mxitloc','$mxitprof')") or die(mysqli_error($con)); ?> As you can see I tried to use the INSERT IGNORE option but it doesn't seem to work... any help please... Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/#findComment-1492180 Share on other sites More sharing options...
cobusbo Posted September 26, 2014 Author Share Posted September 26, 2014 Ok I've manage to get my entries to work, but I'm currently experiencing a problem checking for duplicates. I want to use my userid column and check if a duplicate exist and if it does it should just ignore the entry in the background. My current code is <?php $con=mysqli_connect("sql305.mzzhost.com","mzzho_15247412","92295454","mzzho_15247412_mxit"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?> <? define('TIMEZONE', 'Africa/Harare'); date_default_timezone_set(TIMEZONE); $ip = $_SERVER["REMOTE_ADDR"]; $post_time = date("U"); $mxitua = $_SERVER["HTTP_X_DEVICE_USER_AGENT"]; $mxitcont = $_SERVER["HTTP_X_MXIT_CONTACT"]; $mxituid = $_SERVER["HTTP_X_MXIT_USERID_R"]; $mxitid = $_SERVER["HTTP_X_MXIT_ID_R"]; $mxitlogin = $_SERVER["HTTP_X_MXIT_LOGIN"]; $mxitnick = $_SERVER["HTTP_X_MXIT_NICK"]; $mxitloc = $_SERVER["HTTP_X_MXIT_LOCATION"]; $mxitprof = $_SERVER["HTTP_X_MXIT_PROFILE"]; if(!isset($mxitid)) { $mxitid = "DEFAULT"; } $query = mysqli_query($con,"INSERT IGNORE INTO mxit (ip,time,user_agent,contact,userid,id,login,nick,location,profile) VALUES ('$ip','$post_time','$mxitua','$mxitcont','$mxituid','$mxitid','$mxitlogin','$mxitnick','$mxitloc','$mxitprof')") or die(mysqli_error($con)); ?> As you can see I tried to use the INSERT IGNORE option but it doesn't seem to work... any help please... Nevermind I changed my Userid from text to varchar then I could make it an unique identifier and it solved my problem, thank you Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/#findComment-1492183 Share on other sites More sharing options...
jazzman1 Posted September 26, 2014 Share Posted September 26, 2014 Never used INSERT IGNORE, what insertion result do you get? Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/#findComment-1492185 Share on other sites More sharing options...
cobusbo Posted September 27, 2014 Author Share Posted September 27, 2014 Never used INSERT IGNORE, what insertion result do you get? If a duplicate record in your database Unique field is being detected it will just ignore inserting another row in the background without giving a message error or anything Link to comment https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/#findComment-1492210 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.