0

Currently I have a problem with one of my queries - it seems not to be inserting any of the values into my database. Below is the code I am using for a form submitting.

<?php
if (empty($_POST) === false) {
    $assignment_data = array(
        'title'         => $_POST['title'],
        'number'        => $_POST['number'],
        'weighting'     => $_POST['weight'],
        'handout'       => $_POST['handout'],
        'handin'        => $_POST['handin'],
        'feedback'      => $_POST['feedback'],
        'wordcount'     => $_POST['wordcount'],
        'brief'         => $_POST['brief'],
        'sub_details'   => $_POST['details'],
        'add_note'  => $_POST['notes']
    );

    create_assignment($assignment_data);
    //header('Location: modules.php');
    //exit();
}
function create_assignment($assignment_data) {
    array_walk($assignment_data, 'array_sanitize');

    $fields = '`' . implode('`, `', array_keys($assignment_data)) . '`';
    $data = '\'' . implode('\', \'', $assignment_data) . '\'';

    $query = mysql_query("INSERT INTO `assignments` ($fields) VALUES ($data)");
    echo $fields;
    echo $data;
    $test = mysql_query($query) or die(mysql_error());;

    print $test;
}

?>

I am getting this as the result from entering information into the form and submitting it:

`title`, `number`, `weighting`, `handout`, `handin`, `feedback`, `wordcount`, `brief`,
`sub_details`, `add_note`

'Dream Design', '2', '20', '07/01/2014', '08/01/2014', '08/01/2014', '2', 'asd',
'asdasd', 'asdasdasd'

Query was empty.

The information is being placed in the $fields and $data variables but seems to be not running the query... Nothing is being inserted into my database.

Any help or assistance is greatly appreciated.

3
  • 1
    What does your query look like after the variables are interpolated? Commented Jan 30, 2014 at 20:33
  • what is your mysql_error() ?? Commented Jan 30, 2014 at 20:37
  • $query = ("INSERT INTO assignments ($fields) VALUES ($data)"); - you are using mysql_query twice. Commented Jan 30, 2014 at 20:39

2 Answers 2

4

You have:

$query = mysql_query("INSERT INTO `assignments` ($fields) VALUES ($data)");

and then

$test = mysql_query($query) or die(mysql_error());;
                    ^^^^^^--- result of previous query

You can't use a query handle to "re-run" a query. You need to save your query into a variable, and re-use that. e.g.

$sql = "INSERT ...";

$query = mysql_query($sql) or die(mysql_error());
$test = mysql_query($sql) or die(mysql_error());

Your first query is probably failing, returning a boolean FALSE. That false, when passed in to another query call, will be type-cast to an empty string, hence your "empty query" error message. Adding the or die(...) business will tell you exactly what's wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect answer, thanks for the great explanation. I found out the problem from using the error message as well. I forgot to set a default value for a field in the table - silly me!
0

You've run mysql_connect, right?

Try mysql_stat() to make sure.

You can also try to get some error output right in the first query:

$query = mysql_query("INSERT INTO `assignments` ($fields) VALUES ($data)") or die(mysql_error());

And please please please consider using PDO, a framework, or at least mysqli_

1 Comment

I know I should be using PDO/mysqli, I've only recently picked up learning PHP and this is what the tutorial I used taught me sadly - I will be redoing all the queries for PDO once I have a better grasp and understanding though! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.