Revisiting (and Redirecting) the Create User Script PHP Help

The changes you made in the previous section are great, but there’s more to do. For example you have a new bio column, but no place to enter that information when users sign up. You need credte yserpro to deal with that information when it comes in from your signup form. And then there’s getting a user from the signup form to and passing along the newly created user’s ID, as well. It seems like a lot, but with what you know, this change will be a breeze.

Updating Your User Signup Form

The first change-the bio-is one of the easiest. Open your page and add a new form field so that your users can enter a biography. Leave plenty of space: Have you seen how much information people write about themselves on Facebook these days? Here’s the updated version

<html>
<head>
<link href=” /css/phpMM.css” rel=”stylesheet” type=”text/css” />
</head>
<body>
<div id=”header”><h1>PHP & MySOL: The Missing Manual</h1></div>
<div id=”example”>User Signup</div>
<div id=”content”>
<h1>Join the Missing Manual (Digital) Social Club</h1>
<p>Please enter your online connections below:</p>
dorm action=”scripts/create_user.php” method=”POST”>
<dieldset>
<label for=”first_name”>First Name:</label>
<input type=”text” name=”first_name” size=”20″ /><br />
<label for=”last_name”>Last Name:</label>
<input type=”text” name=”last_name” size=”20″ /><br />
<label for=..email..>E-Mail Address:</label>
<input type=”text” name=”email” size=”SO” /><br />
<label for=”facebook_url”>Facebook URL:</label>
<input type=”text” name=”facebook_url” size=”SO” /><br />
<label for=”twitter_handle”>Twitter Handle:</label>
<input type=”text” name=”twitter_handle” size=”20″ /><br />
<label for=”bio”>Bio:</label>
<textarea name=”bio” cols=”40″ rows=”10″></textarea>
</fieldset>
<br />
<fieldset class=”center”>
<input type=”submit” value=”Join the Club” />
<input type=”reset” value=”Clear and Restart” />
</fieldset>
</form>
</div>
<div id=”footer”></div>
</body>
</html>

While you’re at it. you might as well let your users pick an image for their profile. You won’t write any code in c: to handle this, but it’s Coming soon, and you’ll save a trip back to I when you’re ready to add images.

<body>
<div id=”header”><h1>PHP & MySQL: The Missing Manual</h1></div>
<div id=”example”>User Signup</div>
<div id=”content”>
<h1>Join the Missing Manual (Digital) Social Club</h1>
<p>Please enter your online connections below:</p>
dorm action=”scripts/create_user.php” method=”POST”
enctype=”multipart/form-data”>
dieldset>
<label for=”first_name”>First Name:</label>
<input type=”text” name=”first_name” size=”20″ /><br />
<label for=”last_name”>Last Name:</label>
<input type=”text” name=”last_name” size=”20″ /><br />
<label for=”email”>E-Mail Address:</label>
<input type=”text” name=”email” size=”sO” /><br />
<label for=”facebook_url”>Facebook URL:</label>
<input type=”text” name=”facebook_url” size=”50″ /><br />
<label for=”twitter_handle”>Twitter Handle:</label>
<input type=”text” name=”twitter_handle” size=”20″ /><br />
<label for=”user_pic”>Upload a picture:</label>
<input type=”file” name=”user_pic” size=”30″ />
<label for=”bio”>Bio:</label>
<textarea name=”bio” cols=”40″ rows=”lO”></textarea>
</fieldset>
<!– Buttons for submission and resetting the form –>
</body>
</html>

You need to change the form tag a bit, because now you’re actually uploading a file to a server from your user’s machine. To do this, add the new enc type attribute with the value “multipart/form-data”. That alerts any scripts receiving this form’s input to expect more than just the values in the input fields, like the name of the file. A form like this also submits the data associated with those fields: in this case, that’s the actual file that the user selects to upload

Then, you add a new input of type “file” which lets the user browse his hard drive, select a file, and upload that file. By the way, this code is almost boilerplate. Every time you give your users the opportunity to upload a file, this is the set of changes you’ll need to make

Save your changes here and then open your form in a browser. You should see your updated form, similar to Figure 7-12.

Join the Missing Manual (Digital) Social Club

Join the Missing Manual (Digital) Social Club

If you fill in some values now, without changes to creete y.ser.php, you’ll create a new user without a bio

The header function sends a raw hypertext transfer protocol (HTTP) header to your user’s browser. (HTTP is the language of web traffic. It’s the same  you put at the beginning of most of your URLs in your browser’s address bar.) This function directly manipulates the location of your user’s page

These are simple rules but they’re also really important ones. Get them right. or expect header to fail miserably

All that’s left now is getting that pesky user 10. To do that, you need something that’s one step removed from your current PHP knowledge: an incredibly handy PHP function called mysql_ insert_ id. This isn’t the sort of function you’ll easily find unless you’re looking for, say, a function to get the 10 of the last row INSERTed into a database table with an AUTO_INCREMENT column.

Yes, that’s the exact definition of mysql_ insert _id! It’s built exactly to do what you want to do: get an 10 without any additional SELECT or work.

Even though you can pass a resource into mysql_ insert_ id, it will automatically use the last opened resource, which is perfect. Just add this after your INSERT is called via mysql_ query, and it will automatically reference the resource returned from that call.

What does it return for a value? Just the 10 of the user you want. Yoo can actually tag that onto the URL, just as when you were typing in your URL manually

Go ahead and visit your user creation form, fill out some data, and then submit it. You should be rewarded not by the output of create user.oho, but by loading the user that was just created. Figure 7-13 shows why this should be a fist-pumping moment

php and MySQL

php and MySQL

Rounding Things Out by Using Regular Expressions (Again)

Your profile page (Figure 7-13) is perfect. But. that output looks awful with all that text run together. The user probably pressed Enter a few times to separate the bio into neat-looking paragraphs, but those paragraphs don’t show up in HTML. What you really need is a quick and easy way to replace those Enter key presses with <p></p> tags

You need a way to find certain specific characters and replace them with other characters. You know that each occurrence of Enter is represented by \r or \n or some combination of the two (Chapter 6, page 166), which means that you can use regular expressions to find them and then replace them.

Using preg_ match, update show_user. php to change occurrences of Enter into HTML <p> tags:

<?php
// Database connection code
// SELECT the correct user
if ($result) {
$row = mysql_fetch_array($result);
$first_name = $row[‘first_name’];
$last_name = $row[‘last_name’];
$bio = preg_replace(“/[\r\n]+/”, “</p><p>”, $row[‘bio’]);
$email = $row[’email’];
$facebook_url = $row[‘facebook_url’];
$twitter_handle = $row[‘twitter_handle’];
// Build the Twitter URL
?>
// HTML output

Clearly, you can see why regular expressions are so powerful. You didn’t need lots of looping and searching, and you don’t have to figure out whether the user entered \r or \n or \r\n based on her platform. You just plug in the right regular expression, and you’re off to the races.

All of this put together should give you a version of show_useroho like the following:

<?php
require’ ..I../scripts/app_config.php’;
require’ ..I../scripts/database_connection.php’;
// Get the user ID of the u~er to show
$user_id = $_REQUEST[‘user_id’];
// Build the SELECT statement
$select_query = “SELECT * FROM users WHERE user id =” $user~id;
// Run the query
$re~ult = mysql_query($select_query);
if ($result) {
$row = mysql_fetch_array($result);
$first_name = $row[‘first_name’];
$last_name = $row[‘last_name’];
$bio = pregJeplace(“/[\r\n]+I”, “</p><p>”, $row[‘bio’]);
$email = $row[’email’];
$facebook_url = $row[‘facebook_url’];
$twitter_handle = $row[‘twitter_handle’];
// Turn $twitter_handle into a URL
$twitter_url = ..http://www.twitter.com/
substr($twitter_handle, $position + 1);
// To be added later
$user_image = “../../images/missing_user.png”;
} else {
die(“Error locating user with ID {$user_id}”);
?>
<html>
<head>
<link href=”../../css/phpMM.css” rel=”stylesheet” type=”text/css” I>
</head>
<body>

<div id;”header”><h1>PHP & MySOL: The Missing Manual</h1></div>
<div id;”example”>User Profile</div>
<div id;”content”>
<div class;”user_profile”>
<h1><?php echo “{$first_name} {$last_name}”; ?></h1>
<p><img src;”<?php echo $user_image; ?>” class;”user_pic” />
<?php echo $bio; ?></p>
<p class;”contact_info”>Get in touch with <?php echo $first_name;?>:
</p>
<ul>
<li> …by emailing them at
<a href;”<?php echo $emaif; ?>”><?php echo $email; ?></a></li>
<Li >  by
<a href;”<?php echo $facebook_url; ?>”>checking them out
on Facebook</a></li>
<li> …by <a href='<P php echo $twitter_url; ?>”>following them on Twitter</a></li>
</ul>.
</div>
</div>
<div id;”footer”></div>
</body>
</html>

When you take this for a test spin, you’ll finally see not just your user’s information, but a nicely formatted biography, as presented in Figure 7·14.

php and MySQL

php and MySQL

The Match Name

You may have noticed that there’s a continuous line from the name of a field in your HTML in

But being consistent in your naming makes your life easier  You never have to think, “i know what i called that variable in my PHP,but what was the database column name again

Here’s the flip side, though There are some standard conventions for naming variables in different programming languages and database structures The Java language favors less underscores, and more capitalization Thus first Name would be preferred over first_name; the same is true in (++ although PHP and languages like Ruby prefer underscores over capitalization SQL definitely favors underscores

What this boils down to is a sort of conditional rule of thumb: it you can be consistent without messing up.the conventions of the language within which you’re programming do it! Your code is easier to read from the outermost HTML page to the innermost database table Because PHP is one of the languages that likes underscores use them and keep things simple and consistent across your different pieces of your application,

Posted on January 13, 2016 in Generating Dynamic Web Pages

Share the Story

Back to Top
Share This