0

Currently working with a php script and a sql database. Everything seems like it's working okay and even when echoing the username at the end it seems to be successful. When looking further into the insertion in the database the row was not inserted. There is no error message and the insert statement reflects the columns exactly... Any ideas?

<?php
$db_name = "animator"; // Database name
$link = mysql_connect('localhost', 'root', 'admin');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db($db_name) or die("Could not connect to Database" .mysql_error());

$username  = $_POST['fname'];
$passwords = $_POST['pass'];
$email     = $_POST['email'];

mysql_query($link,"INSERT INTO 'account'('username', 'passwords', 'email') VALUES 
($username , $passwords , $email)");

mysql_close($link);
?>

***UPDATE**** Thank you so much for everyones response. I was finally able to get it working with another technique I found.

<?php
error_reporting(E_ALL);
ini_set('display_errors',"On");
$db_name = "animator"; // Database name
$link = mysql_connect('localhost', 'root', 'admin');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//echo 'Connected successfully';
mysql_select_db($db_name) or die("Could not connect to Database" .mysql_error());

$username = $_POST['fname'];
$passwords = $_POST['pass'];
$email = $_POST['email'];

$query =  "INSERT INTO account (username, passwords, email) VALUES ('$username', 
'$passwords', '$email')";
$result = mysql_query($query);
echo $username;
mysql_close($link);
?>
Zach Harvey
  • 71
  • 1
  • 10
  • You're not checking for errors from the `mysql_query` statement...try adding `or die(mysql_error())` to the end of that line. Alternatively you can echo the query and copy and paste it into phpMyAdmin or the MySQL console to make sure the query itself is good. – Matt Browne Jun 07 '13 at 01:24

3 Answers3

1

Try removing the quotations around account or use ` around account

cameronjonesweb
  • 2,435
  • 3
  • 25
  • 37
1

Quotes are for strings; backticks are for table and column name escaping

mysql_query("INSERT INTO account (username, passwords, email) 
VALUES ('$username', '$passwords', '$email')", $link);

So remove the quotes from the table and column names and add them to the values.

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. Learn about Prepared Statements instead, and use PDO or MySQLi. See this article for a quick overview how to do it and why it is so important.

Community
  • 1
  • 1
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • Sadly this didn't work but I have turned on the error reporting thanks to another comment below and I'm currently working on the issue! Thank you so much for your quick response! – Zach Harvey Jun 07 '13 at 01:52
  • Warning: mysql_query() expects parameter 1 to be string, resource given in /var/www/register.php on line 16 – Zach Harvey Jun 07 '13 at 01:54
  • You need to switch the parameters. First comes the query and then the connection. See my update. – juergen d Jun 07 '13 at 02:01
  • Your edit would have worked as well. Since I've got it to work with the secondary update I put I've decided to leave it like that lol. Thank you once again for your help! – Zach Harvey Jun 07 '13 at 02:40
0

Two things...

First, you should not use PHP's mysql for new development... Read about MySQLi, seriously! (Also, a great blog post for beginners: http://codular.com/php-mysqli)

Second, if you must use mysql, and if PHP doesn't return any errors, you can utilize mysql_error() to diagnose the problem.

P.S. Check for PHP errors using:

error_reporting(E_ALL);
ini_set('display_errors',"On");
honyovk
  • 2,717
  • 18
  • 26
  • Thank you very much for the lines for error reporting. I've looked into mysqli but with the timeline I have right now it's not possible. Really do appreciate the quick response. – Zach Harvey Jun 07 '13 at 01:53