Hello everybody,

I want to insert some data with an array to my mysql database, but it won't work. These are my codes, what could be the problem?

register.php:

if (empty($_POST) === false) {

    $required_fields = array('first_name','last_name','number','district','street', 'house_number', 'appointment_date', 'comment', 'anchor');
    foreach($_POST as $key=>$value)

    {
        if (empty($value) && in_array($key, $required_fields) === true)
        {
            $errors[] = 'Fields marked with an asterisk are required';
            break 1;

        }
    }
}       
?>
<h1>Register</h1>
<?php
if (isset($_GET['success']) && empty($_GET['success'])) {
} else {
    if (empty($_POST) === false && empty($errors) === true) {
    $register_data = array(
    'first_name'        => $_POST['first_name'],
    'last_name'         => $_POST['last_name'],
    'number'            => $_POST['number'],
    'district'          => $_POST['district'],
    'street'            => $_POST['street'],
    'house_number'      => $_POST['house_number'],
    'appointment_date'  => $_POST['appointment_date'],
    'comment'           => $_POST['comment'],
    'achor'             => $_POST['anchor']
);
echo $register_data;
register_user($register_data);
header('Location: register.php?success');
exit();
} else if (empty($errors) === false) {
echo output_errors($errors);
}

register_user function php file:

function register_user($register_data) {
array_walk($register_data, 'array_sanitize');
$fields = '`' . implode('`, `', array_keys($register_data)) . '`';
$data = '\'' . implode('\', \'', $register_data) . '\'';

mysql_query("INSERT INTO `sikeres` ($fields) VALUES ($data)");

}

If someone could help me i would really appreciate it!

Tibor

Recommended Answers

All 8 Replies

EDIT: My bad wasn't reading correctly. Let me take another look :p.

What is the error you are getting?

I am getting no error, it just simply doesn't create a new record in the table.

What happens if you echo your query string? E.g.

echo "INSERT INTO `sikeres` ($fields) VALUES ($data)";

Examine the constructed query to spot possible errors. Add the following temporary debug code to the register_user function:

function register_user($register_data) {
    array_walk($register_data, 'array_sanitize');
    $fields = '`' . implode('`, `', array_keys($register_data)) . '`';
    $data = '\'' . implode('\', \'', $register_data) . '\'';

    // temporary debug code
    die("INSERT INTO `sikeres` ($fields) VALUES ($data)");

    mysql_query("INSERT INTO `sikeres` ($fields) VALUES ($data)");
}

This will display the query as it is constructed, and stop the script. Please post the displayed query here.

Also the function should return some feedback about the success. I would add some error checking and use the return value of the mysql_query function:

function register_user($register_data) {
    array_walk($register_data, 'array_sanitize');
    $fields = '`' . implode('`, `', array_keys($register_data)) . '`';
    $data = '\'' . implode('\', \'', $register_data) . '\'';


    if(mysql_query("INSERT INTO `sikeres` ($fields) VALUES ($data)")) {
        return true;
    } else {
        return false;
    }
}

And last but not least: mysql_* functions are dinosaurs. They are on the verge of extinction. They are going to be dropped from PHP very soon. If you like yourself replace them with PDO or at least mysqli_* functionas.

When I typed in your debug code example, after registering, i got this code:

ArrayINSERT INTO `sikeres` (`first_name`, `last_name`, `number`, `district`, `street`, `house_number`, `appointment_date`, `comment`, `achor`) VALUES ('Test', 'Test', '06305593022', '15', 'na', '12', '2013-09-25', 'qwe', 'qwe')

Thanks for your replies in advance!

Have you tried adding or die... to your query line? E.g.:

mysql_query(..query..) or die(mysql_error());

ArrayUnknown column 'achor' in 'field list'
This is what i get.

Okay I solved it... It should've been 'anchor' istead of 'achor'. What a dumb mistake sorry for everybody's time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.