witchy478 Posted March 17, 2012 Share Posted March 17, 2012 Hi I'm trying to insert some information into my database but it tells me that the name is undefined how to I define it so that it works. Here is my code <html> <body> <form action="insert.php" method="post"> name: <input type="text" name="name" /> email: <input type="text" name="email" /> password: <input type="text" name="password" /> <input type="submit" /> </form> </body> </html> <?php $link = mysql_connect('localhost','test',''); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); } mysql_close($link); $sql="INSERT INTO emails (name,email, password) VALUES ('$_POST[name]','$_POST[email]','$_POST[password]')"; mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/259160-newbie-question/ Share on other sites More sharing options...
guinbRo Posted March 17, 2012 Share Posted March 17, 2012 A question about your code, are you not showing all of it? The reason I ask is because I'm not seeing you ever select a database, you are closing your connection immediately after you close it with mysql_close, and lastly at the end of the code you are closing another connection presumably. The one at the end is unnecessary, since the connection will be closed upon the script fully executing. Quote Link to comment https://forums.phpfreaks.com/topic/259160-newbie-question/#findComment-1328579 Share on other sites More sharing options...
witchy478 Posted March 17, 2012 Author Share Posted March 17, 2012 At the moment that is all of my code. I got rid of the one at the end and moved mysql_close() to the bottom which got rid of the undifined errors but now I can't insert into the database and test is the database Quote Link to comment https://forums.phpfreaks.com/topic/259160-newbie-question/#findComment-1328581 Share on other sites More sharing options...
EmperorJazzy Posted March 17, 2012 Share Posted March 17, 2012 Try this.... <html> <body> <form action="insert.php" method="post"> name: <input type="text" name="name" /> email: <input type="text" name="email" /> password: <input type="text" name="password" /> <input type="submit" /> </form> </body> </html> <?php $link = mysqli_connect('localhost','test',''); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); } $con = mysqli_connect($link); $sql="INSERT INTO emails (name,email, password) VALUES ('$_POST[name]','$_POST[email]','$_POST[password]')"; mysqli_query=($con, $sql); mysqli_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/259160-newbie-question/#findComment-1328582 Share on other sites More sharing options...
witchy478 Posted March 17, 2012 Author Share Posted March 17, 2012 I tried that and it gave me Parse error: syntax error, unexpected '=' in C:\wamp\www\test\insert.php on line 25 Quote Link to comment https://forums.phpfreaks.com/topic/259160-newbie-question/#findComment-1328584 Share on other sites More sharing options...
EmperorJazzy Posted March 17, 2012 Share Posted March 17, 2012 Oh remove the = from that line Quote Link to comment https://forums.phpfreaks.com/topic/259160-newbie-question/#findComment-1328585 Share on other sites More sharing options...
witchy478 Posted March 17, 2012 Author Share Posted March 17, 2012 I've done that and now I get these errors Warning: mysqli_connect() expects parameter 1 to be string, object given in C:\wamp\www\test\insert.php on line 19 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\test\insert.php on line 25 Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\wamp\www\test\insert.php on line 27 Quote Link to comment https://forums.phpfreaks.com/topic/259160-newbie-question/#findComment-1328586 Share on other sites More sharing options...
kicken Posted March 17, 2012 Share Posted March 17, 2012 You need to wrap your code in a big if statement to make it so that it will only run once the form has been submitted. Right now when you load the page it will attempt to insert your data, but there is nothing to insert since you have not yet submitted the data with the form. You also only need to call mysqli_connect once, your doing it twice right now (the second time incorrectly). You need to use the correct variable for the connection when using the other mysqli calls. <html> <body> <form action="insert.php" method="post"> name: <input type="text" name="name" /> email: <input type="text" name="email" /> password: <input type="text" name="password" /> <input type="submit" /> </form> </body> </html> <?php //Check that post data is received if (count($_POST) > 0){ $link = mysqli_connect('localhost','test',''); if (!$link) { die('Could not connect to MySQL: ' . mysqli_error($link)); } $sql="INSERT INTO emails (name,email, password) VALUES ('{$_POST['name']}','{$_POST['email']}','{$_POST['password']}')"; mysqli_query($link, $sql); mysqli_close($link); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259160-newbie-question/#findComment-1328590 Share on other sites More sharing options...
witchy478 Posted March 17, 2012 Author Share Posted March 17, 2012 I finally solved the problem I did the coding like this <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="emails"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Get values from form $name=$_POST['name']; $email=$_POST['email']; $password=$_POST['password']; // Insert data into mysql $sql="INSERT INTO $tbl_name(name, email, password)VALUES('$name', '$email', 'password')"; $result=mysql_query($sql); // if successfully insert data into database, displays message "Successful". if($result){ echo "Successful"; echo "<BR>"; echo "<a href='insert.php'>Back to main page</a>"; } else { echo "ERROR"; } // close connection mysql_close(); ?> and it worked. Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/259160-newbie-question/#findComment-1328591 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.