Solved

mysql query not inserting data

Posted on 2011-09-22
7
266 Views
Last Modified: 2012-05-12
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. There is also no error messages or numbers. Where am I going wrong. Thanks

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

//validate the post form

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

    //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';
    
 }

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

 
$query = 'INSERT INTO `feedback` (`company`, `department`, `name`,  `email`, `position`, `feedback`, `date`, `new`)
             VALUES (\''.$company.'\', \''.$department.'\', \''.$name.'\', \''.$email.'\', \''.$position.'\', \''.$feedback.'\', NOW() , \''.$new.'\');';
    mysql_query($query) or die(mysql_errno());var_dump($query);

Open in new window

0
Comment
Question by:peter_coop
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:amigura
ID: 36581749
run this to see it if inserts data. but it might be ;'; at end of values in your code

// test data
$company= 'test company';
$name= 'test name';
// test data


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

Open in new window

0
 

Author Comment

by:peter_coop
ID: 36583213
@amigura
still the same. it's almost like it stops at the json_decode which transmits successfully. thanks
0
 
LVL 7

Expert Comment

by:amigura
ID: 36584493
make a php file and put this in it to see what happens.

// fill in database info
$db_host='localhost';
$db_name='database name';
$db_user='root';
$db_pass='123';


$db="$db_name"; mysql_select_db($db, $con);
define('DB_CONNECTION',mysql_connect($db_host,$db_user,$db_pass));  // db
mysql_select_db($db_name, DB_CONNECTION); // db  



// test data
$company= 'test company';
$name= 'test name';
// test data

$query = 'INSERT INTO `feedback` (`company`, `department`, `name`,  `email`, `position`, `feedback`, `date`, `new`)
             VALUES (\''.$company.'\', \''.$department.'\', \''.$name.'\', \''.$email.'\', \''.$position.'\', \''.$feedback.'\', NOW() , \''.$new.'\');';
    mysql_query($query) or die(mysql_errno()); var_dump($query);

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:peter_coop
ID: 36585473
firstly, where is $con defined. Made connection to db, inserted the first time, then if I change values, nothing. Also, the echo is still not outputting either in chrome or firebug nor var_dump. thanks
0
 
LVL 2

Accepted Solution

by:
maricksville earned 250 total points
ID: 36586301
With your original script can you try two things. These are just quick assessments while I'm at work.

Can you remove the first ; from line 62 so that instead of reading as );'; it reads )';

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

Open in new window


Secondly can you move the query above the JSON encode:

$query = 'INSERT INTO `feedback` (`company`, `department`, `name`,  `email`, `position`, `feedback`, `date`, `new`)
             VALUES (\''.$company.'\', \''.$department.'\', \''.$name.'\', \''.$email.'\', \''.$position.'\', \''.$feedback.'\', NOW() , \''.$new.'\');';
    mysql_query($query) or die(mysql_errno());var_dump($query);

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

Open in new window

0
 
LVL 2

Expert Comment

by:maricksville
ID: 36586339
On another note, are the variables that you are submitting to the database table converted from a $_POST array?

A form that submits a text field with the name 'test' will be sent via PhP $_POST var and will apear as $_POST['test']. To access this variable in the array I would do the following:

$company = $_POST['test'];

Simply trying to access $test without the above assignment will not do anything. While the value $test equates to FALSE or EMPTY it can still be entered into a MySQL table and MySQL will return a TRUE result on the query.

To check the values that are being sent from your form you can var_dump($_POST) or print_r($_POST). If you have already done so you can use the following to extract the values from the POST array. This is assuming the variables are the same name as your input fields.

$company = $_POST['company'];
$department = $_POST['department'];
$name = $_POST['name'];
$email = $_POST['email'];
$position = $_POST['position'];
$feedback = $_POST['feedback'];
$new = $_POST['new'];

Open in new window

0
 

Author Closing Comment

by:peter_coop
ID: 36594849
thank you
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question