Jump to content

MySQL query wont work.


iStriide

Recommended Posts

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";
}

?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.