Jump to content

Again data don´t go into my db?


eduard
 Share

Recommended Posts

I still don´t understand undefined index? First. I had it without ´isset´! <?php $con = mysql_connect("localhost","root","usbw") or die(mysql_error()); echo "Connected to MySQL";mysql_select_db("website")or die(mysql_error());echo "Connected to Database";if (isset($_POST['value'])){ // Instructions if $_POST['value'] exist}$var1=$_POST["Site_Name"];$var2=$_POST["Site_URL"];$var3=$_POST["Description"];$sql="INSERT INTO links (Site_Name, Site_URL, Description)VALUES('$var1','$var2','$var3 ')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con);?>

Link to comment
Share on other sites

You get an undefined index error if you try to do something like this: $var1=$_POST["Site_Name"]; and $_POST["Site_Name"] doesn't exist. The index is "Site_Name", and the undefined index means that the $_POST array does not have an index called "Site_Name".

Link to comment
Share on other sites

You get an undefined index error if you try to do something like this: $var1=$_POST["Site_Name"]; and $_POST["Site_Name"] doesn't exist. The index is "Site_Name", and the undefined index means that the $_POST array does not have an index called "Site_Name".
Thanks very much for your clear explanation!
Link to comment
Share on other sites

Fixed: <?php $con = mysql_connect("localhost","root","usbw") or die(mysql_error()); echo "Connected to MySQL";mysql_select_db("website")or die(mysql_error());echo "Connected to Database";if (isset($_POST['value'])){ // Instructions if $_POST['value'] exist}$var1=$_POST;$var2=$_POST;$var3=$_POST;$sql="INSERT INTO links (Site_Name, Site_URL, Description)VALUES('$var1','$var2','$var3 ')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con);?>

Link to comment
Share on other sites

Why do the data filled in my insert.html not go to my db? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang='en' xml:lang='en' xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>database</title></head><body><form action="insert.php" method="POST"><label for="Site_Name">Site_Name:</label><input type="text" name="Site_Name" id="Site_Name"/><label for="Site_URL">Site_URL:</label><input type="text" name="Site_URL" id="Site_URL"/><label for="Description">Description:</label><input type="text" name="Description" id="Description"/><input type="submit" value="Add Link"/></form></body></html> <?php $con = mysql_connect("localhost","root","usbw") or die(mysql_error());mysql_select_db("website")or die(mysql_error());if (isset($_POST['value'])){ // Instructions if $_POST['value'] exist}$var1=$_POST;$var2=$_POST;$var3=$_POST;$sql="INSERT INTO links (Site_Name, Site_URL, Description)VALUES('$var1','$var2','$var3 ')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con);?>

Link to comment
Share on other sites

Let's keep this to 1 thread, this is the same issue. The reason the data doesn't go into your database is because $var1, $var2, and $var3 are all set to copies of the $_POST array, not individual things in the array. If you want to see what the array contains then use print_r: echo 'POST: ';print_r($_POST);

Link to comment
Share on other sites

That definitely fixed the undefined index error. Now you have 3 variables set to copies of $_POST, but at least there's no undefined index.
I don´t understand it! Do I´ve to remove $_POST definitively?
Link to comment
Share on other sites

No, you don't. You need to access a certain index in $_POST. The problem is that you're not accessing the correct one, or that you're not checking if it exists first. Print out $_POST like I showed above, maybe the item you're looking for exists but you're just not using the correct index. If you saw what $_POST contains then maybe you could figure out why you're not using the correct index. Printing out the data you're trying to use is an important part of debugging, don't assume that $_POST contains certain data if you haven't verified it. Print it out to see exactly what is there, and match up what you see with the HTML of your form and what you're trying to get in PHP. Your code in post 1 is more correct than your code in post 4, you just need to do some debugging to figure out what the data is that you're working with. One hint is that your PHP code does not check if the form was submitted, it just tries to access items in $_POST. Those will only be there if the form was submitted, but the PHP code doesn't check for that before it tries to get things from $_POST.

Link to comment
Share on other sites

No, you don't. You need to access a certain index in $_POST. The problem is that you're not accessing the correct one, or that you're not checking if it exists first. Print out $_POST like I showed above, maybe the item you're looking for exists but you're just not using the correct index. If you saw what $_POST contains then maybe you could figure out why you're not using the correct index. Printing out the data you're trying to use is an important part of debugging, don't assume that $_POST contains certain data if you haven't verified it. Print it out to see exactly what is there, and match up what you see with the HTML of your form and what you're trying to get in PHP. Your code in post 1 is more correct than your code in post 4, you just need to do some debugging to figure out what the data is that you're working with. One hint is that your PHP code does not check if the form was submitted, it just tries to access items in $_POST. Those will only be there if the form was submitted, but the PHP code doesn't check for that before it tries to get things from $_POST.
I don´t get it! <?php$con = mysql_connect("localhost","root","usbw") or die(mysql_error());mysql_select_db("website", $con);$Site_Name=sname$Site_URL=surl$Description=description $sql="INSERT INTO links (Site_Name, Site_URL, Description)VALUES('$_POST[sname] ' , ' $_POST[surl] ' , ' $_POST[description] ' ) " ;if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con);?> How do you use print_r($_POST)?
Link to comment
Share on other sites

Put it anywhere in that code and it will print the structure of the array.
I think I got it! Do I get as error ´undefined index´ because this php file doesn´t work on its own, but together with the html form where I fill in the data?
Link to comment
Share on other sites

Again, you get an undefined index error any time you try to access an index that does not exist in an array. $_POST is an array, and $_POST only contains indexes if a form was submitted via post. So if you try to access items in $_POST in a script that is not processing a form then you will get that error.

Link to comment
Share on other sites

Again, you get an undefined index error any time you try to access an index that does not exist in an array. $_POST is an array, and $_POST only contains indexes if a form was submitted via post. So if you try to access items in $_POST in a script that is not processing a form then you will get that error.
Your reply is clear! Where do I study that?
Link to comment
Share on other sites

The $_POST[] array only will get values if a <form> was sent with <input> elements that have that value in them. If a form was not sent to the page, $_POST will not have anything in it, therefore if you try to access $_POST['Site_Name'] it will not exist.(undefined)

Link to comment
Share on other sites

Your reply is clear! Where do I study that?
oh man, good question! if only there was website (or a bunch of websites) that offered tutorials on how to use arrays, forms, and everything else you've ever asked for? Oh wait, there is...http://www.w3schools.com/php/default.asphttp://www.google.com/ And even though you say don't understand it, every example anyone has given you is pretty much exactly what you would get out of the tutorials. So, i don't really think it's a fair arguement on your part.
Link to comment
Share on other sites

oh man, good question! if only there was website (or a bunch of websites) that offered tutorials on how to use arrays, forms, and everything else you've ever asked for? Oh wait, there is...http://www.w3schools...php/default.asphttp://www.google.com/ And even though you say don't understand it, every example anyone has given you is pretty much exactly what you would get out of the tutorials. So, i don't really think it's a fair arguement on your part.
Array is simple to find! But don´t I need a tutorial in which array and $_POST are both explained? Like the reply of Ingolme - good, but I don´t understand it! (need php for dummies! Edited by eduardlid
Link to comment
Share on other sites

The $_POST[] array only will get values if a <form> was sent with <input> elements that have that value in them. If a form was not sent to the page, $_POST will not have anything in it, therefore if you try to access $_POST['Site_Name'] it will not exist.(undefined)
They are exactly the same: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang='en' xml:lang='en' xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>database</title></head><body><form action="insert.php" method="POST">Site_Name: <input type="text" name="sname"/>Site_URL: <input type="text" name="surl"/>Description: <input type="text" name="description"/><input type="submit" value="Add Link"/></form></body></html> <?php$con = mysql_connect("localhost","root","usbw") or die(mysql_error());mysql_select_db("website", $con);$links=array("sname","surl","description");$sql="INSERT INTO links (Site_Name, Site_URL, Description)VALUES('$_POST[sname] ' , ' $_POST[surl] ' , ' $_POST[description] ' ) " ;if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con);?> ???
Link to comment
Share on other sites

Array is simple to find! But don´t I need a tutorial in which array and $_POST are both explained? Like the reply of Ingolme - good, but I don´t understand it! (need php for dummies!
you need to look a little bit at least!http://www.w3schools.com/php/php_forms.asphttp://www.w3schools.com/php/php_get.asphttp://www.w3schools.com/php/php_post.asphttp://www.w3schools.com/php/php_mysql_insert.asp This has not been the first time you've been shown these links.
Link to comment
Share on other sites

you need to look a little bit at least!http://www.w3schools...p/php_forms.asphttp://www.w3schools...php/php_get.asphttp://www.w3schools...hp/php_post.asphttp://www.w3schools...ysql_insert.asp This has not been the first time you've been shown these links.
Thanks! I´m sure about that aswell as I´m sure this is the first time I´m trying to learn php good!I´ll study them again, because I´ve seen them all!In none of them is clearly explained what´s my problem (´undefined index´)! At least not to me! Edited by eduardlid
Link to comment
Share on other sites

Eduard, I thought you found this post helpful about 'undefined index': http://w3schools.inv...ndpost&p=238353
welcome to our world. this has been going on, on and off, for about a year now. 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...