Jump to content

[SOLVED] Inserting multiple rows into mysql with checkbox


Recommended Posts

Once again, I need some help from you php gurus. I have a registration form that I've created for a member's database. On one section of the form, the user is to select different topics that are of interest to them. I have used checkboxes on the form so they can select multiple topics. My problem is that the data is not being inserted into my database.

 

Topics section of form code:

 

<table border="0" cellpadding="0" cellspacing="5">
<tr>
	<th><h2>I am interested in these topics:</h2></th> 
</tr>
<tr>
	<th><p class="left">Check all that apply.</p></th>
</tr>
<tr>
	<td><input type="checkbox" name="topics[]" value="Networking"> Networking</td>
</tr>
<tr>
	<td><input type="checkbox" name="topics[]" value="Power Lunches"> Power Lunches</td>
</tr>
<tr>
	<td><input type="checkbox" name="topics[]" value="Entrepreneur Workshop"> Entrepreneur Workshop</td>
</tr>
<tr>
	<td><input type="checkbox" name="topics[]" value="Financial and Retirement Planning"> Financial and Retirement Planning</td>
</tr>
<tr>
	<td><input type="checkbox" name="topics[]" value="Executive Coaching"> Executive Coaching</td>
</tr>
<tr>
	<td><input type="checkbox" name="topics[]" value="Mentoring"> Mentoring Program</td>
</tr>
<tr>
	<td><input type="checkbox" name="topics[]" value="Charity"> Charity Involvement</td>
</tr>
<tr>
	<td><input type="checkbox" name="topics[]" value="Encouragement"> Encouragement</td>
</tr>
<tr>
	<td><input type="checkbox" name="topics[]" value="Career Change/Advice"> Career Change/Advice</td>
</tr>
<tr>
	<td>Other <input type="text" name="topics[]" size="25"></td>
</tr>
</table>

 

My register.php code:

 

<?php

$conn = mysql_connect("****", "****", "****") 
   or die($msg_no_connect);
   mysql_select_db("****") 
   or die(mysql_error());

$fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$username=$_POST['username'];
$password=$_POST['password'];
$password2=$_POST['password2'];
$address1=$_POST['address1'];
$address2=$_POST['address2'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$newsletter=$_POST['newsletter'];
$breakingnews=$_POST['breakingnews'];
$alerts=$_POST['alerts'];
$partners=$_POST['partners'];
$topics=$_POST['topics'];
$industry=$_POST['industry'];
$function=$_POST['function'];
$role=$_POST['role'];
$age=$_POST['age'];
$income=$_POST['income'];
$marital_status=$_POST['marital_status'];
$children=$_POST['children'];
$children_age=$_POST['children_age'];


?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../../css/members.css">
</head>

<body>

<h1>Membership Registration</h1>
<?php $sql = "select * from users where username='$username' and password='$password'";
   $res = mysql_query($sql);
   if(mysql_num_rows($res) > 0){
   echo "<p>That username is already taken. Please <a href='join.php'>go back</a> and select another one.</p>";
   }

   if ($password != $password2) {
   echo "<p>The passwords you have entered do not match. Please <a href='join.php'>try again</a>.</p>";
   }
   else if (mysql_num_rows($res) == 0 ) {

   $res2 = mysql_query("insert into users (fname, lname, username, password, email, address1, address2, city, 
   			 state, zip, newsletter, breakingnews, alerts, partners, topics, industry, function, role, 
   			 age, income, marital_status, children, children_age) 
  			     values ('$fname', '$lname', '$username', '$password', '$email', '$address1', '$address2', '$city', 
			 '$state', '$zip', '$newsletter', '$breakingnews', '$alerts', '$partners', " . implode(',', $_POST['topics']) .", 
			 '$industry', '$function', '$role', '$age', '$income', '$marital_status', '$children', '$children_age')");

   			    if(!$res2){
     			echo "<p>We're sorry. You could not be registered at this time. Please contact the administrator for assistance.<p>";
  				}
			else {
			echo "You have successfully registered. You may now <a href='login.php'>Login</a></p>";
			}
		}
  		 ?>


</body>
</html>

 

When I hit submit, I get the error message "We're sorry. You could not be registered at this time. etc.. " I have checked the other functions such as passwords not matching and if the username has already been taken and they work fine. The problem is inserting the data. If I change " . implode(',', $_POST['topics']) ." to just '$topics', everything else gets inserted into the database, but the topics comes back as "array". My question is, is this the correct way to insert this type of data? Help or suggestions are greatly appreciated!

 

Thank you,

wikedawsum

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.