Jump to content

Why it doesn´t work? Is this the solution?


eduard
 Share

Recommended Posts

<html><body><?phpvar_dump($_POST);if(isset($_POST['price']) && isset($_POST['description']) && isset($_POST['quantity']))$con = mysql_connect("localhost","root","root");if (!$con)echo'successful form submission. initialize DB connection...';die('Could not connect: ' . mysql_error());mysql_select_db("Company", $con);$sql="INSERT INTO products (description, price, quantity) VALUES ('{$_POST["description"]}','{$_POST["price"]}','{$_POST["quantity"]}')";echo 'prepare for INSERT: ' . $sql;if (!mysql_query($sql,$con))die('Error: ' . mysql_error());echo 'one of more form fields missing';mysql_close($con);?></body></html><html><head><style type="text/css">#desc{width:10%;height:5%;}</style><title>POST</title></head><body><form action='insert.php' method="post">description: <input type="text" name="description" id="description"><br />price: <input type="text" name="price" id="price" /><br />quantity: <input type="text" name="quantity" id="quantity" /><input type='submit' value='submit' /></form></body></html>Is this the solution?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.CODE<?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';};?>

Edited by eduardchile
Link to comment
Share on other sites

  • Replies 60
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

not only that, but which set of code are you actually presenting to us to review? The HTML form and the second snippet of supplied PHP code?

Edited by thescientist
Link to comment
Share on other sites

So you're saying when you submit the form nothing happens after that point? Does it just go a blank screen or does it throw an error looking for the page? is your script called insert.php? Is everything running somewhere in the webroot? Are your file paths correct?

Edited by thescientist
Link to comment
Share on other sites

So you're saying when you submit the form nothing happens after that point? Does it just go a blank screen or does it throw an error looking for the page? is your script called insert.php? Is everything running somewhere in the webroot? Are your file paths correct?
I wrote ´open the php file´ (so it does!)
Link to comment
Share on other sites

Be aware that all your $_POST values are strings. If your fields expect other kinds of data, like an int, your INSERT will fail.What do your error messages tell you? Are you getting any, or just a blank screen?

Link to comment
Share on other sites

First, you have 2 different pieces of code posted. Which one are you actually running? Second, what specifically happens when you submit the form, what do you see on the page?
1 is insert.php2 is form.htmlAS WRITTEN BEFORE (2), THE PHP FILE OPENS=NOT RUNNING!
Link to comment
Share on other sites

Be aware that all your $_POST values are strings. If your fields expect other kinds of data, like an int, your INSERT will fail.What do your error messages tell you? Are you getting any, or just a blank screen?
No error meesages, only opening insert.php!
Link to comment
Share on other sites

Eduard, the first piece of code you posted combines a HTML form with some PHP code that doesn't look correct. The second piece of code you posted, after someone's comments that you also pasted, is only PHP code. It seems to me like the PHP code that is part of the form should either not be there, or be replaced with the other PHP code (assuming your form is called insert.php).Regardless, whatever happens, you should be getting error messages if the code fails. You're not showing any error messages, you're just saying that the file "opens" and "doesn't run". I usually mean "open" to say "runs", so when you say it opens but doesn't run I don't know what you mean. If it ran, and there was a problem, there should be an error message. Even if it ran without problems there would still be messages telling you what it's doing.So, now that we're 13 posts in and still don't have the actual problem defined, please give a list of steps you take and exactly what you see in the browser.

Link to comment
Share on other sites

Eduard, the first piece of code you posted combines a HTML form with some PHP code that doesn't look correct. The second piece of code you posted, after someone's comments that you also pasted, is only PHP code. It seems to me like the PHP code that is part of the form should either not be there, or be replaced with the other PHP code (assuming your form is called insert.php).Regardless, whatever happens, you should be getting error messages if the code fails. You're not showing any error messages, you're just saying that the file "opens" and "doesn't run". I usually mean "open" to say "runs", so when you say it opens but doesn't run I don't know what you mean. If it ran, and there was a problem, there should be an error message. Even if it ran without problems there would still be messages telling you what it's doing.So, now that we're 13 posts in and still don't have the actual problem defined, please give a list of steps you take and exactly what you see in the browser.
Yes, you´re right! I am too stressed, I have to go I´ll look at it later! (Time is always my problem, but this will change!)
Link to comment
Share on other sites

What´s wrong with this insert.php file? <html><body><?phpvar_dump($_POST);if(isset($_POST['price']) && isset($_POST['description']) && isset($_POST['quantity']))$con = mysql_connect("localhost","root","root");if (!$con)echo'successful form submission. initialize DB connection...';die('Could not connect: ' . mysql_error());mysql_select_db("Company", $con);$sql="INSERT INTO products (description, price, quantity) VALUES ('$_POST[description]','{$_POST[price]}','$_POST[quantity]')";echo 'prepare for INSERT: ' . $sql;if (!mysql_query($sql,$con))die('Error: ' . mysql_error());echo 'one of more form fields missing';mysql_close($con);?></body></html>These are the errors I get:array(0) { } Notice: Undefined variable: con in /Applications/MAMP/htdocs/website/insert.php on line 9successful form submission. initialize DB connection...Could not connect:

Edited by eduardchile
Link to comment
Share on other sites

hasn't this exact same topic already been discussed?http://w3schools.invisionzone.com/index.php?showtopic=37042I absolutely guarantee that a complete answer (actually multiple answers provided by multiple members) has been given to you in that thread. Please try and read it thoroughly and you will find it too, with no more knowledge required then what you already have.

Link to comment
Share on other sites

hasn't this exact same topic already been discussed?http://w3schools.invisionzone.com/index.php?showtopic=37042I absolutely guarantee that a complete answer (actually multiple answers provided by multiple members) has been given to you in that thread. Please try and read it thoroughly and you will find it too, with no more knowledge required then what you already have.
Yes, but that thread is closed by you!
Link to comment
Share on other sites

I'm not a moderator, so that wasn't me. It was justsomeguy, who is a moderator. All I was trying to say is that answer is in there (and I mean literally, as in the post says... 'this is the answer', and explanations for each solution were covered, and what they were doing, plus all the tutorials were cited in the appropriate places..... which means all you have to do is re-read it slowly and carefully, and you'll find the answer to your question.edit: speaking of that thread, did you try any of the advice that justsomeguy gave you?

Eduard, if you really want to learn PHP I recommend you buy and study this book:http://oreilly.com/catalog/9780596006815One of the authors is the creator of PHP, and that book is known as being a great intro for people starting with PHP. You should read that book from cover to cover, more than once if necessary, if you really want to learn this stuff as quickly as possible. You may be able to find a copy in Dutch or Spanish if you prefer.
Edited by thescientist
Link to comment
Share on other sites

I'm not a moderator, so that wasn't me. It was justsomeguy, who is a moderator. All I was trying to say is that answer is in there (and I mean literally, as in the post says... 'this is the answer', and explanations for each solution were covered, and what they were doing, plus all the tutorials were cited in the appropriate places..... which means all you have to do is re-read it slowly and carefully, and you'll find the answer to your question.edit: speaking of that thread, did you try any of the advice that justsomeguy gave you?
Ok, let´s find the answer!
Link to comment
Share on other sites

This error message I get after reading post 201, Explain nr. 3?:Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /Applications/MAMP/htdocs/website/insert.php on line 7<html><body><?phpvar_dump($_POST);if(isset($_POST["price']) && isset($_POST['description']) && isset($_POST['quantity'])){echo "succesful 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 products (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);{echo "one or more form fields missing";};?></body></html>I neither don´t understand the latest echo! (mysql is closed!)

Link to comment
Share on other sites

You have mismatched quotes:if(isset($_POST["price'])These are very simple errors. Even if you don't understand what the errors mean, you can usually find the error by looking at or around the line it points you to. Most (if not all) of the errors you've posted are simple syntax errors.EDIT:I think you might also be missing an 'else' here:mysql_close($con);{echo "one or more form fields missing";};should be:mysql_close($con);} else {echo "one or more form fields missing";};

Edited by ShadowMage
Link to comment
Share on other sites

try posting your code with indents and formatting. It makes it easier for other's to read and will help you see when you have extraneous characters in your script. Also, be mindful that you include error message, confirming the actual line (like this code here) is line 7. If we indent/format your code, let's see how it looks.

<html><body><?phpvar_dump($_POST);if(isset($_POST["price']) && isset($_POST['description']) && isset($_POST['quantity'])){  echo "succesful 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 products (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);{echo "one or more form fields missing";};?></body></html>

it should be clear if you review the actual post that you are referring to.
<?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';};?>

edit: I mean, now we're just talking about copy/paste. Doesn't get much more basic/simpler than that...

Edited by thescientist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×
×
  • Create New...