ok fixed it,
Now i have the passwords left...
But i never really understood how i will run the password script you gave me so it inserts the password into each users password field?
So basically i have the table Grads, then i have for each user_id the password field.
hopefully you can give me a hand with this
Never saw how to do this in my book yet.
EDIT: and this is what i fixed
$option.="<option value=".$row['user_id'].">".$row['user_id']."</option>";
both rows werent showing user_id thats y it wasnt querying it like u said so....
Whats that other thing u told me to change?
I also cant get this welcome script to work not that it really matters but i think its showing me that something isn't right...
and 1 last thing dealing with the security. How am i going to stop people from bypassing the login script and not just going to the survey? or you think cus its only being used by high school students and no one will know the structure it should be fine?
EDIT EDIT:
Also when i do the submit it is only taking the first name for some bizarre reason
Here is the top of the code incase u want it
<?php
print_r($_POST);
$conn=mysql_connect("********************","*******","**********");
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['user_id'].">".$row['user_id']."</option>";
}
if(isset($_POST['submit'])) {
$query="UPDATE TABLE SET VOTE_STATUS = 1 WHERE USER_ID ='".$_POST['user_id']."'";
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['agent'])) {
echo ", {$_SESSION['agent']}!";
}
echo '</h2>';
// 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("****l@anblickstudios.com","Grad Survey 08",$message,"from: *****@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");
}
?>
and this was the exact welcome code....
<?php
// Welcome the user
echo '<h2>Welcome';
if (isset($_SESSION['agent'])) {
echo ", {$_SESSION['agent']}!";
}
echo '</h2>';
Earlier in your query, you were using $row as value. So I asked you to change the query. Since you have changed the value to $row it should work fine. Its passing first name because (I think) you are storing first name in user_id column. Because, when I asked you to show what's in <option value=''>..</option>, it showed me the first name.
<?php
// Welcome the user
echo '<h2>Welcome';
if (isset($_SESSION['agent'])) {
echo ", {$_SESSION['agent']}!";
}
echo '</h2>';
That doesnt work since you are not setting $_SESSION anywhere. First check if $_SESSION is not set. If its not set, set agent name. Then this will work.
And about the password generation. Well, the link that I gave you was a function. You just have to copy that function in your script and call that function whenever you want to generate a new password. And this is how you call that function.
$pass=generatePassword($length); //length of the password.. by default it takes 8 if you don't specify any.
so i would be using that function in the sql?
I just don't get how i am doing this...
Am i generating say 500 passwords and then running a mysql script to insert them all into the database?
$query="select user_id from table";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
$x=generatePassword($length);
$userid=$row['user_id'];
$sql="update table set password='$x' where user_id='$user_id'";
mysql_query($sql);
}
:) Simple.
Ok here is what i have now...
<?php
print_r($_POST);
$conn=mysql_connect("i**********","*************","***********");
mysql_select_db("db33717_gradsurvey");
$query="select password from grads";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
$x=generatePassword($length);
$userid=$row['user_id'];
$sql="update table set password='$x' where user_id='$user_id'";
mysql_query($sql);
}
function generatePassword ($length = 8)
{
// start with a blank password
$password = "";
// define possible characters
$possible = "0123456789bcdfghjkmnpqrstvwxyz";
// set up a counter
$i = 0;
// add random characters to $password until $length is reached
while ($i < $length) {
// pick a random character from the possible ones
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
// we don't want this character if it's already in the password
if (!strstr($password, $char)) {
$password .= $char;
$i++;
}
}
$pass=generatePassword($length); //length of the password.. by default it takes 8 if you don't specify any.
// done!
return $password;
}
?>
So now this is what i get... Array ( )
Thats all and nothing gets into the database.
Any ideas?
remove print_r($_POST); and then debug yourself. I think I have helped you enough and you are in a state where you can solve your bugs. :)
oh removing the print will allow me to view bugs? Ok il give it a shot.
Nope. print_r($_POST); is for debugging. I asked you to remove it because, once your code is up and running, you dont need those debug information. But anyway, unless you dont debug your code yourself, you wont learn anything. Spoon feeding is not good either. So good luck! :)
I guess you dont know how a function works ! Am I correct ? The function call should be outside the function and not within itself. It will enter an infinite loop. $pass=generatePassword($length);
should be outside. And it should be something like $x=generatePassword(8);
I hope here on, you will debug on your own.
well no i dont ok thanks il give it a shot now
<?php
print_r($_POST);
ini_set('display_errors','1');
$conn=mysql_connect("********************","*******","**************") or die(mysql_error());
mysql_select_db("db33717_gradsurvey") or die(mysql_error());
$query=("SELECT * FROM grads") or die(mysql_error());
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)){
$userid=$row['user_id'];
$password=$row['password'];
$sql="update grads set password"or die(mysql_error());
mysql_query($sql) or die(mysql_error());
}
function generatePassword ($length = 8)
{
// start with a blank password
$password = "";
// define possible characters
$possible = "0123456789bcdfghjkmnpqrstvwxyz";
// set up a counter
$i = 0;
// add random characters to $password until $length is reached
while ($i < $length) {
// pick a random character from the possible ones
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
// we don't want this character if it's already in the password
if (!strstr($password, $char)) {
$password .= $char;
$i++;
}
}
// done!
return $password;
}
$password=generatePassword(8);
?>
<?
echo $user_id;
echo $password;
?>
I tried for almost an hour and a half and i still cant get it
I got it down to this error now
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Mind helping me? I think i just cant get this line correct
$sql="update grads set password"or die(mysql_error());
Thanks, Dani.
hmm.. Ok..
<?php
$conn=mysql_connect("********************","*******","**************") or die(mysql_error());
mysql_select_db("db33717_gradsurvey") or die(mysql_error());
function generatePassword ($length = 8)
{
// start with a blank password
$password = "";
// define possible characters
$possible = "0123456789bcdfghjkmnpqrstvwxyz";
// set up a counter
$i = 0;
// add random characters to $password until $length is reached
while ($i < $length) {
// pick a random character from the possible ones
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
// we don't want this character if it's already in the password
if (!strstr($password, $char)) {
$password .= $char;
$i++;
}
}
// done!
return $password;
}
$query=("SELECT * FROM grads") or die(mysql_error());
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)){
$userid=$row['user_id'];
$password=generatePassword(8);
$sql="update grads set password='$password' where user_id='$userid'";
mysql_query($sql) or die(mysql_error());
echo $user_id." : ".$password; //remove this if you dont want to see the user id and password on the screen
}
?>
This will work.
Just i dont see the users. Thats sorta really important as i changed the script to use SHA-1 encryption so it posts the un encrypted version and i need that output.
Why wont it show the users? Looks fine to me...
nevermind i got it, ended up being on the echo statement it had to be userid instead of user_id
Thanks a million
:) You are welcome!
1 more thing i never got working was that welcome script. How do i save their name from the login script and pass it to the vote page? I now realize i need that as i need to know the person who's voting...
I tried the session but i think i uses the wrong format i am not sure
When the user logs in, start the session, add his name to a session variable.
Eg.
<?php //page1.php
session_start();
$_SESSION['name']="testuser"; //user name
?>
<?php //page2.php
session_start();
echo $_SESSION['name']; //prints testuser
?>
ya thanks just learned that from this book.
Thanks again.
P.S Seems i am learning :P
Great! :) Read more, Learn more!
the only thing i cant get to work is the vote_status any clues?
if(isset($_POST['submit'])) {
$query="UPDATE grads SET vote_status = 1 WHERE USER_ID ='".$_POST['user_id']."'";
mysql_query($query);
}
Thats the code and then i also want to know what do i do if i want to verify if they already voted? Because i really don't want people changing their answers once they do it once... If you know what i mean...
have another column in the table. call it "voted" or something, with default value 0. Whenever a user votes, while updating the column vote_status, also update the column "voted" by setting to 1. So, if it is 0, then that particular user has not voted. If its 1, then he has already voted.
And I don't know what's wrong with your query. Looks fine to me.
hmm i am already doing what u said.
The column is vote_status and its all set to 0
But when i test the system using my login it doesn't change to 1 yet it does e-mail me the info perfectly fine...
print the query. Check if $_POST has any value. You can you die function to check if your query is working fine! mysql_query($query) or die(mysql_error());
user_id has a value as i am using the Welcome script and its working perfectlly.
The die command did nothing the script still ran correctly
I can't think of anything else. It should work then.
hmm let me post my script maybe i did something wrong and we are just missing it here...
<?php
session_start(); // Start the session
print_r($_POST);
$conn=mysql_connect("*********************","********************","*******") or die(mysql_error());
mysql_select_db("************") or die(mysql_error());
$query="select user_id, vote_status from grads" or die(mysql_error());
$result=mysql_query($query) or die(mysql_error());
$option="";
while($row=mysql_fetch_array($result)) {
$option.="<option value=".$row['user_id'].">".$row['user_id']."</option>";
}
if(isset($_POST['submit'])) {
$query="UPDATE grads SET vote_status = 1 WHERE USER_ID ='".$_POST['user_id']."'" or die(mysql_error());
mysql_query($query) or die(mysql_error());
}
?>
<!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>';
// 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'];
$p=$_POST['eci5years'];
$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 = "
Name: {$_SESSION ['user_id']}
Price is right: $a
Become a millionaire: $b
Loose Passport: $c
Live in a van: $d
Cheat on Spouse: $e
Marry For money: $f
Divorces: $g
Prime Minister: $h
Teach at ECI: $i
40 year old Virgin: $j
Live with their parents: $k
Die holding their cell phone: $l
Damaged Liver before 19: $m
End up in jail: $n
Plastic Surgery: $q
Adopt Children: $r
Buy their friends: $s
Memorize the Dictionary: $t
Eci in 5 years: $p
Guitar Hero: $u
Convent: $v
Soulja boy at wedding: $w
Nobel Peace Prize: $x
Tree Hugger/ Environmentally friendly: $y
Biggest Mack: $z
Biggest Partier: $aa
Biggest Partier: $ab
Biggest Cougar: $ac
Most Awkward: $ad
Most Dramatic: $ae
Biggest Gossip: $af
Biggest Cluts: $ag
Best Car: $ah
Best Style: $ai
Best Hair: $aj
Cutest Couple: $ak
Hottest Parents: $al
Playboy/Playgirl: $am
Kingsway Mom and Dad: $an
Funniest: $aq
Most ATheltic: $ar";
mail("dani@anblickstudios.com","Grad Survey 08",$message,"from: dani@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>
$query="UPDATE grads SET vote_status = 1 WHERE USER_ID ='".$_POST['user_id']."'" or die(mysql_error());
You should have "die" function while using mysql functions and not in query itself.
oh so the die wont do anything there ok...
Yep.. well, you use die to print out the message on failure. mysql_query($query) or die("your own message...");
This will execute your query. If it encounters any error, it will stop the terminate the execution of the script and print the message of die function.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.