Solved

mysql query not inserting data

Posted on 2011-09-22
7
270 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

739 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