PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
$con = mysql_connect('localhost','user','pass')
or die('ERROR: Could not connect to Database Server - '.mysql_error());
mysql_select_db('mydatabase',$con)
or die('ERROR: Could not access specified Database - '.mysql_error());
// Your SQL didn't have POST indexs that matched field... so you will want to fix!
$SQL = sprintf('INSERT INTO `tbl_login` (`FirstName`,`LastName`,`Age`) VALUES ("%s","%s","%s")',
mysql_real_escape_string($_POST['firstname']),
mysql_real_escape_string($_POST['uname']),
mysql_real_escape_string($_POST['pwd'])
);
mysql_query($SQL)
or die('ERROR: '.mysql_error());
echo '1 record added';
mysql_close($con);
?>
and from the part : $con = mysql_connect("localhost","","") : what password and user that should i put. since every time i put a password and user i got this error below and i thing its the one why i can't connect to a database.
Could not connect: Access denied for user 'root'@'localhost' (using password: YES)
Well, if you are actually doing a connect to mySQL without a user and password, then you are most likely connecting as guest, and the only database you will be able to access is on called "test" (if it exists, many times it is removed). Try telling it to select "test" instead, see what happens. (if it does select it, your INSERT will fail though)
The username and password you would need we can't give, it would be given to you by either the person or system that is setting up the database for you to use.