Solved

mysql query not inserting data

Posted on 2011-09-22
7
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

632 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