iStriide Posted August 3, 2010 Share Posted August 3, 2010 MySQL connection works and it connects to my database but it doesnt insert values into the table that I created. <form action="phplogin2.php" method="post"> Username: <input type="text" name="user" style="color: white; background-color: blue;"/><br/> Password: <input type="password" name="pass" style="color: grey; background-color: black;"/><br/> <button>Login</button> </form> <?php $con = mysql_connect('localhost', 'root', 'eagles1') or die("did not connect"); $dbc = mysql_select_db('mysql') or die("did not connect to database"); $query = mysql_query("INSERT INTO login VALUES('', '$user', '$pass')") or die("query did not work"); $user = $_POST['user']; $pass = $_POST['pass']; if ($con==true){ echo "MySQL Connection Succesful"; } if ($dbc==true){ echo "MySQL Database Connection Succesful"; } if ($query==true){ echo "MySQL Query Succesful"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209622-mysql-query-wont-work/ Share on other sites More sharing options...
monkeytooth Posted August 3, 2010 Share Posted August 3, 2010 $query = mysql_query("INSERT INTO login VALUES('', '$user', '$pass')") or die("query did not work"); Should look more like $query = mysql_query("INSERT INTO login (column_name, column_name, column_name) VALUES('', '$user', '$pass')") or die("query did not work"); Where "column_name" is equivalent to the column name in the table of the database. also just a friendly suggestions I think you may also want to look into SQL injection prevention through Sanitation dropping an unsanitized $_POST['anything'] (or $_GET) could have serious, serious repercussions in the long run. Heres a good starter article to convey the point on sanitization of your queries. http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php Ill say though this is only a starter article, it can get a lot deeper, but if you follow at the least some of the stuff said there, you will have far less to worry about. Quote Link to comment https://forums.phpfreaks.com/topic/209622-mysql-query-wont-work/#findComment-1094398 Share on other sites More sharing options...
jcbones Posted August 3, 2010 Share Posted August 3, 2010 That query should work, if you only have 3 columns in the table. Does it throw any errors? Have you checked if your variables are set to what you think they are? OR, perhaps You have initialized the variables, AFTER you make the database call, therefore, you are inserting empty values into a database. Quote Link to comment https://forums.phpfreaks.com/topic/209622-mysql-query-wont-work/#findComment-1094399 Share on other sites More sharing options...
monkeytooth Posted August 3, 2010 Share Posted August 3, 2010 nice catch jcb, I didn't even pay mind to that factor, where the variables are assigned after the query :-) As for the query it self I only base that on his original post (having assumed that there are only 3 columns in the query) Quote Link to comment https://forums.phpfreaks.com/topic/209622-mysql-query-wont-work/#findComment-1094401 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.