Jump to content

My echos are not working


WilliamNova
Go to solution Solved by mac_gyver,

Recommended Posts

I have a few issues and I'm starting to think maybe my submit button isn't working properly. Anyways, here goes.

 

I'm trying to make my registration form submit to my database. I wanted to test the echos, such as when the passwords don't match, you see "Your passwords don't match!"

So I put in the wrong password purposely, click submit, and it just erases everything in the form. I even coded it to make sure that if something wrong is entered it's only supposed to wipe the password fields, not everything. The code for that is below.

<form action="#" method="post">
		<input type="text" size="25" name="fname" placeholder="First Name" value="<? echo $fn; ?>">
		<input type="text" size="25" name="lname" placeholder="Last Name" value="<? echo $ln; ?>">
		<input type="text" size="25" name="username" placeholder="Username" value="<? echo $un; ?>">
		<input type="text" size="25" name="email" placeholder="Email" value="<? echo $em; ?>">
		<input type="text" size="25" name="email2" placeholder="Repeat Email" value="<? echo $em2; ?>">
		<input type="password" size="25" name="password" placeholder="Password">
		<input type="password" size="25" name="password2" placeholder="Repeat Password"><br />
		<input type="submit" name="reg" placeholder="Sign up!">
		</form>

As for the echos, this is the entire code for that section.

<?
$reg = @$_POST['reg'];
//declaring variables to prevent errors
$fn = ""; //First Name
$ln = ""; //Last Name
$un = ""; //Username
$em = ""; //Email
$em2 = ""; //Email 2
$pswd = ""; //Password
$pswd2 = ""; //Password 2
$d = ""; //Sign up date
$u_check = ""; //Check if username exists
//registration form
$fn = strip_tags(@$_POST['fname']);
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$em2 = strip_tags(@$_POST['email2']);
$pswd = strip_tags(@$_POST['password']);
$pswd2 = strip_tags(@$_POST['password2']);
$d = date("Y-m-d"); //Year - Month - Day

if ($reg) {
if ($em==$em2) {
// check if user already exists
$u_check = mysql_query("SELECT username FROM users WHERE username='$un'");
// count the amount of rows where username = $un
$check = mysql_num_rows($u_check);
if ($check == 0) {
// check all of the fields have been filled
if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2) {
// check that passwords match
if ($pswd==$pswd2) {
// check the maximum length of username, first, and last name does not exceed 25 characters
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
echo "The maximum limit for username, first, and last name is 25 characters!";
}
else
{
// check the length of password does not exceed 30 characters and is no less than 5 characters
if (strlen($pswd)>30||strlen($pswd)>5) {
echo "Your password must be between 5 and 30 charcters long!";
}
else
{
// encrypt password and password 2 using md5 before sending to database
$pswd = md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");
die("<h2>Welcome to Music IN Demand</h2> Please login to get started!");
}
}
}
else {
echo "Your passwords do not match!!";
}
}
else {
echo "Please Fill in all required fields.";
}
}
else {
echo "Sorry, that username is not available.";
}
}
else {
echo "Your email does not match!";
}
}
?>

I've looked at this a dozen times, I really hope a fresh pair of eyes can see what the hell it is I'm doing wrong.

Link to comment
Share on other sites

Actually, once I put your script up on my server and remove the error suppressors, it works fine.

Aside from the values you have echo'd with php on the form. I suppose you have more to the file that has the form on it though.

Edited by InoBB
Link to comment
Share on other sites

You can't do that with those else statements.  You can only have 1 else statement per if statement.  Put all those echos in a single else statment and watch it work.  You will obviously want to make if statements for your echos though so if email is wrong then echo email is wrong blah blah blah.  Just don't use more than one else per if.  You can also use elseif

Link to comment
Share on other sites

Okay, I did as Ronnie suggested, but still nothing.

 

So I got fed up with it, deleted it and restarted. And to test it I did...

<?
$reg = $_POST['reg'];
// declaring variables to prevent errors
$fn = ""; //First Name
$ln = ""; //Last Name
$un = ""; //Username
$em = ""; //Email
$em2 = ""; //Email 2
$pswd = ""; //Password
$pswd2 = "";//Password 2
$d = "";// Sign up date
$u_check = ""; //Check if username exists
//Registration form
$fn = strip_tags($_POST['fname']);

if ($reg) {
echo "$fn";
}
?>

So, I only have ONE echo. When I fill out the First Name field, it should echo once I click submit with whatever I typed in that field. Except it still doesn't.

Link to comment
Share on other sites

try this:

 

if (isset($_POST['reg'])) {
    if (isset($_POST['fname'])) {
        $fn = strip_tags($_POST['fname']);
        echo $fn;
    } elseif (empty($_POST['fname'])) {
        echo "Please fill out all form fields.";
    }
}
Edited by InoBB
Link to comment
Share on other sites

Hey buddy.  Since you started over, I can see an error in your echo.

echo "$fn";

When you want to write something specific, it is a string.  You put that in quotes.  example:  echo"Ronnie is awesome";

When you are using a variable, you don't put it in quotes.

$fn="Ronnie is awesome";

echo $fn;

You shouldn't have started over.

Link to comment
Share on other sites

Hey buddy.  Since you started over, I can see an error in your echo.

echo "$fn";

When you want to write something specific, it is a string.  You put that in quotes.  example:  echo"Ronnie is awesome";

When you are using a variable, you don't put it in quotes.

$fn="Ronnie is awesome";

echo $fn;

You shouldn't have started over.

 

While that is entirely true, it's not a "bug" in his code because he used double quotes which interpolate variables. So echo "$fn"; will WORK, it's just not RIGHT.

Link to comment
Share on other sites

I'm not keeping the echo "$fn";

It was a test to see if I put, say, William in the First Name field (or fn field as I named it) and pressed the submit button it would echo "William" (or really anything typed into the first name field. I didn't want anything specific.

 

Ideally though I don't want anything typed into the fields to echo. I want it to echo when the password is wrong "Sorry, that password is incorrect." When the username is too short "Sorry, your username must be between 5 and 30 characters." And/or when the Username is taken "Sorry, that username isn't available."

 

However, in the past few days, I have upgraded my server. It was quite aged, thought maybe an update would do some good. So now I have the latest update and still nothing... Nothing is echoing at all. Nor is anything even submitting to the database.

Link to comment
Share on other sites

here are somethings a little more basic to try to find out why it isn't working -

 

1) what URL are you entering in your browser's address bar? it should be something like http://localhost/your_file_name.php

 

2) what is the filename? it must end in .php (unless you have configured your web server to run other extensions as php files.)

 

3) do you have php installed? does a simple script with <?php echo time(); ?> work?

 

4) always use full opening php tags <?php

 

5) your form and your form processing code are apparently all in one file. you may have something that you are not showing us in that file that is preventing the form from being valid or preventing the form processing code from running. post the entire file as is, less any database credentials. out of context snippets of code don't help when the problem is that the whole PAGE doesn't work as expected.

Link to comment
Share on other sites

I believe the code is perfect. I sent it to my friend in Russia via email. He said it was all good. Got all the appropriate messages.

 

But then I saw your reply about the url and I feel dumb. So I tried http://localhost/(filename, the php one) but I got a 404. I tried using my Apache port (that's the way I access phpmyadmin) so, http://localhost:8080/filename but got a different 404 with more info. clciked a link that was available and it actually helped me find the page.

 

But the php was visible, like all the php code was at the top of the page and the registration form had some php in it, which I'm assuming means php is not installed.

 

Oh, and fixed the minimum length for the password, thanks for pointing that out.

Edited by WilliamNova
Link to comment
Share on other sites

I edited my file and used full tags <?php

But I get

 

Parse error: syntax error, unexpected ';' in C:\xampp-portable\htdocs\mid\index.php on line 1

 

Not sure if it matters but line 1 is my header code from my inc file

 

<?php include("inc/incfiles/headerinc.php"; ?>

Edited by WilliamNova
Link to comment
Share on other sites

I removed the full php tag in line 1 and that fixed that.

Then found another error in line 34, fixed it. But now I have a different error in line 29. Here's line 29 as-is with the error

$check = mysql_num_rows($u_check);

Here's the error message.

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp-portable\htdocs\mid\index.php on line 29

 

 

EDIT: I tried encasing ($u_check) with back ticks: (`$u_check`) but all it did was change the word BOOLEAN to null.

Edited by WilliamNova
Link to comment
Share on other sites

  • Solution

re: your edit in post #18 with the line of code and "I removed the full php tag in line 1 and that fixed that." in post #20.

 

the way to fix an error is to find what's causing it and make it right. by changing the php tag back to a non-working php tag, all you did was to effectively remove the line of php code because php no longer sees it as being php code.

 

what's wrong with line 1 of your file is very likely what is causing the latest error. your include statement isn't working because you have a syntax error in the line of code. you either need to add a missing ) to match the ( you do have or since the include statement isn't actually a function, you can remove the (

Link to comment
Share on other sites

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.