0

I have two very long list:

$countryCode 

And

$countryName

To insert these list i have created the following loop:

   $i = 0;

$con=mysqli_connect("localhost","root","root","system_bloglic_com_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

foreach($countryCode as $cCode){
mysqli_query($con,"INSERT INTO Contries (contries_id, contries_name, contries_code)
VALUES(NULL,$cCode,$countryName[$i])");
$i++;
}

When i run this i get no erros but no rows have been inserted into database how come?

6
  • 4
    You won't get errors because you aren't checking for any errors. Commented Aug 20, 2013 at 13:50
  • What @BenFortune said, also do you mean to be typing "countries" as "contries" interchangeably within the query? Commented Aug 20, 2013 at 13:50
  • Maybe it is a typo, contries countries. Commented Aug 20, 2013 at 13:51
  • country name and code are string so you need to insert them as strings: Commented Aug 20, 2013 at 13:51
  • @MarcRasmussen VALUES(NULL,"$cCode","$countryName[$i]") Commented Aug 20, 2013 at 13:54

4 Answers 4

2

did you check mysqli_error(); it will help you to find exactly the error. It returns FALSE if an error occurred. In that case mysqli_error() gives you more information about the error.

$result = mysqli_query($con,"INSERT INTO Contries (contries_id, contries_name, contries_code)
VALUES(NULL,$cCode,$countryName[$i])");
if ( false===$result ) {
  printf("error: %s\n", mysqli_error($con));
}
else {
  echo 'done.';
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your string values should always be enclosed within quotes

mysqli_query($con,"INSERT INTO Contries (contries_id, contries_name, contries_code)
VALUES(NULL,'{$cCode}','{$countryName[$i]}')");

and also you should use error handling as instructed by others using die() method

Comments

1

You should change

mysqli_query(...);

into

mysqli_query(...) or die("error in query: " . mysqli_error($con));

To echo out any errors. Otherwise errors are silently ignored.

Comments

0

You need to check for errors in your code.

if(!mysqli_query($con,"INSERT INTO Contries (contries_id, contries_name, contries_code)
VALUES(NULL,'$cCode','$countryName[$i]')")){
    echo mysqli_error($con);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.