Click to See Complete Forum and Search --> : Storing Data in Mysql Database


AHendersonWebDe
04-21-2008, 01:23 PM
Dear Expert:
I am creating a form where the user enters and selects data options. I want to store the results in my database yet im not sure how to store "checkbox" fields and "selects" fields
I created a table called chancellor but im not sure what Type to choose. Below is what i currently have......

My Fields are:
FLname ......... text utf8_general_ci
candidate ......... text utf8_general_ci
check ......... text utf8_general_ci
Message ......... text utf8_general_ci
rank ......... text utf8_general_ci









<html>
<head>
<title>Candidate Ratings</title>
</head>
<body>
<form method="post" action="chancellor.php">

Committee Member First and Last Name:<input type="text" size="30" maxlength="30" name="FLname"><br />



SelectCandidate:<br />
<select name="candidate">
<option value="Porsha Williams">Jr.High</option>
<option value="Gary">HighSchool</option>
<option value="Aisha">College</option>
</select><br />


Please check Best Representation of Candidate:<br />
Meets Expectations:<input type="checkbox" value="Meets" name="check[]"><br />
Exceeds Expectations:<input type="checkbox" value="Exceeds" name="check[]"><br />
Does Not Meet Expectations:<input type="checkbox" value="DNot" name="check[]"><br />


<textarea rows="5" cols="20" name="Message" wrap="physical">Enter Additional Comments</textarea><br />


Overall Ranking of Candidate 1-5 (5 Being the Highest):<br />
<select name="rank" size="5">
<option value="one">1 - One</option>
<option value="two">2 - Two</option>
<option value="three">3 - Three</option>
<option value="four">4 - Four</option>
<option value="five">5 - Five</option>
</select><br />


<input type="submit" value="submit" name="submit">
</form>







<?php
$FLname = $_POST["FLname"];
$candidate = $_POST["candidate"];
$check = $_POST["check"];
$Message = $_POST["Message"];
$rank = $_POST["rank"];



if (!$FLname)
{
echo '<h3>You have not entered your name.</h3>'
.'<h4>Please go back and try again.</h4>';
exit;
}



if (!get_magic_quotes_gpc())
{
$FLname = addslashes($FLname);
$candidate = addslashes($candidate);
$check = addslashes($check);
$Message = addslashes($Message);
$rank = addslashes($rank);
}





//changes the location of the @ symbol
$db = @mysql_connect('xxxx', 'username', 'pw');

//select the DB here
mysql_select_db('database', $db);

//changed this to be correct
if (mysql_error())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = "INSERT INTO chancellor (FLname, candidate, check, Message, rank) VALUES ('$FLname', '$candidate', '$check', '$Message', '$rank')";

//changed this to mysql_query
$result = mysql_query($query);

//added brackets and changed it to mysql_num_rows
if ($result) {
echo '<img border="0" src="logo.jpg" align="center" width="291" height="81" alt="Parker Executive Search" /> <br /><br />';

echo '<h3>Thank you, Your Data has been Recorded</h3>';
}

mysql_close()
//you don't need to call mysql_close(), this will be called implicitely when the script finishes execution
?>

jamesm6162
04-26-2008, 06:03 AM
In your DB you usually have one yes/no field for each textbox, however it seems that radio buttons would better serve your purpose. Each radio input has the same name.

Example:


Please check Best Representation of Candidate:<br />
Meets Expectations:<input type="radio" value="Meets" name="check"><br />
Exceeds Expectations:<input type="radio" value="Exceeds" name="check"><br />
Does Not Meet Expectations:<input type="radio" value="DNot" name="check"><br />


In your DB the "check" field for the radio inputs, and the "candidate" field can just be a varchar datatype