Jump to content

Explain nr 3?


eduard

Recommended Posts

also, you do realize that 1) your script needs to be combined with all the work we've just done, which is you are getting all the undefined indexes. These two scripts alone will not work independently, which is why I gave you this outline a few posts ago.
if(isset($_POST['description']) && isset($_POST['price']) && isset($_POST['quantity'])){echo 'all fields submitted. do some stuff here';}else{echo 'invalid form submission. field missing';};

The whole point is you submit a form, check that all the fields have been submitted, and if they have, you write the database connection code within the conditional. Without a form submitting data, $_POST['description'] and the rest are useless and will cause your code to break.2) without the else conditional here

if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}echo "1 record added";

it's always going to say 1 record added, no matter what. You should really learn about conditional statements (if/else). This is basic, basic stuff. You need to force your code to branch it's logic basic on successful or un-successful behavior. If you have an error executing the query, then you show a failure message, ELSE, you can show a success message.and stop breaking working code examples. You shouldn't be trying to modify things you don't understand until you can even get a basic working example to work on your own first. This is why we stress so much that you really try and read the tutorials and understand the basics individually before trying to thrust yourself into the heart of it. You keep saying you're a beginner and that you don't have our level experience, you're stressed, blah blah blah. Well yeah, you don't, and you won't if you don't try and learn anything in a meaningful way. We tell you to read the tutorials, you say you do, and then you can't figure out why something is working even when it tells you are missing a ) or ;. If you just tried to listen to us, you would understand we're just trying to make this easier on yourself, but you always blame us for your inability to comprehend and that's really not our fault. If you're having this much trouble with the basics...

TYPICAL A REPLY OF SOMEONE WHO HAS NEVER BEEN IN A VERY DIFFICULT SITUATION!
Link to comment
Share on other sites

  • Replies 239
  • Created
  • Last Reply

look, you're the one making this personal. You came here looking to become a professional developer, so we're going to treat you to a higher standard. For a lot of us here this is our life, and we're taking ample amounts of time and energy out of OUR day's to try and help you (as we've spent ample amounts of time and energy on our own career/educational endeavors). You should be grateful for the amount of time we've spent with you considering how little we've actually gone over in the grand scheme of professional web development for the amount of time you've been on here asking questions. A lot of people come here asking for less and we give them nothing to very little in return because they aren't willing to or don't show the motivation to learn. I think the fact you are willing to generalize about people's life situations that you know nothing about is very narrow-minded and say's a lot about you. I am truely sorry for whatever life situation you are in, but you are alive, you (mostly) seem to have your health, and for that alone you should be grateful. You can generalize about my life if you think that's of your concern, but that's not what I am about. If you have your mind then that's all you need to bring to the table in order to participate and get everything you need out of this place. If you find it necessary to delve outside that, then that's your priority. If you want a physical handicap to dictate your mental abilities, that is also your priority, but as you've never mentioned that you have a learning disability, then it doesn't seem necessary for us to assume that you can't learn IF you're willing to. As far as I am concerned the greater ambition of civilized society is treat everyone equal, regardless of race, religion, orientation, physical/mental (dis)abilities, etc.However, if you're willing to stick to what you came here for, which is learning a new skill and advancing yourself as a web developer, then you have to realize there are certain standards that we will hold you to. We will be honest. We won't sugar coat it if we feel you aren't getting it. Tough love is always a last resort, so if it means that we're at that point, then something in the student/teacher(s) relationship has caused it to be that way. You can decide your own future, regardless of what's happened in your past. Make it your own, or don't. The choice is yours.

Link to comment
Share on other sites

Why did you change it again?! :)This is what you should have:$sql="INSERT INTO products (description, price, quantity)VALUES('{$_POST['description']}','{$_POST['price']}','{$_POST['quantity']}')";if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());} else {echo "Record added";}When we tell you to make a change, don't re-write your whole code, just make the change we tell you to....and please try to learn and understand what we tell you when we explain things....
You know what the problem is! (also this I have written it before!) Your replies differ from the tutorials, so I get confused > more questions E. g. your reply is different from which is written in the SQLInsert tutorial!
Link to comment
Share on other sites

You know what the problem is! (also this I have written it before!) Your replies differ from the tutorials, so I get confused > more questions E. g. your reply is different from which is written in the SQLInsert tutorial!
so let's re-write it then. if you do this, what results do you get? and reply with code and any output.note: as mentioned before, this should be integrated into the previous form handling script we've been working on. so let me show you what we mean.
<?phpvar_dump($_POST);if(isset($_POST['price']) && isset($_POST['description']) && isset($_POST['quantity'])){  echo 'successful form submission. initialize DB connection...';   $con = mysql_connect("localhost","root","root");  if (!$con){	die('Could not connect: ' . mysql_error());  };  echo 'connection initialized...';   mysql_select_db("Company", $con);    $sql="INSERT INTO Persons (description, price, quantity) VALUES  ('$_POST[description]','$_POST[price]','$_POST[quantity]')";  echo 'prepare for INSERT: ' . $sql;  if (!mysql_query($sql,$con)){	die('Error: ' . mysql_error());  }else{	echo "1 record added";  };  mysql_close($con);}else{  echo 'one of more form fields missing';};?>

Link to comment
Share on other sites

Why did you change it again?! :)This is what you should have:$sql="INSERT INTO products (description, price, quantity)VALUES('{$_POST['description']}','{$_POST['price']}','{$_POST['quantity']}')";if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());} else {echo "Record added";}When we tell you to make a change, don't re-write your whole code, just make the change we tell you to....and please try to learn and understand what we tell you when we explain things....
Did you read the reply of thescientist: WITHOUT the if condition! I hope you understand now why I get confused, stressed, frustrated, etc.). Also the error messenger of MAMP differs from your replies!
Link to comment
Share on other sites

You know what the problem is! (also this I have written it before!) Your replies differ from the tutorials, so I get confused > more questions E. g. your reply is different from which is written in the SQLInsert tutorial!
Not really. Besides, very, very few real-life situations will be exactly like the tutorials. Or even relatively close. If you get lost that easily when even the smallest changes are made, you are going to have a very hard time learning this stuff.
Link to comment
Share on other sites

Not really. Besides, very, very few real-life situations will be exactly like the tutorials. Or even relatively close. If you get lost that easily when even the smallest changes are made, you are going to have a very hard time learning this stuff.
I don´t think so, because now I finally know (but it is nowhere written in the tutorials!)
Link to comment
Share on other sites

So far I got:database connection initialized...Notice: Undefined index: description in /Applications/MAMP/htdocs/website/insert.php on line 19Notice: Undefined index: price in /Applications/MAMP/htdocs/website/insert.php on line 19Notice: Undefined index: quantity in /Applications/MAMP/htdocs/website/insert.php on line 19<html><body><?php$con = mysql_connect("localhost","root","root");if (!$con){die('Could not connect: ' . mysql_error());}else{echo 'database connection initialized...'; //add else statement so you can know if it connected};mysql_select_db("Company", $con);mysql_query("INSERT INTO products (description, price, quantity)VALUES ('{$_POST['description']}','{$_POST['price']}','{$_POST['quantity']})");mysql_close($con)?></body></html>

Link to comment
Share on other sites

You know what the problem is! (also this I have written it before!) Your replies differ from the tutorials, so I get confused > more questions E. g. your reply is different from which is written in the SQLInsert tutorial!
I don't get it. You came asking for questions so we show you the tutorials and try and step through them with you. You then complain you don't get the tutorials because they aren't clear and easy to understand. So we try and work around that and then you complain that you don't understand what we're trying to explain because it's not in the tutorials. argh.you can't have it both ways. The tutorials are practically copy/paste code examples. All we're trying to do is explain them in depth, yet without a fundamental grasp on the basics of programming, you have to understand you're going to be at a disadvantage either way. If you understood the basics, I doubt this would be as challenging for all parties involved. But you don't want to take it slow because you're in a hurry to learn, but you aren't making the attempt to learn in a meaningful way. I hope you can understand the difficulty of the situation you're presenting to us because we ARE trying to help you. We're just running out of ways to explain the simplicity of requiring semi-colons at the end of your statements and that your curly braces and parenthesis always need to match... :)
Link to comment
Share on other sites

So far I got:database connection initialized...Notice: Undefined index: description in /Applications/MAMP/htdocs/website/insert.php on line 19Notice: Undefined index: price in /Applications/MAMP/htdocs/website/insert.php on line 19Notice: Undefined index: quantity in /Applications/MAMP/htdocs/website/insert.php on line 19<html><body><?php$con = mysql_connect("localhost","root","root");if (!$con){die('Could not connect: ' . mysql_error());}else{echo 'database connection initialized...'; //add else statement so you can know if it connected};mysql_select_db("Company", $con);mysql_query("INSERT INTO products (description, price, quantity)VALUES ('{$_POST['description']}','{$_POST['price']}','{$_POST['quantity']})");mysql_close($con)?></body></html>
this has already been explained to you three times in the last page of this thread. :)
probably there is no such keys name as price in $_POST array so it is showing this.
also, you do realize that 1) your script needs to be combined with all the work we've just done, which is you are getting all the undefined indexes. These two scripts alone will not work independently, which is why I gave you this outline a few posts ago.
if(isset($_POST['description']) && isset($_POST['price']) && isset($_POST['quantity'])){echo 'all fields submitted. do some stuff here';}else{echo 'invalid form submission. field missing';};

The whole point is you submit a form, check that all the fields have been submitted, and if they have, you write the database connection code within the conditional. Without a form submitting data, $_POST['description'] and the rest are useless and will cause your code to break.

As scientist pointed out, this code should be added to the code you've been working with throughout this thread.
and you've been given an example of how to do so....
so let's re-write it then. if you do this, what results do you get? and reply with code and any output.note: as mentioned before, this should be integrated into the previous form handling script we've been working on. so let me show you what we mean.
<?phpvar_dump($_POST);if(isset($_POST['price']) && isset($_POST['description']) && isset($_POST['quantity'])){  echo 'successful form submission. initialize DB connection...';   $con = mysql_connect("localhost","root","root");  if (!$con){	die('Could not connect: ' . mysql_error());  };  echo 'connection initialized...';   mysql_select_db("Company", $con);    $sql="INSERT INTO Persons (description, price, quantity) VALUES  ('$_POST[description]','$_POST[price]','$_POST[quantity]')";  echo 'prepare for INSERT: ' . $sql;  if (!mysql_query($sql,$con)){	die('Error: ' . mysql_error());  }else{	echo "1 record added";  };  mysql_close($con);}else{  echo 'one of more form fields missing';};?>

wtf....
Link to comment
Share on other sites

I don't get it. You came asking for questions so we show you the tutorials and try and step through them with you. You then complain you don't get the tutorials because they aren't clear and easy to understand. So we try and work around that and then you complain that you don't understand what we're trying to explain because it's not in the tutorials. argh.you can't have it both ways. The tutorials are practically copy/paste code examples. All we're trying to do is explain them in depth, yet without a fundamental grasp on the basics of programming, you have to understand you're going to be at a disadvantage either way. If you understood the basics, I doubt this would be as challenging for all parties involved. But you don't want to take it slow because you're in a hurry to learn, but you aren't making the attempt to learn in a meaningful way. I hope you can understand the difficulty of the situation you're presenting to us because we ARE trying to help you. We're just running out of ways to explain the simplicity of requiring semi-colons at the end of your statements and that your curly braces and parenthesis always need to match... :)
Yes, but read one of my previous replies please? Your replies differ from which is written in the tutorials and I got a reply: ´Not really´ then they differ. For an experienced someone isn´t that a problem, for someone (like me!) it certainly is!
Link to comment
Share on other sites

Your replies differ from which is written in the tutorials
You need to understand that the tutorials are not the only, or even the best, way to do something. The tutorials show examples, they are not the holy grail definition of how to do a certain thing. For the vast, vast majority of what you want to do, there will be no tutorial for it. You will need to understand the concepts, and how to apply those concepts to the problem you're trying to solve. A tutorial can't do that. The only thing a tutorial can do is show examples of specific ways to solve specific problems. There are many, many other ways to solve any given problem.You said several times that you have problems following the tutorials, so it makes sense for people to try and explain something in a different way. You need to realize that, you need to realize that the tutorials are only small examples which illustrate specific concepts. The tutorial on SQL inserts does not show the only way to do an insert, it shows a single way to do an insert just to illustrate what doing an insert does. Look at this, here's a completely different way to do an insert:
$db = new mysqli("localhost", "user", "pass", "db");$query = $db->prepare("INSERT INTO reg (username, email, ip, date, external_id, spammer) VALUES (?, ?, ?, ?, ?, ?)");$query->bind_param("sssiii", $_GET["username"], $_GET["email"], $_GET["ip"], strtotime($_GET["date"]), $external_id, $spammer_bool);$query->execute();

The tutorial doesn't cover anything like that, but it works. In my applications, I do it like this:

$db = new lms_database($config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name']);$db->insert('reg', array(  'username' => $_GET['username'],  'email' => $_GET['email'],  'ip' => $_GET['ip'],  'date' => strtotime($_GET['date']),  'external_id' => $external_id,  'spammer' => $spammer_bool));

Three completely different ways to do an insert, and all of them work. The point of the insert tutorial is not to show you the one and only way to insert data into a database, it's to get you familiar with the concept of inserting data into a database, what happens when you do it, why to do it, etc, not show you the only way possible to do it. If you are focusing only on the code and not the concepts, then maybe that's why you're having a hard time. I mean, look at this tutorial:http://www.w3schools.com/php/php_looping_for.aspThat's about looping through an array, and right there it shows you 2 ways to do it. You should be able to read through that tutorial and at the end understand that if you want to loop through an array, you have a few options. It's not important to memorize all of the options, just to understand the concept of looping.

Link to comment
Share on other sites

You need to understand that the tutorials are not the only, or even the best, way to do something. The tutorials show examples, they are not the holy grail definition of how to do a certain thing. For the vast, vast majority of what you want to do, there will be no tutorial for it. You will need to understand the concepts, and how to apply those concepts to the problem you're trying to solve. A tutorial can't do that. The only thing a tutorial can do is show examples of specific ways to solve specific problems. There are many, many other ways to solve any given problem.You said several times that you have problems following the tutorials, so it makes sense for people to try and explain something in a different way. You need to realize that, you need to realize that the tutorials are only small examples which illustrate specific concepts. The tutorial on SQL inserts does not show the only way to do an insert, it shows a single way to do an insert just to illustrate what doing an insert does. Look at this, here's a completely different way to do an insert:
$db = new mysqli("localhost", "user", "pass", "db");$query = $db->prepare("INSERT INTO reg (username, email, ip, date, external_id, spammer) VALUES (?, ?, ?, ?, ?, ?)");$query->bind_param("sssiii", $_GET["username"], $_GET["email"], $_GET["ip"], strtotime($_GET["date"]), $external_id, $spammer_bool);$query->execute();

The tutorial doesn't cover anything like that, but it works. In my applications, I do it like this:

$db = new lms_database($config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name']);$db->insert('reg', array(  'username' => $_GET['username'],  'email' => $_GET['email'],  'ip' => $_GET['ip'],  'date' => strtotime($_GET['date']),  'external_id' => $external_id,  'spammer' => $spammer_bool));

Three completely different ways to do an insert, and all of them work. The point of the insert tutorial is not to show you the one and only way to insert data into a database, it's to get you familiar with the concept of inserting data into a database, what happens when you do it, why to do it, etc, not show you the only way possible to do it. If you are focusing only on the code and not the concepts, then maybe that's why you're having a hard time. I mean, look at this tutorial:http://www.w3schools.com/php/php_looping_for.aspThat's about looping through an array, and right there it shows you 2 ways to do it. You should be able to read through that tutorial and at the end understand that if you want to loop through an array, you have a few options. It's not important to memorize all of the options, just to understand the concept of looping.

I totally agree with you! But what if the concepts (very important!) aren´t good c. q. clear?
Link to comment
Share on other sites

Then you ask questions, we give you answers, and you don't complain that our answers are different from the unclear tutorials. If you have questions about our answers, then you ask those too and we answer them. We repeat the process until you don't have any more questions.I'll tell you right now a lot of the tutorials on the site aren't very good. We expect questions. Just don't complain if our answers are not given in the tutorials. They leave out a lot of information. There is a lot of information to cover, and they only have a little bit of space to try and explain it. That's what a tutorial is, it's a brief explanation, not an in-depth analysis.

Link to comment
Share on other sites

You need to understand that the tutorials are not the only, or even the best, way to do something. The tutorials show examples, they are not the holy grail definition of how to do a certain thing. For the vast, vast majority of what you want to do, there will be no tutorial for it. You will need to understand the concepts, and how to apply those concepts to the problem you're trying to solve. A tutorial can't do that. The only thing a tutorial can do is show examples of specific ways to solve specific problems. There are many, many other ways to solve any given problem.You said several times that you have problems following the tutorials, so it makes sense for people to try and explain something in a different way. You need to realize that, you need to realize that the tutorials are only small examples which illustrate specific concepts. The tutorial on SQL inserts does not show the only way to do an insert, it shows a single way to do an insert just to illustrate what doing an insert does. Look at this, here's a completely different way to do an insert:
$db = new mysqli("localhost", "user", "pass", "db");$query = $db->prepare("INSERT INTO reg (username, email, ip, date, external_id, spammer) VALUES (?, ?, ?, ?, ?, ?)");$query->bind_param("sssiii", $_GET["username"], $_GET["email"], $_GET["ip"], strtotime($_GET["date"]), $external_id, $spammer_bool);$query->execute();

The tutorial doesn't cover anything like that, but it works. In my applications, I do it like this:

$db = new lms_database($config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name']);$db->insert('reg', array(  'username' => $_GET['username'],  'email' => $_GET['email'],  'ip' => $_GET['ip'],  'date' => strtotime($_GET['date']),  'external_id' => $external_id,  'spammer' => $spammer_bool));

Three completely different ways to do an insert, and all of them work. The point of the insert tutorial is not to show you the one and only way to insert data into a database, it's to get you familiar with the concept of inserting data into a database, what happens when you do it, why to do it, etc, not show you the only way possible to do it. If you are focusing only on the code and not the concepts, then maybe that's why you're having a hard time. I mean, look at this tutorial:http://www.w3schools.com/php/php_looping_for.aspThat's about looping through an array, and right there it shows you 2 ways to do it. You should be able to read through that tutorial and at the end understand that if you want to loop through an array, you have a few options. It's not important to memorize all of the options, just to understand the concept of looping.

AND THIS THE FIRST TIME I MAKE AN INSERT.PHP FILE!
Link to comment
Share on other sites

You don't need to define them, you need to check if they are defined before you use them. You can use isset to check that. That's what this line does:

if (isset($_POST['price']) && isset($_POST['description']) && isset($_POST['quantity'])){

That way the code only runs if those three things are defined, so you avoid the error when they aren't. In practical terms, that means to only run the code if they submitted the form. If they didn't submit the form, don't run the code.

Link to comment
Share on other sites

The error messages are clear in my insert.php file:Undefined indexesBut how do I define them?
as has been explained; by submitting the form. the form submits data to the script and populates the $_POST array. When you don't do this, $_POST is not populated, and thus you get undefined indexes. Understand the flow. 1) form is filled out and submitted2) script checks $_POST for existence of certain members (description, price, quantity)3) if they are there, do something. In this case, execute an INSERT statement to the database.re-read out posts, this is all there.
Link to comment
Share on other sites

You don't need to define them, you need to check if they are defined before you use them. You can use isset to check that. That's what this line does:
if (isset($_POST['price']) && isset($_POST['description']) && isset($_POST['quantity'])){

That way the code only runs if those three things are defined, so you avoid the error when they aren't. In practical terms, that means to only run the code if they submitted the form. If they didn't submit the form, don't run the code.

But in my phpMyAdmin I have 11 times: description=0, price=0, quantity=0!How can I input other values?
Link to comment
Share on other sites

as has been explained; by submitting the form. the form submits data to the script and populates the $_POST array. When you don't do this, $_POST is not populated, and thus you get undefined indexes. Understand the flow. 1) form is filled out and submitted2) script checks $_POST for existence of certain members (description, price, quantity)3) if they are there, do something. In this case, execute an INSERT statement to the database.re-read out posts, this is all there.
Do I have to execute the INSERT statement in phpMyAdmin?
Link to comment
Share on other sites

But in my phpMyAdmin I have 11 times: description=0, price=0, quantity=0!How can I input other values?
By actually submitting the form and using the values you submitted.The reason you have 0's is because when a variable is undefined in PHP an empty value is used. So your POST values will be inserted into the query string as this:"INSERT INTO table (description, price, quantity) VALUES ('', '', '')"Empty strings.**Cringing as I break open a whole new can o' worms...**The fact that they are all 0's concerns me a little because it implies that all of your fields in the table are defined as integer fields. If that is the case, you have a problem. You will not be able to insert a description into an integer field because a description will be text.
Link to comment
Share on other sites

Do I have to execute the INSERT statement in phpMyAdmin?
#$%@#! :) are you asking these questions on purpose to annoy us? or is this really that hard for you? the whole point of writing this script is so that you DON'T have to do it in phpMyAdmin. The whole point of this ridiculously long thread has been to show you how to automate the process. You seriously just aren't reading anything anymore, or you're being stubbron and refusing to read posts by certain members because full explanations with code have been given to you. I'm really at a loss as to what to say anymore in light of this question.
Link to comment
Share on other sites

#$%@#! :) are you asking these questions on purpose to annoy us? or is this really that hard for you? the whole point of writing this script is so that you DON'T have to do it in phpMyAdmin. The whole point of this ridiculously long thread has been to show you how to automate the process. You seriously just aren't reading anything anymore, or you're being stubbron and refusing to read posts by certain members because full explanations with code have been given to you. I'm really at a loss as to what to say anymore in light of this question.
First, when I work c. q. study I´m always serious!Second, I also wrote that I haven´t much time to study! Therefore, I receive too many replies which I can´t read well! I asked for 1 (or 2 or 3) persons, but that isn´t possible, because this is an ´open´ forum!
Link to comment
Share on other sites

By actually submitting the form and using the values you submitted.The reason you have 0's is because when a variable is undefined in PHP an empty value is used. So your POST values will be inserted into the query string as this:"INSERT INTO table (description, price, quantity) VALUES ('', '', '')"Empty strings.**Cringing as I break open a whole new can o' worms...**The fact that they are all 0's concerns me a little because it implies that all of your fields in the table are defined as integer fields. If that is the case, you have a problem. You will not be able to insert a description into an integer field because a description will be text.
What do you mean by ´using the values you submitted´? Where? In phpMyAdmin? Also if I fill in onlu numbers (fpr price and quantity) I get also 0!
Link to comment
Share on other sites

Your PHP code is supposed to get the values from the form and add them to a SQL query to insert them into the database. This has nothing to do with phpMyAdmin.What you do need to use phpMyAdmin for is to open your table and check your fields. What is the data type of each field? Your description field should be something like varchar or text, and the price and quantity fields can be float, double, or decimal fields.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...