Jump to content
Larry Ullman's Book Forums

Paul

Members
  • Posts

    147
  • Joined

  • Last visited

  • Days Won

    1

Everything posted by Paul

  1. Thanks for your opinions. Piecing togethor both of your replies it would seem that the consensus seems to be learn Ajax, whilst at the same time picking up relevant pieces of JS along the way. I do think that it's interesting and a touch ironic that with all this technology when we want to learn something we always turn to a good old fashioned book! Cheers Paul
  2. Hi all, Having read the post from Chop on a complex form it's raised a couple of questions that I've been mulling over. Javascript and PHP. I know that Javascript is client side and PHP is server side. I know that Javascript is used for manipulating various aspects of the browser. However aside from this there appears, to my amateur eyes, to be a vast degree of similarity. I guess my question is: 1. What 'bits' of Javascript need to be learnt in order to make dynamic websites, without spending time duplicating knowledge already learnt with PHP? I have a very large, 'learn Javascript from scratch' book that is about as intersting as watching paint dry. It would be good to skip to the interesting bits! 2. This sort of leads into the next question. Currently I'm comfortable with producing static websites using XHTML and CSS. I want to produce dynamic websites so I'm learning PHP (using Larry's PHP6 & MySQL5 book) but where should I go from here to extend my knowledge. Should Javascript be the next port-of-call or more advanced PHP or Ajax or......? What would be the most beneficial learning curve? I appreciate that the answer may well be a subjective one, but would be very interested in some opinions. Thanks once again everyone. I aspire, one distant day, to actually contribute to this forum as opposed to just keep asking! Cheers Paul
  3. Antonio, Thanks for all the time you've put into these replies. I take on board what you're saying about the results of a query being an object that I need to tell what to do with. I have started to look at the PHP manual but as a beginner it can get a little daunting! I will stick with it though. Thanks again for your help. Paul
  4. Antonio, So I need to use mysqli_fetch_array in order to use the results from a DB query. So $result (in your code below) contains the results of the query but are not accessible. Beginners question, but why does there need to be the extra step. Is it in case the query returns more than one row? Also in your code below where does "name" come from in the echo line. Should it read "$row["firstname"] . $row["lastname"]" Cheers Paul
  5. Hi Antonio, So, in order to get it to work I would have to run a SELECT query outside of the 'if' and then I could assign that to a variable and use that in some text, i.e. $q = "SELECT CONCAT (first_name, last_name) FROM users WHERE user_id = 22; $r = @mysqli_query($dbc, $q); echo '<p>This users name is' . $r . '!'</p>'; Would that work?
  6. Hi everyone, Please refer to the following code: <?php # Script 9.2 - delete_user.php //This page is for deleting a user from the DB //Accessed through view_user.php $page_title = 'Delete a user'; include ('includes/header.html'); echo '<h1>Delete a user</h1>'; // Check for a valid user ID, through GET or POST if ( (isset($_GET['id']) ) && (is_numeric($_GET['id']) ) ) //This checks that the id is appended to the url from view_users.php { $id = $_GET['id']; } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) //This checks that the id has come from the hidden field in the submitted form { $id = $_POST['id']; } else { //There is no valid id at all and therefore kill the script here echo '<p class="error">Oops, what are you doing here?</p>'; include ('includes/footer.html'); exit(); } require_once ('../mysqli_connect.php'); //Check that the form has been submitted. if (isset($_POST['submitted'])) //Has the form been submitted or just loaded? { //First check that the user clicked on the yes button on the form if ($_POST['sure'] == 'Yes') { //Make the query to delete the user $q = "DELETE FROM users WHERE user_id = $id LIMIT 1"; $r = @mysqli_query($dbc, $q); if (mysqli_affected_rows($dbc) == 1) //Checking that the query ran OK { //Print the success message echo '<p>The user has been deleted.</p>'; } else { //Error message if the query didn't run OK echo '<p>The user could not be deleted at the moment because of a system error</p>'; echo '<p>' . mysqli_error($dbc) . '<br />Query:' . $q . '</p>'; } } else //The user did not click on the yes button on the form { echo '<p>If you want to delete' . $row[0] . 'you need to click on the yes button</p>'; } } else //The form has not been submitted yet so load the form { //First retrieve the users info. from the DB $q = "SELECT CONCAT (last_name, ' , ', first_name) FROM users WHERE user_id = $id"; $r = @mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) //Check that the query returned a user { $row = mysqli_fetch_array ($r, MYSQLI_NUM); //The form for deleting a user echo ' <form action="delete_user.php" method="post"> <h3>Name: ' . $row[0] . '</h3> <p>Are you sure that you want to delete this user?<br /> <input name="sure" type="radio" value="Yes"> Yes <input name="sure" type="radio" value="No" checked> No </p> <p><input name="submit" type="submit" value="Submit"></p> <input name="submitted" type="hidden" value="TRUE"> <input name="id" type="hidden" value="' . $id . '"> </form>'; } else { //There were no rows or more than 1 row returned by the SELECT query echo '<p class="error">Oops, you shouldn\'t be here</p>'; } } mysqli_close($dbc); include ('includes/footer.html'); ?> The code works fine, as per Larry's example, but I tried to be smart. Within the following line, which comes up if the user does not confirm the deletion, I tried to insert the users name by using $row[0]. My thinking was that $row[0] has already been populated with the users name when the form loaded. Therefore all I'm doing is referring to the same variable in order to make the sentence read better. The error tells me that it's an undefined variable. echo '<p>If you want to delete' . $row[0] . 'you need to click on the yes button</p>'; What am I doing wrong? Cheers Paul
  7. Hi everyone, I ran the same script through Chrome and IE8 (I use Firefox) and the fields were not populated so I think HartleySan has cracked it. Could I also refer you all to the 2nd part of my original question: 2. In script 8.5 there are two 'mysqli_close($dbc)' functions but only one 'require_once('../mysqli_connect.php') function. Please could someone explain why a DB needs to be closed twice when it has only been opened once. As per Jonathon's request I've put the code here: <?php # Script 8.3 - register.php $page_title = 'Register'; include ('includes/header.html'); // Check if the form has been submitted if (isset($_POST['submitted']) ) { //Open the DB require_once('../mysqli_connect.php'); //Initialise and error array $errors = array(); //Check for a first name and assign it to a variable if (empty($_POST['first_name']) ) { $errors[] = 'You forgot to enter your first name.'; } else { $fn = mysqli_real_escape_string($dbc, trim($_POST['first_name'])); } //Check for a last name and assign it to a variable if (empty($_POST['last_name']) ) { $errors[] = 'You forgot to enter your last name.'; } else { $ln = mysqli_real_escape_string($dbc, trim($_POST['last_name'])); } //Check for an email address and assign it to a variable if (empty($_POST['email']) ) { $errors[] = 'You forgot to enter your email address.'; } else { $e = mysqli_real_escape_string($dbc, trim($_POST['email'])); } //Check for a password, match it against the confirm field and assign it to a variable if (!empty($_POST['pass1']) ) { if ($_POST['pass1'] != $_POST['pass2']) { $errors[] = 'Your password did not match the confirmed password.'; } else { $p = mysqli_real_escape_string($dbc, trim($_POST['pass1'])); } } else { $errors[] = 'You forgot to enter your password.'; } //Check if the errors array is empty. If so register the user. If not print the details to show the user (done later) if (empty($errors) ) { //Register the user //Make the query and insert into DB $q = "INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )"; //As per fourm suggestion $r = @mysqli_query ($dbc, $q) or die("Error: " . mysqli_error($dbc) ); //this will output any query string error syntax. //$r = @mysqli_query ($dbc, $q); //Check if the query ran. Does $r have a TRUE value? if ($r) { //Print a thankyou message to the user echo '<h1>Thank you</h1> <p>You are now registered. In Chapter 11 you will actually be able to log in!</p><p><br /></p>'; } else { //If the query didn't run OK. //A message to the user echo '<h1>Oops, system error.</h1> <p class="error">You might not be registered because of a system error. Sorry.</p>'; //A message to the programmer echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>'; } //Close the Db connection mysqli_close($dbc); //include the footer and exit the script include ('includes/footer.html'); exit(); } else { //Report the input errors to the user echo '<h1>Errors</h1> <p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p><p><br /></p>'; } //Close the Db connection mysqli_close($dbc); } ?> <!--'// Start of HTML--> <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> <p> <input type="hidden" name="submitted" value="TRUE" /> </p> </form> Thanks everyone. Paul
  8. Jonathon and JorgeLP, Thanks once again for a quick response. Right. I changed p to $p at JorgeLP's suggestion and inserted the line of code. Rerun. Same. Jonathon. You are correct. The form is OK, but 2 of the fields are already populated. Not sure if this is what you mean but I cleared the cache from the browser, closed the browser window and rerun the script. Same thing, two fields populated with what I'm assuming are my login details. Thanks Paul
  9. Hi, 1. When I run the following code, as per the above scripts, there are no errors and the blank form appears OK except that in the email and password fields appear 'root' and what I'm assuming is my DB password, although it appears as '*******'. (PHP version 5.3.4, MySQL client version: mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $). What am I doing wrong? Even as a novice I could see that this is probably not a good security measure! START OF SCRIPT <?php # Script 8.3 - register.php $page_title = 'Register'; include ('includes/header.html'); // Check if the form has been submitted if (isset($_POST['submitted']) ) { //Open the DB require_once('../mysqli_connect.php'); //Initialise and error array $errors = array(); //Check for a first name and assign it to a variable if (empty($_POST['first_name']) ) { $errors[] = 'You forgot to enter your first name.'; } else { $fn = mysqli_real_escape_string($dbc, trim($_POST['first_name'])); } //Check for a last name and assign it to a variable if (empty($_POST['last_name']) ) { $errors[] = 'You forgot to enter your last name.'; } else { $ln = mysqli_real_escape_string($dbc, trim($_POST['last_name'])); } //Check for an email address and assign it to a variable if (empty($_POST['email']) ) { $errors[] = 'You forgot to enter your email address.'; } else { $e = mysqli_real_escape_string($dbc, trim($_POST['email'])); } //Check for a password, match it against the confirm field and assign it to a variable if (!empty($_POST['pass1']) ) { if ($_POST['pass1'] != $_POST['pass2']) { $errors[] = 'Your password did not match the confirmed password.'; } else { $p = mysqli_real_escape_string($dbc, trim($_POST['pass1'])); } } else { $errors[] = 'You forgot to enter your password.'; } //Check if the errors array is empty. If so register the user. If not print the details to show the user (done later) if (empty($errors) ) { //Register the user //Make the query and insert into DB $q = "INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('p'), NOW() )"; $r = @mysqli_query ($dbc, $q); //Check if the query ran. Does $r have a TRUE value? if ($r) { //Print a thankyou message to the user echo '<h1>Thank you</h1> <p>You are now registered. In Chapter 11 you will actually be able to log in!</p><p><br /></p>'; } else { //If the query didn't run OK. //A message to the user echo '<h1>Oops, system error.</h1> <p class="error">You might not be registered because of a system error. Sorry.</p>'; //A message to the programmer echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>'; } //Close the Db connection mysqli_close($dbc); //include the footer and exit the script include ('includes/footer.html'); exit(); } else { //Report the input errors to the user echo '<h1>Errors</h1> <p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p><p><br /></p>'; } } ?> <!--'// Start of HTML--> <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> <p> <input type="hidden" name="submitted" value="TRUE" /> </p> </form> END OF SCRIPT 2. In script 8.5 there are two 'mysqli_close($dbc)' functions but only one 'require_once('../mysqli_connect.php') function. Please could someone explain why a DB needs to be closed twice when it has only been opened once. Thanks once again. Paul
  10. Hi, I actually got it to work eventually but I wanted to wait for a reply because I'm not clear on what DEFINE is? To correct it I changed the statement from: DEFINE ('DB_USER', 'root'); DEFINE ('DB_PASSWORD', 'mypassword'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'forum'); // Make the connection $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() ); To: DEFINE ('DB_USER', 'root'); DEFINE ('DB_pass', 'mypassword'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'forum'); // Make the connection $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_pass, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() ); Which is, HartleySan, what you are saying. It's DB_PASSWORD that's the constant. I changed it to DB_PASS. But if the constant is only in the script then presumably DB_PASSWORD is gone forever. Which doesn't, in my mind, make it much of a constant. I guess I'm a bit confused about the point of constants or the DEFINE function. I was under the impression that once a DEFINE was defined it couldn't be changed...fullstop. Could the same thing be done just by creating a variable? What's the difference? Cheers.
  11. Hi, I think I've messed this one up. Here's what I did. I typed up the above script exactly. I didn't have a password set-up for 'root' so I typed DEFINE('DB_PASSWORD', '""'); It didn't work (Could not connect to MySQL: Access denied for user 'root'@'localhost' (using password: YES)). I thought that maybe it doesn't like not having a password so I went into XAMPP security and set up a password for the 'root' access. I tested this by setting up a DB connection in Dreamweaver and it tested and connected OK. So I know that my username, password and host names are correct. I changed the DEFINE part of the php file as follows - DEFINE ('DB_PASSWORD', 'mypassword');. It didn't work. Same error message. I then thought, oops, it's because the DB_PASSWORD as been set up as the constant '""'. Can a constant be changed or deleted? If not (which I suspect is the case, otherwise it would be a bit of a misnomer) where do I go from here? Out of interest is the constant value stored somewhere on the webserver? Thanks once again. Paul
  12. Thanks Larry, But looking ahead there may come a time when I would need to write something involving ROLLBACK but would be unable to test it, presumambly anything involving e-commerce. Would I need to use MySQL client to test it? Cheers Paul
  13. Hiya, I'm trying to run the example in chapter 6 for ROLLBACK. I'm using PHPMyAdmin. I start with START TRANSACTION;, and change the balance. The balance changes OK but when I go back into SQL and type ROLLBACK; it has no effect. Does ROLLBACK not work in PHPMyAdmin because when you do SELECT blah blah to check the balance change it leaves the SQL window and displays the results in a table. I then click back on the SQL tab and type ROLLBACK;. Any ideas. Cheers Paul
  14. Larry, Correct. I changed Skype. The XAMPP website has a specific section for Vista problems with instructions for changing the Skype port. HartleySan said that Windows servers suck, I'm not sure, from reading other forums, that Vista and, for that matter, IE8 are far behind. Since I (or rather XAMPP and HartleySan) got it to work I've created my first database and table. Really enjoying the book. Cheers Paul
  15. Hiya, I took your advice and installed XAMPP. However Apache does not appear to work. It starts, comes up with 'running' and 5 seconds later turns itself off. I've posted an entry on the XAMPP forum but have you (or anyone else) come accross this problem? Cheers Paul
  16. HartleySan, Thanks very much for all your info. I have asked my hosting company to move my website over to Linux. The database is not a problem as I've only just starting playing with it using Larry's book, so there's nothing in it. It's interesting what you said about Windows. This is the kind of information, though, that beginners like me don't know. One more question, if I may. Up until now I have been following all the PHP examples by doing them myself on Dreamweaver and uploading them (using Dreamweaver) to my hosting company as a page of my website to make sure they work. In your opinion would I be better doing these tutorials and in future, testing, on my own machine. Dreamweaver has the ability, it appears, to set up a testing server. A quick look at a Dreamweaver article indicates that they think that I am safer and more efficient if I use a test server on my own machine. They give extensive instructions on how to do it. Would you agree that I'm better setting it up on my own machine than uploading it each time to my host? Thanks for all your help and I hope other beginners are benefiting from my basic questions! Paul
  17. HartleySan, Thanks for the response. I've tried looking at forums and playing with MyLittleAdmin and don't get a good feel for it. The forums are tiny. MyLittleAdmin doesn't seem to be anywhere near as big or as well supported as PHPMyAdmin. Could you help me understand this. I have a Windows hosting package that comes with a MS SQL database and MyLittleAdmin. They have told me that I can use PHPMyAdmin if they move my account, whatever, to Linux. However, if they did that I would need to back up my own website because they would have to point the DNS to Linux. I don't pretend to understand that but that's what they've told me. I guess that I'm trying to get my head round the difference between the MySQL / PHPMyAdmin combination and the MS SQL / MYLittleAdmin combination. It appears that they are not interchangable, one is Windows and one is Linux...or have I got that wrong? Is that just what my hosting company do? If I decide to change from Windows hosting to Linux does that make any difference to how my website works, or how I FTP pages etc? Can I use PHPMyAdmin myself (seperate from my hosting company) and connect with a MS SQL database? If so what information would I need - server name, database name, login and password? I'm very new to this (as you've undoubtably gathered). Thanks for your time, I appreciate it. Cheers Paul
  18. Hi, I'm using MyLittleAdmin as a client (that's what my hosting company supply) and under the section 'new query' I typed in the code from the book as per below. I received the error message shown below the code. There is an interface that allows table data to be input into little boxes, however there does not appear to be a way to do AUTO_INCREMENT and it doesn't have MEDIUMINT on its dropdown for 'type'. Any ideas anyone? Cheers Paul CREATE TABLE users ( user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, first_name VARCHAR(20) NOT NULL, last_name VARCHAR(40) NOT NULL, email VARCHAR(60) NOT NULL, pass CHAR(40) NOT NULL, registration_date DATETIME NOT NULL, PRIMARY KEY (user_id) ); Msg 102, Level 15, State 1, Line number 2 Incorrect syntax near 'UNSIGNED'.
  19. Hi, I have reached chapter 4 in the book where Larry starts to talk about databases. He uses PHPMyAdmin. My hosting company, because I chose a Windows hosting package, uses myLittleAdmin instead of PHPMyAdmin. Is this a problem? Are they pretty much the same? My hosting company tells me that they are but they are experts and not complete beginners. Thanks Paul
  20. Hiya, Referring to script 3.10 it appears that a function needs to be either returned or printed. I'm struggling to understand why that is. In the above mentioned script the function calculate_total performs the calculation of the total and puts it in a variable called $total. Further down the script the function is called based on the values entered on the form and the result placed in a variable called $sum. Now, from the 'Variable Scope' article at the end of the chapter a defined variable within a function cannot be accessed outside it. This, I assume, is why the results of the calculation are put into $sum and not $total. However why does the function need to have the line - return number_format ($total, 2) ? In my mind the function defines the job, this job is then called further down the script and it's results stored in $sum. Why is the return line needed? Thanks Paul
×
×
  • Create New...