Solved

mysql query not inserting data

Posted on 2011-09-22
7
265 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

770 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