0

I am at a loss why my query is not inserting values into db. Even if I do echo $query or var_dump($query) there is nothing printed at all. All values are being passed successfully just not being inserted. I am getting Could not connect but I do not know why. All connections are established and as a test I took this code and ran it on its own with dummy data and it inserted the data fine. I can only think it has something to do with the $response_array. Where am I going wrong?

<?php require_once('Connections/sample.php'); ?>
<?php

session_start();

$new = 1;
$activity = 'General Contact Enquiry';
$mobile = 'Submitted from mobile';
$name = mysql_real_escape_string($_POST['GC_name']);
$department = mysql_real_escape_string($_POST['GC_department']);
$message = mysql_real_escape_string($_POST['GC_message']);
$email = mysql_real_escape_string($_POST['GC_email']);
$company = mysql_real_escape_string($_POST['GC_company']);
$position = mysql_real_escape_string($_POST['GC_position']);

//response array with status code and message
$response_array = array();

//validate the post form

//check the name field
if(empty($name)){

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'Name cannot be blank';

//check the name field
} elseif(empty($company)) {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'You must enter a company name';

//check the position field
}elseif(empty($position)) {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'You must enter a position';

//check the email field
} elseif(empty($email)) {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'You must enter a valid email address';

//check the dept field
}elseif($department=="Choose Department") {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'You must select a department';

//check the message field
}elseif(empty($message)) {

    //set the response
    $response_array['status'] = 'error';
    $response_array['message'] = 'You must enter a message';

//check the dept field
}
else {

    //set the response
    $response_array['status'] = 'success';
    $response_array['message'] = 'Your enquiry has been sent succesfully';
    $flag=1;
 }

//send the response back
echo json_encode($response_array);

if($flag == 1) {

    mysql_select_db($database_sample, $sample);
    $query = 'INSERT INTO feedback (company, department, name,  email, position, feedback, date, new) VALUES (\''.$company.'\', \''.$department.'\', \''.$name.'\', \''.$email.'\', \''.$position.'\', \''.$message.'\', NOW() , \''.$new.'\')';
    mysql_query($query) or die("Could not connect");
    }


?>
7
  • Are you sure the "Could not connect" message is coming from that query and not the database connection script? Try to change the message to something else and see if you still get the same message. Commented Sep 23, 2011 at 9:15
  • Looks fine to me. You don't need extra PHP tags for require_once, but that's not the error. Could not connect is a pretty clear error message - are you REALLY sure, the connection works? The error message tells me something else. Commented Sep 23, 2011 at 9:17
  • @SebastianWramba Well, the OP supplied the error message himself so it doesn't really tell anything... Commented Sep 23, 2011 at 9:21
  • @Juhana, The message is definitely coming from the code I posted. Thanks Commented Sep 23, 2011 at 9:23
  • @SebastianWramba, I shall take the connection out of the loop and try my own and report back in 5 mins. thanks Commented Sep 23, 2011 at 9:24

3 Answers 3

2

Did you try mysql_query($query) or die (mysql_error());

Hope so you will getting right error description.

Sign up to request clarification or add additional context in comments.

3 Comments

after inserting mysql_error() statement please let us know.. and please varify the correct syntax for mysql_error(). for you php version.
fakhir, I already tried with the error. I am using 5.1.6. thanks
did you pass mysql_error() in die function.. it always return error message in error case.. even when you are not connected with mysql datebase....
1

May be you need to verify the $query content.

Try this way,

$query = "INSERT INTO `feedback` (`company`, `department`, `name`,  `email`,
          `position`,`feedback`, `date`, `new`) VALUES 
          ('$company','$department','$name','$email','$position',
          '$message',NOW(),'$new')";

2 Comments

thank you. that was it. Could you explain why that would make a difference. thanks
@bollo First of all back-quote is used to avoid possible ambiguity due to reserved words of mysql and double quoted string is used to format sql statement without appending single quotes. I'd like to suggest you to use PDO or mysqli to ease the database operations.
1

Use proper error checking and debugging to find out what the problem is.

That way, you can find out what goes wrong yourself! :)

For example, you could use simple echo statements to find out the following:

  • Is $flag really 1, is that code block ever actually executed?
  • What does $query contain? Is it a proper SQL query?
  • Does mysql_error() say anything?

5 Comments

pekka, I have done what you suggested and they all pass. mysql_error() says nothing, that is why I put in could not connect. thanks
@bollo what does $query contain?
if you are refering to type, they are strings.
@bollo what does $query contain?
@bollo by the way, if you die("could not connect"), then a echo mysql_error() afterwards won't show up of course. Just in case that is the problem

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.