Jump to content

Recommended Posts

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 44

Warning: 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...

 

Edited by cobusbo
Link to comment
https://forums.phpfreaks.com/topic/291298-mysqli-and-php-problem/
Share on other sites

 

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

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()); 


 

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

 

 

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

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);

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...

 

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

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

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.