Jump to content

Beginners Questions


Virtual-Instructor

Recommended Posts

In order to do that you have to have a local test server. Then you put in the URL, username password, etc. to set it up. Either way you need a test server so its just easier going to the web browser and typing localhost in the web address bar. I use dreamweaver as well, and by the time you get the live view setup in dreamweaver you could just open a browser :) it does the samething :P. But here is a tutorial to set it up: http://www.adobe.com/devnet/dreamweaver/articles/setup_testing_server.html

Link to comment
Share on other sites

Thanks for all the help everyone, I am finally getting results.

 

So just to update everyone, this is where I am. I am working in Video 2 of the CRUD folder. This one may not be that difficult. So I am working with Dreamweaver and going through the examples in the videos one at a time. I am altering the code just slightly to work with my dbase that I have set up. In the view records video Ben covers how to extract data from the dbase and display it on the page in a table. I almost came out of my skin when it worked and actually returned the records that I called! :clap: What I did notice was a message from DW; "Dynamically-related files could not be resolved because the site definition is not correct for this server." My problem is that if the statement is true, then why did the records pop up? Addtionally how do I resolve this issue so that it does not appear?

 

Any insight into this would be helpful.

Link to comment
Share on other sites

What I did notice was a message from DW; "Dynamically-related files could not be resolved because the site definition is not correct for this server."

I'm not a Dreamweaver user myself -- I use a different app for coding, and simply open my browser if I need to preview. So I'm not sure how much help I can be... That said, the error message does say that "the site definition is not correct for this server" so it may mean something within the Dreamweaver site definition needs to be fixed. I'm assuming you know how to do that? (but I don't want to assume anything...?)

Link to comment
Share on other sites

Its not a big deal, it won't effect anything you are doing but if you look in the bar above where you type the code you should see "setup" or something like that. I think DW just needs to sync up with the webroot folder (www).

 

:rolleyes: I actually fixed this one almost alone. I realized that I had the wrong set up for the file location. Ah well live and learn.

 

Speaking of learing. I'm really starting to enjoy this. I'm making minor alterations to the code and it still seems to be working. That is up until I tried to add a new record. Now I am stuck! :bash: I've been over the code 6 times and I can't figure where I made the wrong turn, but I keep getting the same error.

 

Fatal error: Call to undefined method mysqli_stmt::bind_pararm() in C:\Program Files (x86)\wamp\www\STC\records.php on line 64

 

I have a sneaky suspicion that I just set the thing up wrong, (I tend to do that alot) but I can't figure out where. I'm including the code for this one below. The bolded out code is line 64. I know that it has something to do with my parameters, but I'm not sure what.

 

<?php

include("db-connection.php");

 

function renderForm($first = '', $last = '', $error = '', $id = '')

{ ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>

<?php if ($id != ''){echo "Edit Record"; } else {echo "New Record"; } ?>

</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

<body>

<h1><?php if ($id != ''){echo "Edit Record"; } else {echo "New Record"; } ?></h1>

<?php

if ($error != '')

{

echo "<div style='padding:4px; border:1px solid red; color:red'>" .$error . "</div>";

}

?>

 

<form action="" method="post">

<div>

<?php if($id != '') { ?>

<input type="hidden" name="id" value="<?php echo $id; ?>" />

<p>ID: <?php echo $id; ?></p>

<?php }?>

 

<strong>First Name: *</strong> <input type="text" name=first_name value="<?php echo $first; ?>"/><br/>

<strong>Last Name: *</strong> <input type="text" name=last_name value="<?php echo $last; ?>"/>

<p>* Required</p>

<input type="submit" name="submit" value="Submit" />

 

</div>

</form>

</body>

</html>

 

<?php }

 

if (isset($_GET['id']))

{

// Editing existing record

renderForm(NULL, NULL, NULL, $_GET['id']);

}

else

{

// Create new record

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

{

$first = htmlentities($_POST['first_name'], ENT_QUOTES);

$last = htmlentities ($_POST['last_name'], ENT_QUOTES);

 

if ($first == '' || $last == '')

{

$error = 'ERROR: Please fill in all required fiedlds!';

renderForm($first, $last, $error);

}

else

{

if ($stmt = $mysqli->prepare("INSERT members (first_name, last_name) VALUES (?,?)"))

{

$stmt->bind_pararm("ss", $first, $last);

$stmt->execute();

$stmt->close();

}

else

{

echo "ERROR: Could not prepare SQL statement.";

}

 

header("Location: view.php");

}

}

else

{

renderForm();

}

}

$mysqli->close();

?>

Link to comment
Share on other sites

I want to publicly thank Ben and Randy for all of their help so far. You guys have been invaluable, and I don't believe that I would understand this stuff as well as I do without both of your support. Thank you.

 

Ok so I think I'm getting close to making my first form work. I know I'm missing something but I'm not sure what. To make things easy, I have attached two files to this post, one is a snap shot of the table that I am working with, and the second one is the errors that I am getting.

 

Here is my code so far. :bash:

 

<?php

//Connect to the DBase

include('connection-db.php');

//Create the form after the php code, then handle the form with php.

?>

 

<form action="" method="post">

 

<div>

<label for="bvaid">Mentor BVA ID:</label>

<input type="text" name="bva" id="bvaid" size="20"><br />

<label for="first">First Name:</label>

<input type="text" name="name" id="first" size="20" /><br />

<label for="date">Available Date:</label>

<input type="text" name="date" id="date" size="20" /><br />

<label for="start">Start Time:</label>

<input type="time" name="start" id="start" size="20" /><br />

<label for="stop">Stop Time:</label>

<input type="text" name="stop" id="stop" size="20" /><br />

<input type="submit" name="submit" value="Create Block">

</div>

 

</form>

 

<?php

//this is how I handle the form//..

if($_POST['submit'])

{

$id=$_POST['bva'];

$name=$_POST['name'];

$date=$_POST['date'];

$start=$_POST['start'];

$stop=$_POST['stop'];

echo "Name: " . $id . "<br />";

echo "First Name: " . $name . "<br />";

echo "Date Available: " . $date . "<br />";

echo "Start Time: " . $start . "<br />";

echo "Stop Time: " . $stop . "<br />";

 

if ($stmt = $mysqli->prepare("INSERT schedule (mentor_id, mentor_first_name, avail_date, start_time, stop_time) VALUES (?,?,?,?,?)"))

{

$stmt->bind_param("ssdtt", $id, $name, $date, $start, $stop);

$stmt->execute();

$stmt->close();

}

}

?>

dbase.PDF

dbase problem.PDF

Link to comment
Share on other sites

what does your "connection-db.php" file look like? The error you are getting currently seems to indicate to me that you aren't connecting successfully to the database.

 

<?php

 

//Create the Variables for the db connection.

$server = 'localhost';

$user = 'root';

$pass = '';

$db = 'stc';

 

//Make the Connection.//

$my_sqli = new mysqli($server, $user, $pass, $db);

 

//Return any errors.//

mysqli_report(MYSQLI_REPORT_ERROR);

 

?>

 

This is the code that I use to connect to the db. It is based directly from the other connection that I established for another db that I built to work the examples in the videos.

Link to comment
Share on other sites

what does your "connection-db.php" file look like? The error you are getting currently seems to indicate to me that you aren't connecting successfully to the database.

 

<?php

 

//Create the Variables for the db connection.

$server = 'localhost';

$user = 'root';

$pass = '';

$db = 'stc';

 

//Make the Connection.//

$my_sqli = new mysqli($server, $user, $pass, $db);

 

//Return any errors.//

mysqli_report(MYSQLI_REPORT_ERROR);

 

?>

 

This is the code that I use to connect to the db. It is based directly from the other connection that I established for another db that I built to work the examples in the videos.

Link to comment
Share on other sites

You have the basic idea correct, though perhaps the exact terminology is wrong.

 

That line creates ("instantiates") a new object (note the "new" section of that line), which is stored within a variable $my_sqli. My "mysqli()" section indicates which object you want to create, and the variables within the parenthesis are called "arguments", settings which the object requires when it is first created. This is an example of object oriented programming, which you can learn about here: http://killerphp.com/tutorials/object-oriented-php/

 

Yes, you can change "$my_sqli" to something else if you'd prefer -- just make sure you stay consistent and use the exact same variable name throughout your code.

 

 

EDIT: I could have sworn you replied to this topic, which is what I was responding to above. I'm not sure what happened to your reply? Maybe the forum ate it, or you deleted it?

Link to comment
Share on other sites

Ben you are a Genius! :clap: Ok so then let me clarify so that I understand.

 

$my_sqli = new mysqli($server, $user, $pass, $db);

 

The bold text is a created variable and the italisized text is the actual command line to which we pass the previously established connection variables? If that is true could I then rename the $my_sqli variable to something like $dbconnection?

 

Moving on here. Ok so it would appear that I am getting closer. I have changed nothing in the dbase table but I am now getting a new error as you can see on the attached file. I'm pretty sure that I simply used the wrong data type here. But going off of your previous examples, I figured that the data type was simply the first letter of the data type. Both the start and stop variables are units of time as in hours of the day. Correcting one will probably lead to a correction of both and allow the data to be saved to the dbase.

time.PDF

Link to comment
Share on other sites

Can you give me an example of what sort of text the user might enter into that field? To be honest, I'd suggest converting those time inputs into dropdown selects if at all possible, so you can be sure of what sort of data the user will be entering.

 

If you look up the bind_param() function on php.net (http://php.net/manual/en/mysqli-stmt.bind-param.php) you'll notice you only have a couple options -- "i", "d", "s", "b". If you are expecting numbers, you'd probably want "i" for integer. If you are expecting a string (for example "2:00") this might be a bit trickier. I've always had a little bit of trouble dealing with dates in PHP, and knowing how to handle conversion between the different date formats.

 

Keep in mind -- ideally, you would want your PHP script to do some basic validation and ensure that the values that you are getting from the form are valid. For example, currently, if the user decided to enter in something invalid into the form (let's say text into a field where your script is expecting a number) it may cause errors when you try to run the database query. Ensuring that the user is providing valid values will help prevent errors and improve the user's experience.

Link to comment
Share on other sites

Can you give me an example of what sort of text the user might enter into that field? To be honest, I'd suggest converting those time inputs into dropdown selects if at all possible, so you can be sure of what sort of data the user will be entering.

 

If you look up the bind_param() function on php.net (http://php.net/manual/en/mysqli-stmt.bind-param.php) you'll notice you only have a couple options -- "i", "d", "s", "b". If you are expecting numbers, you'd probably want "i" for integer. If you are expecting a string (for example "2:00") this might be a bit trickier. I've always had a little bit of trouble dealing with dates in PHP, and knowing how to handle conversion between the different date formats.

 

Keep in mind -- ideally, you would want your PHP script to do some basic validation and ensure that the values that you are getting from the form are valid. For example, currently, if the user decided to enter in something invalid into the form (let's say text into a field where your script is expecting a number) it may cause errors when you try to run the database query. Ensuring that the user is providing valid values will help prevent errors and improve the user's experience.

 

Times will be handled in military time, or a 24 hour clock. 13:00, 15:00 and so on. I actually did try the integer method, but it displayed in the dbase all wrong, which leads me to believe that I would have a similar problem when extracting the information. I am of the opintioin that if I could find a way to format the infromation being inserted into the dbase then that would certainly help. But as a basic anwser to your question, the information will deffinately be integer in nature. What I would really like to do is to auto format the entry so that as they type it just naturally fills in. As I said before, right now, at this stage of my learning, I would just be happy making the functionality work, I can go back later and improve on it.

 

Oh and by the way, I tried clicking on the link that you sent me and it didn't work. Said that it wasn't available at this time.

Link to comment
Share on other sites

Corrected link (sorry, I forget that if I have a parenthesis right after the link, the forum includes the parenthesis within the link): http://php.net/manual/en/mysqli-stmt.bind-param.php

 

Sounds like the correct way forward is to use "i" -- it's just a matter of formatting that input into something that the database will be able to understand. I'll get back to you on this.

Link to comment
Share on other sites

Corrected link (sorry, I forget that if I have a parenthesis right after the link, the forum includes the parenthesis within the link): http://php.net/manual/en/mysqli-stmt.bind-param.php

 

Sounds like the correct way forward is to use "i" -- it's just a matter of formatting that input into something that the database will be able to understand. I'll get back to you on this.

 

Thanks for the updated link, that time it worked. I tried using the integer method and that seemed to work, but when it loaded to the dbase it was in the way wrong format. Inputting 14:00 into the form, returned 00:00:14 in the dbase.

Link to comment
Share on other sites

Then you have to manually modify the user's input after you have retrieved the $_POST data to ensure it's in the correct format. It could be as simple as doing this:

 

$time = $_POST['time']; // this assumes the user enters in "xx:xx";

$time = $time . ':00';

 

though it would probably be slightly more difficult than that since you can't necessarily assume the user will enter in both hours and minutes. If they just enter "14", it will be interpreted as 14 minutes, not 14 hours.

 

As I said earlier, your best bet on this is probably to use a select rather than a text input, and have the user select from a predefined list of options:

 

<select name="time">

...

<option value="12:00:00">12:00</option>

<option value="13:00:00">13:00</option>

<option value="14:00:00">14:00</option>

...

</select>

 

This has some more info on PHP's time functionality: http://www.tizag.com/mysqlTutorial/mysql-time.php

Link to comment
Share on other sites

Then you have to manually modify the user's input after you have retrieved the $_POST data to ensure it's in the correct format. It could be as simple as doing this:

 

$time = $_POST['time']; // this assumes the user enters in "xx:xx";

$time = $time . ':00';

 

though it would probably be slightly more difficult than that since you can't necessarily assume the user will enter in both hours and minutes. If they just enter "14", it will be interpreted as 14 minutes, not 14 hours.

 

As I said earlier, your best bet on this is probably to use a select rather than a text input, and have the user select from a predefined list of options:

 

<select name="time">

...

<option value="12:00:00">12:00</option>

<option value="13:00:00">13:00</option>

<option value="14:00:00">14:00</option>

...

</select>

 

This has some more info on PHP's time functionality: http://www.tizag.com/mysqlTutorial/mysql-time.php

 

:bash: Ok this is getting complicated and now I feel like I'm getting nowhere. Try this one on for size.

I've been doing some reading, and it looks as though I can't do anything to alter the format in the dbase table. It is set that way it is and its not going to change. Rather than typing out dozens of lines of code to handle this issue for this single form, what if I started a new file and created the functions to handle these issues in that file, then called the function from the file for the form that I am working on. This way, I should be able to keep my code relatively clean and still accomplish my goal.

Link to comment
Share on other sites

In regards to the javascript date picker, here are two I would would suggest:

 

http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/

http://jqueryui.com/demos/datepicker/

 

The first link might need a little bit of CSS work to improve the looks of the calendar, but either would work. I can help with questions once you get to the point where you want to integrate this into your site.

 

One thing though -- while it does generate a consistent format for dates, it isn't always in the correct format that the database needs (at least with the JqueryUI datepicker, though, you can actually specify the format.) In that case, you'd then need to change the format, probably using the "explode" function to separate the date into its individual parts. Here's bit of sample code that will give you an idea of what I mean:

 

$date = '12/5/2010';
$date = explode('/', $date); // splits date into an array of individual parts. Divide at the "/"
echo $date[2] . '-' . $date[0] . '-' . $date[1];

Link to comment
Share on other sites

:huh: I think I have throughly confussed myself :huh:

 

Ok so as of now I have established a page to view available training blocks. The table has a hyperlink to "Request Block", which then takes the user to the request schedule page (that code is below). What I am trying to do right now is pass some of the data from the actual record to the form itself which will display the infromation between the header tags. I spent a good deal of time trying to figure out how to get the information out, and I think that what I have right now should work if I have understood everything correctly. My problem is in setting the variables. (see the bolded code below) I'm having trouble trying to figure out how to grab the record id from the Available Training Blocks Page. I know that I am probably just dancing around the anwser, but I'm still so new at this I have a tendency to chase my tail as it were. Not all of the code is here because I'm not finished with it just yet, but if you want to see what I have, then please just let me know.

 

<?php

//Connect to the dbase.//

include('connection-db.php');

?>

 

<form action="" method="post">

<div>

 

<h1>

<?php if($id != '') { ?>

<input type="hidden" name="id" value="<?php echo $id; ?>" />

<p>ID: <?php echo $id; ?></p>

<?php }?>

<?php

 

//Set the Variables//

$mentorid = $my_sqli->query("SELECT mentor_id FROM schedule WHERE id = '?'");

$mentorname = $my_sqli->query("SELECT mentor_first_name FROM schedule WHERE id = '?'");

$date = $my_sqli->query("SELECT avail_date FROM schedule WHERE id = '?'");

$time = $my_sqli->query("SELECT start_time FROM schedule WHERE id = '?'");

if ($id != '')

{

echo "Request Training flight with" . $mentorid . " - " . $mentorname . "on" . $date . "beginning at" . $time .".";

}

else

{

echo "";

} ?>

</h1>

 

<label for='pilotid'>Pilot BVA ID:</label>

<input type='text' name='pilot' id='pilotid' size="20"><br />

<label for='pilotname'>Pilot First Name:</label>

<input type='text' name='first' id='pilotname' size="20"><br />

<label for='program'>Please Select Your Program:</label>

<input type="text" name="program" id="program" size="20"><br />

<label for='type'>Please Select a Flight Type:</label>

<input type="text" name="type" id="type" size="20"><br />

<label for='description'>Describe Flight Objective</label>

<input type="text" name="descriptioin" id='descriptioin' size="20"><br />

<label for='ac'>Please Select AC Type</label>

<input type="text" name="ac" id="ac" size="20"><br />

<input type="submit" name="submit" value="Submit Request">

</div>

</form>

Link to comment
Share on other sites

:huh: Ok I think I may have missed something but I need a little help in determining where. I'm going through the PHP Login videos right now and I'm on Part 3. I have double and tripple checked everything and still cannont find where I went wrong. I've been following Ben trhough the examples, but for some reason when I check the code in IE, I get what you see on the attached document. The error reporting is directing me back to line 11 of my login.php code, but I don't see where I did anything different than what Ben did, asside from all the notes that I have been taking. Anyone with any insight is welcome to comment.

 

<?php

 

/*

* LOGIN.PHP

* Log In Members

*/

// Query the dbase and make sure that the userneame and password that //the user has entered matches what the dbase has. If so set the session variable and direct the member on to the members page.

// Sessions are a way to store data along multiple pages. They are temporary so that they will be destroyed when either the destroy function is called or when the browser is closed. They work by temporarily setting a cookie on the users browser that uniqely identifies the users machine and allows them to interact with the server, the server will automatically save variable associated with the user.

 

// start session / load configs. This needs to be the first thing that the server see's. Spaces at the top of the document will cause problems.

$session_start();

//Include the needed files.

include('includes/config.php');

include('includes/db.php');

 

// form defaults

$error['alert'] = '';

$error['user'] = '';

$error['pass'] = '';

$input['user'] = '';

$input['pass'] = '';

 

 

include('views/v_login.php');

?>

session start line 11.PDF

Link to comment
Share on other sites

I've made yet another error. Ben showed me once how to get rid of that index error, but I don't remember how. The other problem that I have as you will see by the attached file is that my yellow Alert box doesn't show. The actual text shows up, but for some reason the yellow box is missing.

 

style.css code

body { font-family: Arial; margin:0; }

h1 { background: green; padding: 20px; font-size: 1.2m; color: white; margin: 0 0 20px 0; }

#content { padding: 0 20px; }

 

.required { color: red; font-size: .8m; clear:both; padding-top: 20px; }

.error { float: left; color: red; display: block; padding: 4px 0 0 10px; font-size: .8m }

.alert { background" #feff96; border: 1px solid yellow; padding: 8px; margin-bottom: 20px; }

 

form { overflow: auto; }

label { float: left; clear: both; width: 160px; display: block; }

input { width: 220px; padding: 2px; margin-bottom: 4px; float: left; }

input.submit { float: left; clear:both; width: 80px; margin-top: 20px; }

 

 

v_login.php

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Log In</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="views/style.css" media="screen" rel="stylesheet" type="text/css" />

</head>

 

<body>

<h1>Log In</h1>

<div id='content'>

<form action='' method="post">

<div>

<?php if($error['alert'] !='')

{ echo "<div class='alert'>" .$error['alert']."</div>"; } ?>

 

<label for='username'>Username: *</label>

<input type="text" name="username" value="<?php echo $input['user'];?>"><div class="error"><?php echo $error['user']; ?></div>

 

<label for='password'>Password: *</label>

<input type="password" name="password" value="<?php echo $input['pass'];?>"><div class="error"><?php echo $error['pass']; ?></div>

 

<p class="required"> * Required Fields</p>

 

<input type="submit" name="submit" class='submit' value="Submit">

</div>

</form>

</div>

 

</body>

</html>

 

login.php

 

// form defaults

$error['alert'] = '';

$error['user'] = '';

$error['pass'] = '';

$input['user'] = '';

$input['pass'] = '';

 

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

{

//Process the form

if($_POST['usernmae'] == '' || $_POST['password'] == '')

{

//both fields need to be filled in

if ($_POST['username'] == '') {$error['user'] = 'required!';}

if ($_POST['password'] == '') {$error['pass'] = 'required!';}

$error['alert'] = 'Please fill in the Required Fields';

 

$input['user'] = $_POST['username'];

$input['pass'] = $_POST['password'];

 

include('views/v_login.php');

}

}

else

{

include('views/v_login.php');

}

 

 

 

?>

 

Now I'm pretty sure that the error is somewhere between the style.css and v_login.php. Of course I could be wrong here, but anyone that has any insight, I would appriciate it if someone could explain this one to me.

Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

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...
×
×
  • Create New...