Solved

mysql query not inserting data

Posted on 2011-09-22
7
263 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now