Jump to content

Getting mysql error when trying to insert md5 password


simcoweb

Recommended Posts

Ok, simple stuff. I'm writing a quickie script to insert a user (username & encrypted password) into a mysql table for the purpose of adding an administrator/user/login.

 

I have this:

 

$username = "bozotheclown";

$password = md5("bozosbigtop");

 

with query written in standard stuff:

 

$sql = "INSERT INTO admin_table (username, password) VALUES '$username', '$password'";

 

But I get a mysql error:

 

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 ''bozotheclown', '7627a74990b4087d37e591b6f8063599' at line 1

 

I've not used the md5 for anything yet so not familiar with how i'd set this up.

I got it to insert and to encrypt it by changing to this:

 

<?php
$username = "bozotheclown";
$password = "bozosbigtop";
$enc_password = md5($password);
// run query to insert
include 'dbconfig.php';

// Connect to database
$con = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname, $con) or die(mysql_error());

$sql = "INSERT INTO eastside_admin (username, password) VALUES ('$username', '$enc_password')";
$results = mysql_query($sql) or die(mysql_error());
?>

 

But, when I try to validate that it worked I get an error using this code:

 

<?php
$num_rows = mysql_affected_rows($results);
if ($num_rows == 1) {
  echo "Administrator successfully added";
} else {
  echo "Could not insert administrator for some reason.";
}
?>

 

What's strange is that it IS inserting the user info but then fails on the validation and shows the "Could not insert..." message. When I view the database I see the entry, however.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.