footbagger Posted January 19, 2008 Share Posted January 19, 2008 I'm extremley new to mySql, and trying to get a basic mySql page working on my computer before I upload it. The aim is to use the POST form to put data into a database, but it always seems to have trouble connecting to the database. Here's the output: Test sql page Name Entered: joe Info Entered: 213453251 Connected successfully Could not select database here is the code: <html> <head> <title>php post form test </title> </head> test sql page <hr /> Name Entered:<br /><?php echo $_REQUEST["Name"]; ?><br /> Info Entered:<br /><?php echo $_REQUEST["Info"]; ?><br /> <br /> <br /> <?php $db = mysql_connect("127.0.0.1","janis","test") ; if (!$db){ echo "error"; die('Could not connect: ' . mysql_error()); /*catches connection errors and aborts if the databse can't be updated*/ } /*connects to the sql server at localhost, with username: janis password: test*/ echo 'Connected successfully'; $table = "testDatabase"; mysql_select_db($table) or die('Could not select database'); /*having trouble connecting...??????????????????*/ mysql_query ('INSERT INTO ' . $table . ' VALUES ( $_REQUEST["Name"], $_REQUEST["Info"] );') or die('Error, query failed. ' . mysql_error()); /*insert the data tuple (name,info) using the data sent by POST from the form on the other page.*/ // Performing SQL query $query = 'SELECT * FROM my_table'; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); echo "<br />"; echo "the new table is now: <hr />"; // Printing results in HTML echo "<table>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($db); ?> <body> </body> </html> Any help is greatly appreciated -Janis Quote Link to comment https://forums.phpfreaks.com/topic/86759-solved-newbie-mysql-problem/ Share on other sites More sharing options...
asmith Posted January 19, 2008 Share Posted January 19, 2008 probebly there's no data base with name "testDatabase" in your mysql . check that , try changing to this line : mysql_select_db($table,$db) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/86759-solved-newbie-mysql-problem/#findComment-443437 Share on other sites More sharing options...
footbagger Posted January 19, 2008 Author Share Posted January 19, 2008 ok thanks, I changed the code and now this is happening: Test sql page Name Entered: joe Info Entered: 213453251 Connected successfully Access denied for user 'janis'@'%' to database 'testDatabase' Quote Link to comment https://forums.phpfreaks.com/topic/86759-solved-newbie-mysql-problem/#findComment-443441 Share on other sites More sharing options...
footbagger Posted January 19, 2008 Author Share Posted January 19, 2008 ok sorted that problem now this is happening: joe Info Entered: 213453251 Connected successfully Error, query failed. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '["Name"], $_REQUEST["Info"] )' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/86759-solved-newbie-mysql-problem/#findComment-443443 Share on other sites More sharing options...
Daniel0 Posted January 19, 2008 Share Posted January 19, 2008 Change it to: mysql_query ('INSERT INTO ' . $table . ' VALUES ( {$_REQUEST["Name"]}, {$_REQUEST["Info"]} );'); And then read this: http://php.net/string#language.types.string.parsing.complex Quote Link to comment https://forums.phpfreaks.com/topic/86759-solved-newbie-mysql-problem/#findComment-443444 Share on other sites More sharing options...
footbagger Posted January 19, 2008 Author Share Posted January 19, 2008 great info, thankyou. I didn't know about the curly brackets Quote Link to comment https://forums.phpfreaks.com/topic/86759-solved-newbie-mysql-problem/#findComment-443449 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.