Hey Sorry to bug you guys again. I had one more question. As i am developing this program now i realize that i have no clue how to save the data to the database.

basically the whole point in making this program was to be able to export a table in my database that would store all the submitted data from the survey.

I will have like 30 questions and almost 400 grads. all that data needs to be recorded.

What is going to be the easiest table structure to do that type of thing?

Or is e-mail just going to end up being easier?

Recommended Answers

All 62 Replies

CREATE TABLE test (
id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
name VARCHAR( 100 ) NOT NULL ,
address VARCHAR( 100 ) NOT NULL
) ENGINE = InnoDB

If that's my table, and I have a form with 2 fields name and address, this is how I would be inserting the values to the table test. This should get you started.(I hope!)

<?php
if(isset($_POST['submit'])){
$conn=mysql_connect("localhost","root");
mysql_select_db("exact");
$name=$_POST['name'];
$address=$_POST['address'];
$query="INSERT INTO TEST (ID,NAME,ADDRESS) VALUES ('','$name','$address')";
mysql_query($query);
}
?>
<html>
<body>
<form method="post" action="insert_example.php">
Enter Name: <input type="text" name="name"><br />
Enter Address: <textarea rows="10" cols="20" name="address"></textarea>
<br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Cheers,
Nav

so if i wanted to send all the results via e-mail i would do something similar? i am going to search on that site you gave me and that book lets see if i can get it....

I found out how to mail it i just need some help with the security now.

I want to stop people from just going to the page vote.php, i want them going through the login how is that done?

You can use mail function.
Eg. mail("to address","subject","message contents","from: from address");

so i know that part but how do i then integrate it with the submit button? And how do i tell the mail function what to mail... Like what form questions I wasnt able to find out how to do that yet

Well, you can have a condition to check if the submit button was pressed.

if(isset($_POST['submit'])){
$message="This is the body of this mail. blah blah blah..........some more text... ";
mail("test@gmail.com","test mail",$message,"from: someone@gmail.com");
}

you can send all the data in the message part of the mail.

ok so basically i can wrap that around the whole form were they are entering the info?

Yep. Get all the form information, align it as you want. Put it in the 'message' part of the mail.

k so 1 more thing

In the email part i dont know how to retrieve the persons answers from the folllowing...

<label>40. Funniest</label><br />
<select name="funniest">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

...

Thanks, Dani

On submitting the page, if the user has selected a value from the select box, it will be in $_POST. So, you can do something like $x=$_POST. $x will have his selection.

just goes to show i dont know php enough yet... I actually bought another book so i will be learning it for sure.

Thank you very very much for your help, so glad someone was able to as i had to get this done.

Thanks again

Ok after about an hour of trying to figure this part out i wasn't able to....

So i have those variables

$a=$_REQUEST['priceisright'];

"I read that i should be using request and post in the message part obviouslly..."

Now when i go to e-mail

// Mail Script
if(isset($_POST['submit'])){
$message = "Thank you for registering with our site!\nYour selection was '{$_POST['style']}'.\n\nSincerely,\nUs";
mail("****@anblickstudios.com","Grad Survey 08",$message,"from: *****@anblickstudios.com");
}
?>

Now i am pretty sure i have the wrong format in this portion

'{$_POST['style']}'.

Mind helping me with my format there. I almost have it

no problemo! :)


Instead of {$_POST}, you can assign that to a variable and use that variable. Its little confusing for starters on how php handles strings.

// Mail Script
if(isset($_POST['submit'])){
$selection=$_POST['style'];
$message = "Thank you for registering with our site!\nYour selection was $selection.\n\nSincerely,\nUs";
mail("****@anblickstudios.com","Grad Survey 08",$message,"from: *****@anblickstudios.com");
}
?>

You can do that.

so then for multiple selections i just use commas?

so it would be

$selection=$_POST['style'], $_POST['funniest'];

or can i just go and use what i already typed?

$a=$_POST['priceisright'];
$b=$_POST['millionaire'];
$c=$_POST['loosepassport'];
$d=$_POST['vanriver'];
$e=$_POST['spusecheat'];
$f=$_POST['marrymoney'];
$g=$_POST['5divorces'];
$h=$_POST['primeminister'];
$i=$_POST['teacheci'];

I am guessing that would work now wont it?

Yep. What you have done is correct.

$x=$_POST['field1'];
$y=$_POST['field2'];
$z=$_POST['field3'];
....

//In case you want to concat them, you can do it this way. 
$string = $x." ".$y." ".$z; //This will concat all the three variables separated by a space.
...

:) Hope that helps.

hmm only issue is it will still send me back nothing...

Thats so weird? I select something and it just sends me nothing

Try out a sample mail first to your own id. If you get it, then the problem is with your script. If you dont get a mail, then probably, your smtp is not configured properly.

sorry i didn't get what you said.

I get the e-mail it says this exactly

Thank you for registering with our site!
Your selection was .

Sincerely,
Us

ok..Then the problem is not with smtp.. Check if $_POST is sending a value.

lol sorry man i dont even know how to do that yet....

comment out mail(...) line in your script and on top of the script, right after <?php put this line. print_r($_POST); Then fill out the form once again, click on submit. Check If any value assigned to .

ok checked. Absolutely nothing is being assigned. I tried it on a bunch of other ones also. Nothing changes with the post message at the top...

Well, you need to post your script then.

<?php
print_r($_POST);
$conn=mysql_connect("***********.anblickstudios.com","************","*******"); 
mysql_select_db("gradsurvey");
$query="select user_id from grads";
$result=mysql_query($query);
$option="";
while($row=mysql_fetch_array($result)) {
   $option.="<option value=".$row['grads'].">".$row['user_id']."</option>";    
}
if(isset($_POST['submit'])) {
$query="UPDATE TABLE SET VOTE_STATUS = 1 WHERE USER_ID ='".$_POST['grads']."'";	
mysql_query($query);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Etobicoke Collegiate Institute's Grad Survey 2008</title>
</head>
<body>


<?php
// Welcome the user
echo '<h2>Welcome';
if (isset($_SESSION['user_id'])) {
	echo ", {$_SESSION['user_id']}!";
}
echo '</h2>';


?>



<?php
// Mail Script
if(isset($_POST['submit'])){
$selection=$_POST['style'];
$a=$_POST['priceisright'];
$b=$_POST['millionaire'];
$c=$_POST['loosepassport'];
$d=$_POST['vanriver'];
$e=$_POST['spusecheat'];
$f=$_POST['marrymoney'];
$g=$_POST['5divorces'];
$h=$_POST['primeminister'];
$i=$_POST['teacheci'];
$j=$_POST['40yearoldvirgin'];
$k=$_POST['livewparents'];
$l=$_POST['dieholdingcell'];
$m=$_POST['damagedliver'];
$n=$_POST['jail'];
$q=$_POST['plasticsurgery'];
$r=$_POST['adoptchildren'];
$s=$_POST['buyfriends'];
$t=$_POST['Dictionary'];
$u=$_POST['guitarhero'];
$v=$_POST['convent'];
$w=$_POST['souljaboywedding'];
$x=$_POST['nobelpeace'];
$y=$_POST['huggerenvironment'];
$z=$_POST['mack'];
$aa=$_POST['ego'];
$ab=$_POST['partier'];
$ac=$_POST['cougar'];
$ad=$_POST['awkward'];
$ae=$_POST['dramtic'];
$af=$_POST['gossip'];
$ag=$_POST['cluts'];
$ah=$_POST['car'];
$ai=$_POST['style'];
$aj=$_POST['hair'];
$ak=$_POST['cutestcouple'];
$al=$_POST['hottestparents'];
$am=$_POST['playboygirl'];
$an=$_POST['kingswaymd'];
$aq=$_POST['funniest'];
$ar=$_POST['athletic'];

$message = "Price is right: $a";
mail("email@anblickstudios.com","Grad Survey 08",$message,"from: email@anblickstudios.com");
}

// Mail Script
if(isset($_POST['submit'])){
$selection=$_POST['style'];
$message = "Thank you for registering with our site!\nYour selection was $selection.\n\nSincerely,\nUs";
mail("****@anblickstudios.com","Grad Survey 08",$message,"from: *****@anblickstudios.com");
}
?>

<form action="vote.php" method="post">
<fieldset><legend>Please complete the following questions. Thanks!</legend>




<Label> <b>Most likely to...</b></Label><br />


<label>1. Be on the price is right</label><br />
<select name="priceisright">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>2. Become a millionaire</label><br />
<select name="millionaire">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>3. Loose passport in Europe</label><br />
<select name="loosepassport">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>4. Live in a van by the river</label><br />
<select name="vanriver">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>5. Cheat on Spouse</label><br />
<select name="spusecheat">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>6. Marry for money</label><br />
<select name="marrymoney">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>7. have 5 divorces before 30</label><br />
<select name="5divorces">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>8. Be prime minister of Canada</label><br />
<select name="primeminister">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>9. Teach at ECI</label><br />
<select name="teacheci">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>10. Be a 40 year old virgin</label><br />
<select name="40yearoldvirgin">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>11. Live with their parents</label><br />
<select name="livewparents">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>12. Die holding their cell phone</label><br />
<select name="dieholdingcell">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>13. Have a damaged liver before 19</label><br />
<select name="damagedliver">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>14. End up in jail</label><br />
<select name="jail">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>15. Get plastic surgery on their whole body</label><br />
<select name="plasticsurgery">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>16. Adopt children from a 3rd world</label><br />
<select name="adoptchildren">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>17. Buy their friends</label><br />
<select name="buyfriends">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>18. Memorize the Dictionary</label><br />
<select name="Dictionary">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>19. Still be at ECI in 5 years</label><br />
<select name="eci5years">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>20. Become the true Guitar Hero</label><br />
<select name="guitarhero">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>21. Join a Convent</label><br />
<select name="convent">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>22. Dance to Soulja boy at their wedding</label><br />
<select name="souljaboywedding">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>23. Win the nobel peace prize</label><br />
<select name="nobelpeace">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>24. Be the biggest tree hugger/environmentally friendly</label><br />
<select name="huggerenvironment">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label><b>Biggest</b></label><br />

<label>25. Mack </label><br />
<select name="mack">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>26. Ego</label><br />
<select name="ego">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>27. Partier</label><br />
<select name="partier">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>28. Cougar</label><br />
<select name="cougar">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>29. Awkward</label><br />
<select name="awkward">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>30. Dramatic</label><br />
<select name="dramatic">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>31. Gossip</label><br />
<select name="gossip">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>32. Cluts</label><br />
<select name="cluts">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label><b> Best</b></label><br />

<label>33. Car</label><br />
<select name="car">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>34. Style</label><br />
<select name="style">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>35. Hair</label><br />
<select name="hair">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label> <b>------------------------------------------------------------</b></label><br />

<label>36. Cutest Couple</label><br />
<select name="cutestcouple">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>37. Hottest Parents</label><br />
<select name="hottestparents">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>38. Playboy/Playgirl</label><br />
<select name="playboygirl">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>39. Kingsway Mom & Dad</label><br />
<select name="kingswaymd">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

<label>40. Funniest</label><br />
<select name="funniest>
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>

<br />

<label>41. Most athletic</label><br />
<select name="athletic">
  <option value="">Please Choose One Person</option>
  <option value="">Not Voting</option>
	<?php echo $option; ?>
</select>
<br />

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





</form>


</body>
</html>

Weird. This script is running fine on my system. Print out the message! I get,

Thank you for registering with our site! Your selection was naveen2. Sincerely, Us

. Also check if you are populating the dropdown list! Or, I think you probably selected either "Please Choose One Person" or "Not Voting". Assign some value to these options and test.

ok but how do i check? When i click the drop down menu i see all the names. That all works...

huh! Right click on your page and click on view source. Check if the value for <option value=''> is being set. Check if its not null. Then select some dropdown list values and check again. When you submit, if you get something like, array([priceisright]=>your choice,......) , then your code has no problem!

well if you mean this

<option value=>Jacob Anderson</option><option value=>Mohammed Abbas</option><option value=>Hiroaki Abe</option>

Yes they are being populated properly. I tried selecting but nothing. So maybe something is wrong with the script?

See! I told you. Values are not being assigned to the option :) ! This should have been,

<option value=Jacob Anderson>Jacob Anderson</option><option value=Mohammed Abbas>Mohammed Abbas</option><option value=Hiroaki Abe>Hiroaki Abe</option>

:)


Edit: Change $query="select user_id from grads"; to $query="select user_id,grads from grads"; It should work.

so hows that fixed? thats straight from the database? Its not importing correctly possibly


FIXED!

I hope... :P 1 second

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.