Member Avatar for skinbug

Hi all, when inserting values stored in an array to MySQL database, where do the values go?

I have a form with 5 checkboxes that may or may not be checked, their names all being an array.

Question: how do I write the sql file?

assuming the names are nature[]

would I need to do this...

nature varchar(50) null

Plz help!

Recommended Answers

All 4 Replies

Member Avatar for skinbug

Hi thanks for the response, but my question still stands...

Where do the values get stored in the database?

Surely there must be a column defined in the sql file that has a name, a data type, how many characters are expected, can it be blank, etc...

Like I said, would I need to do this...? nature varchar(50) null It is this part that I don't get.

Member Avatar for skinbug

ok I'm gonna add some code here, maybe this will clarify what I am trying to do...

register.php

<input type="text" name="firstName" value="<?php echo $form->value("firstName"); ?>" />
<input type="text" name="lastName" value="<?php echo $form->value("lastName"); ?>" />
<input type="checkbox" name="nature[]" value="0" />
<input type="checkbox" name="nature[]" value="1" />
<input type="checkbox" name="nature[]" value="2" />

process.php

$retval = $session->register($_POST[firstName], $_POST['lastName'] ($question = implode(",",$_POST['questions'])));

session.php

function register($subfirstname, $sublastname, $subquestion) {
     // firstname error checking

     // lastname error checking

     // Errors exist, have user correct them
     if($form->num_errors > 0) {
          return 1; // Errors with form
     }
     else {
          if($database->addNewUser($subfirstname, $sublastname, $subquestion)) {
               return 0; // New user added successfully
          }
          else {
               return 1; // Registration failed
          }
     }
}

database.php

function addNewUser($firstname, $lastname, $questions) {
     $q = "INSERT INTO users VALUES ('$firstname', '$lastname', $questions)";
     return mysql_query($q, $this->connection);
}

tables.sql

DROP TABLE IF EXISTS users

CREATE TABLE users (
     firstname   varchar(30) not null,
     lastname    varchar(30) not null
);

========


you will notice that the tables.sql does not have a column defined for questions. Should there be one?

Areas that I am not sure about are the sql and the formatting of the implode in process.php

Please help me someone if you can,

thanks.

hi,
create a new row in database call it as nature and write like this

$string = implode(",",$_POST['nature']);

$query = "insert into table (nature) values ('$string')";

mysql_query($query);

it will inset the values into database with comma seperated

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.