Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
bobafifi

Script 8.6: A Registration Script

Recommended Posts

I have a table that has URLs for some records in the fourth column and then records that don't have URLs. To distinguish the two, I use a field called 'publish' (set to 1 for records with, 2 for records without).

 

// Fetch and print all the records.

$bg = '#eeeeee'; // Set the background color.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.

echo '<tr bgcolor="' . $bg . '">

<td align="left"><a href="' . $row['field4'] . '">' . $row['field1'] . '</a></td>

<td align="left">' . $row['field2'] . '</td>

<td align="left">' . $row['field3'] . '</td>

<td align="left"><a href="' . $row['field5'] . '">' . "Link2" . '</a></td>

 

</tr>

';

}

 

echo '</table>';

 

//

 

How do I get the records without URLs to not print this

<td align="left"><a href="' . $row['field5'] . '">' . "Link2" . '</a></td>

 

but instead, print this?

<td align="left"></td>

 

 

Thanks!

-Bob

Share this post


Link to post
Share on other sites

Assuming "$row['field5']" has no value if there's no URL then you could just simple to a "if" clause

 

if (($row['field5'] == "") || ($row['field5'] == "http://")) { // obviously change it to what suits you
 echo '<td align="left"></td>';
} else {
 echo '<td align="left"><a href="' . $row['field5'] . '">' . "Link2" . '</a></td>';
}

or something similar.

 

I haven't tested the code but you might need {} around {$row['field5']}

  • Upvote 1

Share this post


Link to post
Share on other sites

hmmm... still getting parse errors

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/example/public_html/example.php on line 122

 

Thanks anyway Craig,

-Bob

Share this post


Link to post
Share on other sites

Hi Jonathon,

I'm using Larry's registration script 8.6

 

This is the section of the code that prints the table

 

// Fetch and print all the records.

$bg = '#eeeeee'; // Set the background color.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.

echo '<tr bgcolor="' . $bg . '">

<td align="left"><a href="' . $row['field4'] . '">' . $row['field1'] . '</a></td>

<td align="left">' . $row['field2'] . '</td>

<td align="left">' . $row['field3'] . '</td>

<td align="left"><a href="' . $row['field5'] . '">' . "Link2" . '</a></td>

 

</tr>

';

}

 

echo '</table>';

Share this post


Link to post
Share on other sites

I only get the parse error messages when I try to cut into the while loop, otherwise it works great (thanks Larry!).

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/example/public_html/example.php on line 122

 

I noticed Larry addressed a similar post a few years ago here

http://www.larryullm...39568#msg-39568

 

"What you need to do is have the while loop create TDs--one for each iteration--instead of TRs. The problem is that the loop also has to create new TRs as appropriate and close old TRS as appropriate. And complete the last TR if there's an odd number of records. I just use a flag variable for this purpose. This is untested but may work for you:

$n = 0;

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

if ($n == 0) {

echo '<tr>';

}

echo "<td><img src=\"show_image.php?image={$row['thumbnail']}\"></img></td>

<td width=\"20px\"></td>\n";

$n++;

 

if ($n == 2) {

echo '</tr>';

$n = 0;

}

 

}

 

if ($n == 1) {

echo '<td></td>

<td width=\"20px\"></td>

</tr>';

}

Also, there is no closing IMG tag.

 

Best Wishes,

Larry

 

Writer/Web Developer/Instructor

Forum Moderator

Share this post


Link to post
Share on other sites

Got it! Here's how I did it for anybody looking to do the same:

 

// Fetch and print all the records.

$bg = '#eeeeee'; // Set the background color.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

if ($row['publish']=="1")

{

$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.

echo '<tr bgcolor="' . $bg . '">

<td align="left"><a href="' . $row['field4'] . '">' . $row['field1'] . '</a></td>

<td align="left">' . $row['field2'] . '</td>

<td align="left">' . $row['field3'] . '</td>

<td align="left"><a href="' . $row['field5'] . '">' . "Link2" . '</a></td>

 

</tr>

';

}

else

{

$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.

echo '<tr bgcolor="' . $bg . '">

<td align="left"><a href="' . $row['field4'] . '">' . $row['field1'] . '</a></td>

<td align="left">' . $row['field2'] . '</td>

<td align="left">' . $row['field3'] . '</td>

<td align="left"></td>

 

</tr>

';

}

}

 

echo '</table>';

  • Upvote 1

Share this post


Link to post
Share on other sites

larry i used the script in your book title PHP 6 and MySQL 5 for Dynamic web sites chapter 8, the registration script did not work for me rather i'm getting ' could not connect to MySQL: Access denied for user 'username'@'localhost' (using password: YES) ' and i try debugging it by making sure that MySQL server is running by starting my MySQL and it started i even created another database with it name user but to no avial but i was able to login to PHPMYADMIN . i'm using MYSQL version 5.5.17 apache 2.2.21 PHP 5.2.17 phpmyadmin 3.4.7.1 OS window 7 this is the code for mysqli_connect

<?php

// this file contains the database access information.

DEFINE ('DB_USER', 'username');

DEFINE ('DB_PASSWORD', 'password');

DEFINE ('DB_HOST', 'localhost');

DEFINE ('DB_NAME', 'sitename');

// make the connection

$bdc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('could not connect to MySQL: ' . mysqli_connect_error() );

?>

 

this is the code for the registration

 

<?php # register.php

$page_title = 'Register';

include ('includes/header.html');

// check if the form has been submitted:

if (isset($_POST['submitted']))

{

$errors = array(); // initialize an error array.

 

// check for a first name:

if (empty($_POST['first_name']))

{$errors[] = 'you forget to enter your first name.';

}

else

{

$fn = trim($_POST['first_name']);

}

 

// check for a last name:

if (empty($_POST['last_name']))

{

$errors[] = 'You forgot to enter your last name.';

}

else

{

$ln = trim($_POST['last_name']);

}

 

// check for the email address :

 

if (empty($_POST['email']))

{

$errors[] = 'You forgot to enter your email address.';

}

else

{

$e = trim($_POST['email']);

}

 

// check for a password and match agaist the confirmed password:

 

if (!empty($_POST['pass1']))

{

if ($_POST['pass1'] != $_POST['pass2'])

{

$errors[] = 'Your password did not match the confirmed password.';

}

else

{

$p = trim($_POST['pass1']);

}

}

else

{

$errors[] = 'You forgot to enter your password.';

}

if (empty($errors))

{

// if everything is ok

 

// register the user in the database

 

require_once

('includes/mysqli_connect.php'); //connect to the database.

 

// make the query:

 

$q = "INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )";

$r = @mysqli_query ($dbc, $q); //run the query

 

if ($r)

{ //if it ran ok

// print a message:

 

echo '<h1>Thank you!</h1>

<p>You are now registered. go to your mail to confirm the registration then you can be able to log in!</p><p><br /></p>';

}

else

{ // if it did not run ok

 

 

// public message:

 

echo '<h1>System Error</h1>

<p class="error"> You could not be registered due to a system error. We apologize for any inconvinience.</p>';

 

// debugging message:

 

echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

} // end of if ($r) IF

 

mysqli_close($dbc); // close the database connection.

 

// include the footer and quit the script:

 

include ('includes/footer.html');

exit();

}

else

{ // report the error

echo '<h1>error!</h1>

<p class="error">The following error(s) occured:<br />';

foreach ($errors as $msg)

{ //print each error

 

echo " $msg<br />\n";

}

echo '</p><p>Please try again.</p><p><br /></p>';

} // end of if (empty ($errors)) IF .

} // End of the main submit conditonal.

?>

<h1>Register</h1>

<form action="register.php" method="post">

<p>First name: <input type="text"

name="first_name" size="15" maxlength="20" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>

<p>Last Name: <input type="text" name="last_name" size="15" maxlength="40" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>

<p>Email Address: <input type="text" name="email" size="20" maxlength="80" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /> </p>

<p>Password:

<input type="password" name="pass1" size="10" maxlength="20" /></p>

<p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" /></p>

<p><input type="submit" name="submit" value="Register" /></p>

<input type="hidden" name="submitted" value="TRUE" />

</form>

<?php

include ('includes/footer.html');

?>

 

please any help will be appreciated

Share this post


Link to post
Share on other sites

Thanks Jonathon for helping me. i have fixed that but yet still getting the same error report when i submit the registration form:

 

could not connect to MySQL: Access denied for user 'username'@'localhost' (using password: YES). please help me out am stuck

Share this post


Link to post
Share on other sites

Jonathon please i have created a login / logout page and i'm get the same error , like my MySQL is not working fine. shall this is the error message this time:

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'user'@'localhost' (using password: NO) in D:\Webpages\login_check.php on line 8

can't connect

 

this is code for the INDEX.PHP:

 

<html><head><title>Login</title></head><body>

<form method="post" action="check_login.php"><br />Username: <input type="text" name="username"><br />Password: <input type="password" name="password"><br /><input type="submit" name="submit" value="login"></form></body></html>

 

CHECK_LOGIN.PHP:

 

<?php

session_start();

$user="root"; // my server user name

$host="localhost"; // server host

$password="kingso6"; // this is password for my server

$database="site"; // database for server

$usertable="users"; // table for database

mysql_connect($host, $user, $password) or die ('can not connet');

mysql_select_db($database) or die (mysql_error());

$myusername = $_POST['username'];

$mypassword = $_POST['password'];

$sql = "SELECT * FROM {$usertable} WHERE username='{$myusername}' AND password='{$mypassword}'";

$result = mysql_query($sql);

$count = mysql_num_rows($result);

if($count == 1)

{

$_session["username"] = $myusername;

$_session["password"] = $mypassword;

 

header ("location:login_success.php"); // look like culy bracket

}

else

{

echo 'Wrong Username OR Password';

}

 

LOGIN_CHECK.PHP:

 

<?php

$user="user";

$host="localhost";

$password="";

$database="site";

$usertable="users";

mysql_connect($host, $user, $password) or die ("can't connect");

mysql_select_db($database) or die (mysql_error());

$myusername = $_session['username'];

$mypassword = $_session['password'];

$sql = "SELECT * FROM {$usertable} WHERE username='{$myusername}' AND password='{$mypassword}'";

$result = mysql_query($sql);

$count = mysql_num_rows($result);

if ($count == 0)

{

header("location:index.php");

}

 

LOGIN_SUCCESS.PHP:

 

<?php

require_once("login_check.php");

?>

<html>

<head>

<title> Login Success</title>

</head>

<body>

Welcome back <?php echo $myusername; ?> <br />

<a $set="logout.php">Logout</a>

</body>

</html>

 

THE DATABASE NAME IS 'site' while the database table is 'users' username for the table created is 'admin' password 'kingso'

 

PLEASE HELP AND FOUND OUT WHAT IS WRONG WITH MY SERVER

Share this post


Link to post
Share on other sites


<?php
$link = mysql_connect('localhost', 'root', 'kingso6');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
?>

 

Just try this in a file on its own

 

What does it return?

 

Why do your scripts have different passwords?

Share this post


Link to post
Share on other sites

Hello,

How can a create admin panel. I have linked chapter 8 script admin to chapter 16 user. But there is a problem in the admin side. Any user can login in the admin and view and edit user. I want only the admin to be able to access the admin panel with his username and password. Hope to hear from you soon.

Share this post


Link to post
Share on other sites

You'd want to create a way to identify user levels. Normally you'd add a column to the database and store the user level with the other info. Then, upon login, you retrieve this info, too, and store it in a session. Then the admin panel would check the stored user level and allow/reject users accordingly.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...