Jump to content

Explain nr 3?


Recommended Posts

  • Replies 239
  • Created
  • Last Reply

Top Posters In This Topic

Can I now go to point 3b (post 1)? I don´t understand ´view all the data´?
By all means. That's what scientist was trying to explain how to do in this post:
...since we have already gone over inserting, it's merely integrating those steps we have gone over with you, which is1) making a connection to the database2) selecting the table3) creating and executing the query4) *(optional but recommended): printing out some sort of success/failure message to the screenpretty soon you will completed your first step in being a developer of databases. If you have access to phpMyAdmin, after you make a successful request to the database, you will start seeing your submitted form data appearing in the table, and that will certainly be a good feeling. I know it was the first day I did this. I was actually pretty excited, haha.
Link to post
Share on other sites
By all means. That's what scientist was trying to explain how to do in this post:
Thanks! But what means ´view all data´?
Link to post
Share on other sites
Thanks! But what means ´view all data´?
"view all data" means that the data is being sent to the PHP script and you can access it. Like you just did by printing out the description of the product using the $_POST array.
Link to post
Share on other sites
"view all data" means that the data is being sent to the PHP script and you can access it. Like you just did by printing out the description of the product using the $_POST array.
Ok, thanks!
Link to post
Share on other sites

What´s wrong with this file:<html><body><?php$con = mysql_connect("localhost","root","root");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("Company", $con);$sql="INSERT INTO products (description, price, quantity)VALUES('$_POST[descriiption]','$_POST[lprice]','$_POST[quantity]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con)?></body></html>(opened with Safari, saved in localhost (website))

Link to post
Share on other sites

What is the output? Do you get any error messages?EDIT:Wait, I see the error. You're trying to interpolate arrays inside a string:$sql="INSERT INTO products (description, price, quantity)VALUES('$_POST[descriiption]','$_POST[lprice]','$_POST[quantity]')";Try this:$sql="INSERT INTO products (description, price, quantity)VALUES('{$_POST['descriiption']}','{$_POST['price']}','{$_POST['quantity']}')";Notice the curly brackets around the entire thing and the single quotes around the indexes/keys. Oh, you also had 'lprice' instead of 'price'

Edited by ShadowMage
Link to post
Share on other sites

I did the changes! Do I have to write ´VALUES´? And I have no connection to my database, because there is no error message!

Link to post
Share on other sites
And I have no connection to my database, because there is no error message!
What? If you didn't have a connection, there would be an error. That's what this checks for:
$con = mysql_connect("localhost","root","root");if (!$con){die('Could not connect: ' . mysql_error());}

Link to post
Share on other sites

yes, you have to do it like it's supposed to be done.http://www.w3schools.com/php/php_mysql_insert.aspsyntax exists for a reason.and also, you can added custom feedback, it's a big part of what we've been trying to teach you, so you can deduce these things for yourself. We've given you examples before.... i.e.

<?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);$sql="INSERT INTO products (description, price, quantity)VALUES('$_POST[descriiption]','$_POST[lprice]','$_POST[quantity]')";if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}else{echo "1 record added";  // this needs to be PART of the if/else, or else it will always say what you tell it to say, regardless of any kind of success/failure}mysql_close($con)?>

this is the whole point of conditional statements. You write them to test for certain things, or values. If something meets the criteria, then have it tell you one thing. If doesn't meet the criteria, then have it say something else. Pretty simple and it's absolutely necessary for you to be able to know how to do this on your own.

Edited by thescientist
Link to post
Share on other sites
What? If you didn't have a connection, there would be an error. That's what this checks for:
$con = mysql_connect("localhost","root","root");if (!$con){die('Could not connect: ' . mysql_error());}

But I can open this file with Safari?
Link to post
Share on other sites
But I can open this file with Safari?
You should know by now that you don't open PHP files directly with a browser, you open the browser and go to the URL of the PHP script that you want the web server to execute.
Link to post
Share on other sites

What does this error message mean:database connection initialized...Notice: Undefined index: description in /Applications/MAMP/htdocs/website/insert.php on line 19Notice: Use of undefined constant price - assumed 'price' in /Applications/MAMP/htdocs/website/insert.php on line 19Notice: Undefined index: price in /Applications/MAMP/htdocs/website/insert.php on line 19Notice: Use of undefined constant quantity - assumed 'quantity' in /Applications/MAMP/htdocs/website/insert.php on line 19Notice: Undefined index: quantity in /Applications/MAMP/htdocs/website/insert.php on line 191 record added<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);$sql="INSERT INTO products (description, price, quantity)VALUES('$_POST{[description]}','{$_POST[price]}','{$_POST[quantity]}')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con)?></body></html>

Link to post
Share on other sites
'$_POST{[description]}','{$_POST[price]}','{$_POST[quantity]}'
if you wan to interpolate the array it should be some thing like {$_POST['price']}in associative array you have to specify the key in quotes. in your code quotes were missing so php parser thoughts its a constant. but there is no such constant defined so it at last assumed it as a string. but it also throws you a notice.and for this
Notice: Undefined index: price in /Applications/MAMP/htdocs/website/insert.php on line 19
probably there is no such keys name as price in $_POST array so it is showing this.
Link to post
Share on other sites

This I don´t understand:database connection initialized...Notice: Undefined variable: sql in /Applications/MAMP/htdocs/website/insert.php on line 20Error: Query was emptyWhere do I have to fill in the query? In a form.html?<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);"INSERT INTO products (description, price, quantity)VALUES('$_POST{['description']}','$_POST{['price']}','$_POST{['quantity']}')";if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}echo "1 record added";mysql_close($con)?></body></html>

Link to post
Share on other sites
This I don´t understand:database connection initialized...Notice: Undefined variable: sql in /Applications/MAMP/htdocs/website/insert.php on line 20Error: Query was empty
That error message should be pretty obvious. The variable $sql is not defined. You should be able to find the error.This is what you had before:$sql="INSERT INTO products (description, price, quantity)VALUES('$_POST{[description]}','{$_POST[price]}','{$_POST[quantity]}')";if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}This is what you have now:"INSERT INTO products (description, price, quantity)VALUES('$_POST{['description']}','$_POST{['price']}','$_POST{['quantity']}')";if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}Find the difference. Why do you change things that aren't broken? Edited by ShadowMage
Link to post
Share on other sites

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

Edited by thescientist
Link to post
Share on other sites
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...

I HAVE NEVER BLAMED YOU!
Link to post
Share on other sites

maybe blame was too strong, but you certainly give people a lot of grief for trying to help you at times.

Link to post
Share on other sites
maybe blame was too strong, but you certainly give people a lot of grief for trying to help you at times.
But I never have written that it´s someone else cause that I didn´t manage, because it isn´t!However, if I´m frustrated (and I was it many times!), I don´t react nice! (But you know me by now!)
Link to post
Share on other sites
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...

I wrote the tutorials are not clear and although it´s daylight it´s very hard to read ´{´or ´[´ (I also wrote!)
Link to post
Share on other sites
I wrote the tutorials are not clear and although it´s daylight it´s very hard to read ´{´or ´[´ (I also wrote!)
We've personally explained things to you many times, yet you still have trouble understanding....Anyway,Did you get the error fixed? As scientist pointed out, this code should be added to the code you've been working with throughout this thread.
Link to post
Share on other sites

I still get an error message which I don´t understand!Parse error: syntax error, unexpected T_STRING in /Applications/MAMP/htdocs/website/insert.php on line 21<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 post
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....

Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...