purtip3154 Posted January 3, 2008 Share Posted January 3, 2008 <?php $user = "user"; $pass = "test"; mysql_connect('localhost','root','michael1'); mysql_select_db('LogPass'); $sql="INSERT INTO Table1 VALUES ('$ulog', '$plog')"; $rs=mysql_query($sql); header("Location: http://localhost/proj/"); ?> I've been having some problems getting this code to add a user and pass into my db... It worked perfectly in odbc, so I know it works. There is no error message, but the values do not get inserted... Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/ Share on other sites More sharing options...
awpti Posted January 3, 2008 Share Posted January 3, 2008 change: $rs=mysql_query($sql); to: $rs=mysql_query($sql) or die(mysql_error()); Also, where is $ulog and $plog coming from? Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429530 Share on other sites More sharing options...
phpSensei Posted January 3, 2008 Share Posted January 3, 2008 $sql="INSERT INTO `Table1` VALUES ('$ulog', '$plog')"; Where are the columns set for Table 1? Try this <?php $user = "user"; $pass = "test"; mysql_connect('localhost','root','michael1') or die(mysql_error()); mysql_select_db('LogPass')or die(mysql_error()); $sql="INSERT INTO `Table1` (username,password) VALUES ('$ulog', '$plog')"; $rs=mysql_query($sql)or die(mysql_error()); header("Location: http://localhost/proj/"); ?> edit: Oops. someone already posted, sorry. Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429532 Share on other sites More sharing options...
revraz Posted January 3, 2008 Share Posted January 3, 2008 $sql="INSERT INTO Table1 VALUES ('$user', '$pass')"; You were passing the wrong values. Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429533 Share on other sites More sharing options...
purtip3154 Posted January 3, 2008 Author Share Posted January 3, 2008 When the error message comes it says "No Database Selected", but there is one selected. I changed the values back, but to no avail. EDIT: You have to select the columns? Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429539 Share on other sites More sharing options...
shocker-z Posted January 3, 2008 Share Posted January 3, 2008 means that you haven't got mysql_select_db('databasename', $link) or die(myswl_error()); http://uk2.php.net/function.mysql-select-db If not, then you fancy showing the whole code? Regards Liam Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429543 Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2008 Share Posted January 3, 2008 You must list the columns if you are not inserting a value into each column. If you only have two columns in the table and you are inserting a value into each column, you don't need to list the columns. Because you get a database selected error message from the query statement, it means that your mysql_select_db() statement is incorrect. Fix that problem first. Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429548 Share on other sites More sharing options...
purtip3154 Posted January 3, 2008 Author Share Posted January 3, 2008 That is the whole code, but the mysql_select_db() statement is there... Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429597 Share on other sites More sharing options...
redarrow Posted January 3, 2008 Share Posted January 3, 2008 try that but even it work's sort your insert out mate.... <?php $user = "user"; $pass = "test"; $db=mysql_connect('localhost','root','michael1'); mysql_select_db('LogPass',$db); $sql="INSERT INTO Table1 VALUES ('$ulog', '$plog')"; $rs=mysql_query($sql)or die(mysql_query()); header("Location: http://localhost/proj/"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429612 Share on other sites More sharing options...
phpSensei Posted January 3, 2008 Share Posted January 3, 2008 Where are your values for the INSERT? Plus $sql="INSERT INTO Table1 VALUES ('$ulog', '$plog')"; That is wrong. Read this http://www.w3schools.com/php/php_mysql_insert.asp Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429613 Share on other sites More sharing options...
redarrow Posted January 3, 2008 Share Posted January 3, 2008 Where are your values for the INSERT? Plus $sql="INSERT INTO Table1 VALUES ('$ulog', '$plog')"; That is wrong. Read this http://www.w3schools.com/php/php_mysql_insert.asp it not wrong he not using the first varable defention you dont need to tell the varables what thay are if u dont want to....# it bad but it still works Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429616 Share on other sites More sharing options...
purtip3154 Posted January 3, 2008 Author Share Posted January 3, 2008 $user = "user"; $pass = "test"; Those are the values... Sorry, I mistyped. Thanks for the code redarrow, but it did not work. Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429618 Share on other sites More sharing options...
revraz Posted January 3, 2008 Share Posted January 3, 2008 Repost your current code and also post your DB Table layout and verify the DB name. Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429621 Share on other sites More sharing options...
redarrow Posted January 3, 2008 Share Posted January 3, 2008 try this mate should insert your name password and goto google it will work if in your databae you got ulog and plog in a table as one table,,,,, <?php $user="user"; $pass="test"; $db=mysql_connect("localhost","root","michael1"); $result=mysql_select_db("LogPass",$db); $user=addslashes($_POST['user']); $pass=addslashes($_POST['pass']); if(isset($user)||($pass))){ $sql="INSERT INTO Table1 (ulog,plog) VALUES ('$user', '$pass')"; $rs=mysql_query($sql)or die(mysql_query()); header("Location: http://www.google.com"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429628 Share on other sites More sharing options...
revraz Posted January 3, 2008 Share Posted January 3, 2008 This makes no sense $user="user"; $pass="test"; $user=addslashes($_POST['user']); $pass=addslashes($_POST['pass']); He has no FORM to POST from. Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429633 Share on other sites More sharing options...
phpSensei Posted January 3, 2008 Share Posted January 3, 2008 This makes no sense $user="user"; $pass="test"; $user=addslashes($_POST['user']); $pass=addslashes($_POST['pass']); He has no FORM to POST from. He probably only posted the Script. Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429639 Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2008 Share Posted January 3, 2008 Considering that the original problem was that there was no error checking on any of the mysql function calls to get php/mysql to tell exactly which one and why it was failing and that once some error checking was added to the mysql_query(), which resulted in the only addition information that there was no database selected, this thread has gotten off topic of helping with the problem of connecting to the database server, selecting a database, and executing a query. Correct the actual query once it is executing, but the code is not even reaching that point yet. The OP stated the $ulog/$plog/$user/$pass was a typo. Without knowing what the column names are, posting code with guesses about what the names are and if a form is involved, is just adding confusion. Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429654 Share on other sites More sharing options...
purtip3154 Posted January 3, 2008 Author Share Posted January 3, 2008 Sorry, been away for a while... DB Layout: Name: LogPass Table: Table1 Headers: Username, Password I was only posting the script but the form was just your basic html page and I prefer to get the script working before integrating it into the form... The code I was using was this one: <?php $user = "user"; $pass = "test"; mysql_connect('localhost','root','michael1') or die(mysql_error()); mysql_select_db('LogPass')or die(mysql_error()); $sql="INSERT INTO `Table1` (username,password) VALUES ('$user', '$pass')"; $rs=mysql_query($sql)or die(mysql_error()); header("Location: http://localhost/proj/"); ?> Thanks to PhpSensei I was still having the No Database Selected error though. Is it a problem having MySql and xampp installed if Mysql has a diff. port and service name? Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429802 Share on other sites More sharing options...
phpSensei Posted January 4, 2008 Share Posted January 4, 2008 Remove the OR die and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429815 Share on other sites More sharing options...
revraz Posted January 4, 2008 Share Posted January 4, 2008 Still didn't fix your variables. Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429818 Share on other sites More sharing options...
phpSensei Posted January 4, 2008 Share Posted January 4, 2008 ya it should be <?php $user = "user"; $pass = "test"; mysql_connect('localhost','root','michael1') or die(mysql_error()); mysql_select_db('LogPass'; $sql="INSERT INTO `Table1` (username,password) VALUES ('$user', '$pass')"; $rs=mysql_query($sql)or die(mysql_error()); header("Location: http://localhost/proj/"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/84336-mysql-value-adding-problem/#findComment-429823 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.