Jump to content
eduard

Next step?

Recommended Posts

The goal was and is to show a php application (database) on my website! I´ve an insert -, select -, where -, delete php etc. and I´ve 2 forns html!So, what´s next?I assume 1 html form with several MYSQL queries!Where can I read how to do that?

Share this post


Link to post
Share on other sites

SELECT, INSERT, DELETE and UPDATE are different kinds of MySQL queries. If your application makes use of all of them, it's already a full, ready to be shown, working application.And to make use of them, you'd need one or more HTML forms, each with the purpose of adding (INSERT), editing (UPDATE), removing (DELETE) and retrieving (SELECT) some sort of data. The MySQL part of the PHP tutorial contains examples for all of these kinds of queries.

Share this post


Link to post
Share on other sites
SELECT, INSERT, DELETE and UPDATE are different kinds of MySQL queries. If your application makes use of all of them, it's already a full, ready to be shown, working application. And to make use of them, you'd need one or more HTML forms, each with the purpose of adding (INSERT), editing (UPDATE), removing (DELETE) and retrieving (SELECT) some sort of data. The MySQL part of the PHP tutorial contains examples for all of these kinds of queries.
I understand your reply! But how do I make such a html form?

Share this post


Link to post
Share on other sites

um.. just make each form submit to it's own script. If you can get that working, you can try and make it so that one form does it all. That could be as simple as having a hidden form input that passes a "command" if you will, (i.e. "update", "delete", "insert") that you can use in your script to determine what kind of query you need.

Share this post


Link to post
Share on other sites
um.. just make each form submit to it's own script. If you can get that working, you can try and make it so that one form does it all. That could be as simple as having a hidden form input that passes a "command" if you will, (i.e. "update", "delete", "insert") that you can use in your script to determine what kind of query you need.
I have (nearly: undefined index I don´t understand well - although it´s already explained to me - undefined is obvious, but ´index´ (?)), but I have the select, where, update and delete php files working!So, still my question: How does such a html form look like and where do I find an example of it?

Share this post


Link to post
Share on other sites

It's up to you to build the form with the fields you want to add to the database. It looks like any other HTML form. The insert and update forms can be the same, the update form just needs the data filled in from the database. A select and delete page don't need forms, in fact those would probably be the same page (a page that displays all of the data with a "delete" link next to each row).

Share this post


Link to post
Share on other sites

Correct me if I'm wrong, but 'index' means index in an array. Since $_POST, $_GET are basically global arrays in PHP, and arrays consist of 'indexes' or 'keys' or 'elements' but I believe the most common term for it is 'index'.A typical array may look like this: arrayExample[0] = 'Hello'; In this case, index 0 holds/has the value 'Hello'; ArrayExample[1] = 'world'; here index 1 has/holds the value 'world'; etc etc. Arrays always start from 0, not 1. For the $_POST global array, this is known as a 'associative array', meaning, instead of the index being a number, it will have a label/name to it. For example: $_POST['myName'], in this case, the index here is 'myName' but instead of being a number like 0, it has a label/name to it called 'myName' and it has/holds a value of someone's name when someone enters their name into the input field in the form: <input type="text" name="myName" />. You can then do this: $firstName = $_POST['myName']; The value in $_POST['myName'] will be set to/stored into $firstName. In your HTML form, make sure to name the 'name' attribute for the input field. Example: <input type="text" name="myName" /> Highlighted in red is the 'name' attribute. When you process the form with PHP, in other to receive the value in $_POST that came from the form's input field with the name attribute of "myName", you have to make sure the name attribute matches with the index of the $_POST array. Remember that $_POST is an associative array, so in this case it will be $_POST['myName']; If they don't match, for example <input type="text" name="fname" /> and in PHP you have this: $_POST['myName']; you'll get the 'undefined index' error message. "fname" and 'myName' don't match. Whatever the user types into here: <input type="text" name="myName" />You retrieve the value in PHP like this: $firstName = $_POST['myName']; Hope this helped.

Share this post


Link to post
Share on other sites
Correct me if I'm wrong, but 'index' means index in an array. Since $_POST, $_GET are basically global arrays in PHP, and arrays consist of 'indexes' or 'keys' or 'elements' but I believe the most common term for it is 'index'. A typical array may look like this: arrayExample[0] = 'Hello'; In this case, index 0 holds/has the value 'Hello'; ArrayExample[1] = 'world'; here index 1 has/holds the value 'world'; etc etc. Arrays always start from 0, not 1. For the $_POST global array, this is known as a 'associative array', meaning, instead of the index being a number, it will have a label/name to it. For example: $_POST['myName'], in this case, the index here is 'myName' but instead of being a number like 0, it has a label/name to it called 'myName' and it has/holds a value of someone's name when someone enters their name into the input field in the form: <input type="text" name="myName" />. You can then do this: $firstName = $_POST['myName']; The value in $_POST['myName'] will be set to/stored into $firstName. In your HTML form, make sure to name the 'name' attribute for the input field. Example: <input type="text" name="myName" /> Highlighted in red is the 'name' attribute. When you process the form with PHP, in other to receive the value in $_POST that came from the form's input field with the name attribute of "myName", you have to make sure the name attribute matches with the index of the $_POST array. Remember that $_POST is an associative array, so in this case it will be $_POST['myName']; If they don't match, for example <input type="text" name="fname" /> and in PHP you have this: $_POST['myName']; you'll get the 'undefined index' error message. "fname" and 'myName' don't match. Whatever the user types into here: <input type="text" name="myName" />You retrieve the value in PHP like this: $firstName = $_POST['myName']; Hope this helped.
Sure it does! Thanks very much!

Share this post


Link to post
Share on other sites
It's up to you to build the form with the fields you want to add to the database. It looks like any other HTML form. The insert and update forms can be the same, the update form just needs the data filled in from the database. A select and delete page don't need forms, in fact those would probably be the same page (a page that displays all of the data with a "delete" link next to each row).
Ok, but how can I put this form (in my portfolio?) on my website so that it´s attractive for potential customers?

Share this post


Link to post
Share on other sites

In your portfolio page, you create the form as follows: (This is just a example because I don't know you want as input fields.) Lets say you want people to comment on your website or comment on anything regarding you and your work.. the form can look like this: HTML page:

<form method="post" action="comment.php" ><label for="fName">First Name:</label><input type="text" id="fName" name="firstName" /><br/><label for="lName">Last Name:</label><input type="text" id="lName" name="lastName" /><br/><label for="comment">Comment:</label><textarea id="comment" name="comments"></textarea><br/><input type="submit" value="Submit Comments" /></form>

comment.php can look something like this:

<?php$firstName = $_POST['firstName'];$lastName = $_POST['lastName'];$comments = $_POST['comments'];  $con = mysql_connect("your host here","your username here","your pass here"); if (!$con)  {  die('Could not connect: ' . mysql_error());  } mysql_select_db("my_db", $con);  $sql = "INSERT INTO comments (FirstName, LastName, Comments) VALUES ('$firstName','$lastName','$comments')"; if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }mysql_close($con);?>

Make sure in your database(in the example above, its called 'my_db') that you make a table called comments with columns: FirstName, LastName, Comments. Then once people fill in the form and comment, you can use phpmyadmin to view what people are commenting.

Edited by Don E

Share this post


Link to post
Share on other sites
In your portfolio page, you create the form as follows: (This is just a example because I don't know you want as input fields.) Lets say you want people to comment on your website or comment on anything regarding you and your work.. the form can look like this: HTML page:
<form method="post" action="comment.php" ><label for="fName">First Name:</label><input type="text" id="fName" name="firstName" /><br/><label for="lName">Last Name:</label><input type="text" id="lName" name="lastName" /><br/><label for="comment">Comment:</label><textarea id="comment" name="comments"></textarea><br/><input type="submit" value="Submit Comments" /></form>

comment.php can look something like this:

<?php$firstName = $_POST['firstName'];$lastName = $_POST['lastName'];$comments = $_POST['comments'];  $con = mysql_connect("your host here","your username here","your pass here"); if (!$con)  {  die('Could not connect: ' . mysql_error());  } mysql_select_db("my_db", $con);  $sql = "INSERT INTO comments (FirstName, LastName, Comments) VALUES ('$firstName','$lastName','$comments')"; if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }mysql_close($con);?>

Make sure in your database(in the example above, its called 'my_db') that you make a table called comments with columns: FirstName, LastName, Comments. Then once people file in the form and comment, you can use phpmyadmin to view what people are commenting.

Thanks very much! However, this isn´t what I want! I want to show how a database works using msql queries!

Share this post


Link to post
Share on other sites
Thanks very much! However, this isn´t what I want! I want to show how a database works using msql queries!
it is you just don't realize it........just take the man's word and take the lesson in.

Share this post


Link to post
Share on other sites
Thanks very much! However, this isn´t what I want! I want to show how a database works using msql queries!
then clearly you still don't understand anything that anyone has told you. This is clear example of using a form to submit values to put into a database. Which part of that are you missing? Which part don't you get? Using this example, you could have the same page with the form on also SELECT from the same table being INSERT'd into, to show the comments as well. You really need to learn how to start connecting the dots or else you're never going to get any of this stuff. Edited by thescientist

Share this post


Link to post
Share on other sites
then clearly you still don't understand anything that anyone has told you. This is clear example of using a form to submit values to put into a database. Which part of that are you missing? Which part don't you get? Using this example, you could have the same page with the form on also SELECT from the same table being INSERT'd into, to show the comments as well. You really need to learn how to start connecting the dots or else you're never going to get any of this stuff.
How to set a form like this? Like any other form?

Share this post


Link to post
Share on other sites

i don't know anymore. even when you ask and get handed working example, you don't seem to get it. I'm not sure what else you are expecting from us at this point.

Share this post


Link to post
Share on other sites
i don't know anymore. even when you ask and get handed working example, you don't seem to get it. I'm not sure what else you are expecting from us at this point.
You are too early! I still have to study the examples!

Share this post


Link to post
Share on other sites
Thanks very much! However, this isn´t what I want! I want to show how a database works using msql queries!
You do not show the database (or the queries)... you show the application that uses it (the forms, and the results of PHP).Let's put it like this... Imagine you want to sell cars that use an engine that you built (bare with me...), and in an attempt to sell a car, you want to show people the inner workings of the engine, while the car is working... last I checked, you can't really do that. And showing the engine itself while it's not running doesn't really give the client anything to go by... So instead, to make the sell, you show clients how the car runs using your engine - you're selling the car, not the engine.Similarly, you're selling your skills to build applications that use databases. You're not selling the databases themselves. Clients are not interested in that, because a database is useless without an application to deal with it... just like a car engine is useless by itself, and must instead be part of a larger machine (the car in our example).

Share this post


Link to post
Share on other sites
You do not show the database (or the queries)... you show the application that uses it (the forms, and the results of PHP). Let's put it like this... Imagine you want to sell cars that use an engine that you built (bare with me...), and in an attempt to sell a car, you want to show people the inner workings of the engine, while the car is working... last I checked, you can't really do that. And showing the engine itself while it's not running doesn't really give the client anything to go by... So instead, to make the sell, you show clients how the car runs using your engine - you're selling the car, not the engine. Similarly, you're selling your skills to build applications that use databases. You're not selling the databases themselves. Clients are not interested in that, because a database is useless without an application to deal with it... just like a car engine is useless by itself, and must instead be part of a larger machine (the car in our example).
Thanks! Good explanation!

Share this post


Link to post
Share on other sites

Eduard, I showed you that example to get an idea as to how to insert data in your database table first, then once you understand that, you can take the 'comments' from the database and display them on your site thus 'showing how a database works using mysql queries'. (I think this is what you mean by that)

Share this post


Link to post
Share on other sites
Eduard, I showed you that example to get an idea as to how to insert data in your database table first, then once you understand that, you can take the 'comments' from the database and display them on your site thus 'showing how a database works using mysql queries'. (I think this is what you mean by that)
I apreciate that, but your example and the example of the tutorial insert.php don´t make me understand the error I make!

Share this post


Link to post
Share on other sites
I apreciate that, but your example and the example of the tutorial insert.php don´t make me understand the error I make!
The "undefined index" error? Don E also gave you an explanation for that too. The one about which you said was helpful. Did you not get it (I remind you it's OK if you didn't get it, but pretending you did - only to ask the same question again later - is not OK).

Share this post


Link to post
Share on other sites

I see 2 different html forms!<html><body><form action="insert.php" method="post">Firstname: <input type="text" name="firstname" />Lastname: <input type="text" name="lastname" />Age: <input type="text" name="age" /><input type="submit" /></form></body></html> <form method="post" action="comment.php" ><label for="fName">First Name:</label><input type="text" id="fName" name="firstName" /><br/><label for="lName">Last Name:</label><input type="text" id="lName" name="lastName" /><br/><label for="comment">Comment:</label><textarea id="comment" name="comments"></textarea><br/><input type="submit" value="Submit Comments" /></form>Which one is the right one?

Share this post


Link to post
Share on other sites

Thank you so much, Don E from Michigan. For your definitive, logical explanation of 'indexes' or 'keys' or 'elements' and 'associative array' meanings. I felt like i learned something which was not clear in my mind before.I have a question,You wrote the code look like this in comment.php file, 1#)- $sql = "INSERT INTO comments (FirstName, LastName, Comments) VALUES ('$firstName','$lastName','$comments')"; In php lessons, as far as i know in order to get PHP to execute any statement we must use the mysql_query() function.This function is used to send a query or command to a mysql connection. And i would write the statement something like this. 2#) - db_query("INSERT INTO comments(FirstName, LastName, Comments)VALUES('$firstName' , '$lastName' , '$comments')"); Now i am confused on which way to wright the statement.I would appreciate if you tell me why it is so something like in 1#)

Share this post


Link to post
Share on other sites
Thank you so much, Don E from Michigan. For your definitive, logical explanation of 'indexes' or 'keys' or 'elements' and 'associative array' meanings. I felt like i learned something which was not clear in my mind before.I have a question,You wrote the code look like this in comment.php file, 1#)- $sql = "INSERT INTO comments (FirstName, LastName, Comments) VALUES ('$firstName','$lastName','$comments')"; In php lessons, as far as i know in order to get PHP to execute any statement we must use the mysql_query() function.This function is used to send a query or command to a mysql connection. And i would write the statement something like this. 2#) - db_query("INSERT INTO comments(FirstName, LastName, Comments)VALUES('$firstName' , '$lastName' , '$comments')"); Now i am confused on which way to wright the statement.I would appreciate if you tell me why it is so something like in 1#)
It would be same but if you store the sql in variable you will be able to print it out when you will need to debug so that you can study how your query and its values being evaluated. it will be same when you will pass $sql to mysql_query($sql); Edited by birbal

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...

×
×
  • Create New...