I am coding a kind of simple registration form for a company for employees to select the dates the will be available for work. I am having trouble when a user selects multiple dates, it writes 0000-00-00 in the database. I would like it to make two records of both dates.
This is my basic form code:
<form method="post" action="action.php">
<input name="UserID" type="hidden" value="3">
<input type="checkbox" name="Datum[]" value="2014-01-04">4 january
<input type="checkbox" name="Datum[]" value="2014-01-06">6 january
<input name="submit" type="submit" />
</form>
My action.php looks like this:
<?php
include("config.php");
$UserID=$_POST['UserID'];
$Datum=$_POST['Datum'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test</title>
</head>
<body>
<?php
$res = mysql_query("insert into workingdays (UserID, Datum)
values ('$UserID', '" . implode(',', $_POST['Datum']) ."')");
if(!$res){
echo "Error! <a href='/mysql'>Go back.</a>";
}
else {
echo "Succesful. <a href='/mysql'>Go back.</a>";
}
?>
</body>
</html>
I use two tables.
wp_users (the actual wordpress table):
ID | display_name | Email
workingdays:
ID | UserID | Date
The UserID of 'workingdays' and the ID of wp_users are joined. I would like to see every option of the checkbox get its own row.
How can I achieve this?
First foreach the $_POST['Datum'] then mysql insert.
foreach ($_POST['Datum'] as $date){
$res = mysql_query("insert into workingdays (UserID, Date)
values ('$UserID', '$date')");
if(!$res){
echo "Error! <a href='/mysql'>Go back.</a>";
}
else {
echo "Succesful. <a href='/mysql'>Go back.</a>";
}
}
I hope this is what you meant..
If its only a couple fields per user, use http://codex.wordpress.org/Function_Reference/update_user_meta and http://codex.wordpress.org/Function_Reference/get_user_meta
which uses user_meta table based on user ID