View Full Version : I made this php script, and it doesnt seem to work
labrynth_of_fire
12-10-2003, 02:50 PM
Ive tried everything i can, but the print command seems to not be working ^>_<^
<?php
$number=$_POST['number']
$name=$_POST['name'];
$email=$_POST['email'];
$comments=$_POST['comments'];
$to="sr71storm@charter.net";
$message="$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email";
if(mail($to,"Comments From Your Site",$message,"From: $email\n")) {
echo "you have chosen" $number"Thanks for your comments.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
$number="1";
if ($number =="1"){
print ("http://zultone.com/images/image1.gifyou have chosen 1st image");
}
elseif($number=="2"){
print("http://zultone.com/images/image2.gifyou have chosen 2nd image");
}
else($number=="3"){
print("http://zultone.com/images/image3.gifyou have chosen 3rd image");
}
?> see anything wrong with it?
i am using it on a form www.zultone.com/image_pick.html ,its just an idea i had to see if it would work but it doesnt seem to be working...
I want the user to type in a number, and that number brings up a certain image, and shows it to them and sends me an email about which one they picked, but its showing nothing....
cyanide
12-10-2003, 03:14 PM
Hi,
Try changing the extension of that page to .php instead of .html
php scripts won't run otherwise
labrynth_of_fire
12-10-2003, 03:27 PM
do you mean save it as a .php instead of .html ? thats what i did...but when i first uploaded it , it was in binary, will that ruin the whole thing?
labrynth_of_fire
12-10-2003, 05:35 PM
ive got another question about php, how do you make it where people can log in to your site, and make an account, and have profiles n such?
cyanide
12-10-2003, 06:56 PM
do you mean save it as a .php instead of .html ? thats what i did...but when i first uploaded it , it was in binary, will that ruin the whole thing?
Yes.
You can even just right-click and change name also.
The page won't be able to read your php script unless the php extension is there
Binary is for images, you should upload in ASCII
cyanide
12-10-2003, 06:59 PM
ive got another question about php, how do you make it where people can log in to your site, and make an account, and have profiles n such?
Well, you're talking about a whole new ball-game there.
Extensive knowledge of php and interaction with MySQL databse would be required.
You should check out some free scripts at hotscripts (http://www.hotscripts.com/PHP/Scripts_and_Programs/index.html)
Atleast it'll get you started in the right direction
labrynth_of_fire
12-10-2003, 07:11 PM
thanks, its saved as image_show.php but still isnt working, ill fiddle with it some more, and thanks for that link!!
Could I use JavaScript with that to?
redcircle
12-10-2003, 07:58 PM
It could be done in Javascript.
the first line
$number=$_POST['number']
does not have an ; at the end of the line. It's usually nice to post the error you are getting.
labrynth_of_fire
12-10-2003, 08:50 PM
thanks for pointing that out, it never said i had an error...all that happend was a blank page, and i should get an email but havent gotten any from it...
davebarnes
12-12-2003, 12:45 PM
A better (in my opinion) place to ask this question is: www.phphelp.com
adposter
12-14-2003, 01:54 AM
ive got another question about php, how do you make it where people can log in to your site, and make an account, and have profiles n such?
There's a lot of topic about creating your own login sessions but here's a basic one:
1.) create a mysql table called USERS, you can add more fields later as you wish:
create table users (
id int unsigned not null auto_increment,
userid varchar(24) not null,
password varchar(24) not null,
primary key(id));
2.) create your PHP script form. We'll call this script LOGIN.PHP
<?
// when the user click login button
if (isset($login))
{
// 1.) here is where you connect to your MYSQL db to access user id and password.
$dbname = "user_db"; // supposing you created a db user_db
@mysql_connect("0.0.0.0.0", "useruser", "passpass") or die(mysql_error());
@mysql_select_db($dbname) or die(mysql_error());
// 2.) Once connected you then run a query
$qry = "SELECT * FROM users where userid=\"$userid\" and password=\"$pass\"";
$res = mysql_query($qry) or die(mysql_errno().":".mysql_error());
$rows = mysql_num_rows($res);
if ($rows == 0)
print "ACCESS DENIED";
else {
// 3.) Once verified correct. You set the cookie by doing:
setcookie("userid", $userid, time()+3600); //1 HR
// 4.) then you can open the menu or whatever page the user wants to see...VIOLA!
print "Welcome $userid!!!!"; }
}
else
{
print "<form action=\"login.php\" method=\"post\">
User ID: <input type=\"text\"name=\"userid\" size=\"10\">
Password: <input type=\"password\"name=\"pass\" size=\"10\">
<input type=\"submit\" name=\"login\" value=\"Login\">
</form>
";
}
?>
This is a very very basic way to create a login screen, but i'm sure other members here might give you another better idea. There are lots of ways to create better ones, but because you're beginning PHP, I hope this one helps and easy to grasp.
Thanks,
Jon
labrynth_of_fire
12-14-2003, 11:24 AM
oh wow, thank you!!
adposter
12-14-2003, 01:57 PM
oh wow, thank you!!
No problemo...
mind you, there's no error correction on these, like checking to see if user has typed anything in the fields, but I guess you know how to do that, so I won't bother writing it...i'll leave that up to you for the sake of learning it...
Let me know if it worked...
Jon
computergenius
12-14-2003, 03:55 PM
print ("http://zultone.com/images/image1.gifyou have chosen 1st image");
see anything wrong with it?
Apart from the fact that the page must have the extension .php insteadof .html, you have too many quotes. It should be
print ('http://zultone.com/images/image1.gifyou have chosen 1st image');
Notice that I have changed quotes 1 and 4 to single quotes.
You could have changed quotes 2 and 3 to single quotes instead, but this would have run very slightly slower.
And I don't like the if..else. I would have used a function (as the text is so similar), or a switch statement if the text is not likely to stay so similar.
redcircle
12-14-2003, 11:01 PM
thanks for pointing that out, it never said i had an error...all that happend was a blank page, and i should get an email but havent gotten any from it...
Do you have error's truned on in the php.ini ?
1.) create a mysql table called USERS, you can add more fields later as you wish:
create table users (
id int unsigned not null auto_increment,
userid varchar(24) not null,
password varchar(24) not null,
primary key(id));
2.) create your PHP script form. We'll call this script LOGIN.PHP
<?
// when the user click login button
if (isset($login))
{
// 1.) here is where you connect to your MYSQL db to access user id and password.
$dbname = "user_db"; // supposing you created a db user_db
@mysql_connect("0.0.0.0.0", "useruser", "passpass") or die(mysql_error());
@mysql_select_db($dbname) or die(mysql_error());
// 2.) Once connected you then run a query
$qry = "SELECT * FROM users where userid=\"$userid\" and password=\"$pass\"";
$res = mysql_query($qry) or die(mysql_errno().":".mysql_error());
$rows = mysql_num_rows($res);
if ($rows == 0)
print "ACCESS DENIED";
else {
// 3.) Once verified correct. You set the cookie by doing:
setcookie("userid", $userid, time()+3600); //1 HR
// 4.) then you can open the menu or whatever page the user wants to see...VIOLA!
print "Welcome $userid!!!!"; }
}
else
{
print "<form action=\"login.php\" method=\"post\">
User ID: <input type=\"text\"name=\"userid\" size=\"10\">
Password: <input type=\"password\"name=\"pass\" size=\"10\">
<input type=\"submit\" name=\"login\" value=\"Login\">
</form>
";
}
?>
Somewhat correct but your method does not take into account that PHP default install has register_globals turned off. Posted variables should be $_POST['userid'] and $_PASS['pass']; For some optimizations I would first use single quotes opposed to double quotes. PHP looks for variables within the double quotes. using single quotes it doesn't. I would also add a limit at the end of the SQL statement so the script will stop after it finds the one result. Also because we are not using any information in the sql result we only need to select the primary index field from the record. These are just speed optimizations not necessary and really wouldn't have an effect on a small site. But what if there were 10,000 users. If it found 1 record after searching through the first 10 it doesn't need to look any more it has a valid result. Without the limit 1 with will continue on through the rest of the 10,000 which is wasting CPU. Best to start off learning the best way. Make it a habit to optimize not just get the job done.
$qry = 'SELECT id FROM users where userid="'.$_POST['userid'].'" and password="'.$_POST['pass'].'" limit 1';
$res = mysql_query($qry) or die(mysql_errno().":".mysql_error());
$rows = mysql_num_rows($res);
adposter
12-15-2003, 12:27 AM
Do you have error's truned on in the php.ini ?
My installation which is default as far as i'm concerned is okay with all errors turned on. Using PHP 4.3+. With regards to those minor details, I would've put them but because I was giving labyrinth_of_fire a basic script which he requested, he got what he asked.
I've tested the script and it worked on my system. Those minor details you've explained are for later use. Can't spoon feed. Thanks for the added info though. As for him since he's learning the basic stuff, it's all here. What he just needs to grasp is to get the basics of PHP.
Nevertheless, the comment is appreciated. Why complicate, when you can make it simple with results. And later on, optimize it. As I said to him earlier, there are better ways to do it, but he has the basic idea and he can learn from it.
Jon