Jump to content

Recommended Posts

I am creating a set of pages to establish connection to mysql.

 

i create a page called mysqlcfg.php with this code

<?php
$server = 'localhost'
$db_user = 'admin'
$db_pass = 'admin'
$database = 'test'
$table = 'users'
?>

and i have a log in script such as

<?php 

include("config.php"); 

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// check if the username is taken
$check = "select id from $table where username = '".$_POST['username']."';"; 
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry); 
if ($num_rows != 0) { 
echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=register.html>Try again</a>";
exit; 
} else {

// insert the data
$insert = mysql_query("insert into $table values ('NULL', '".$_POST['username']."',
'".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());

// print a success message
echo "Your user account has been created!<br>"; 
echo "Now you can <a href=login.html>log in</a>"; 
}

?>

 

but i get an error saying that the username when some one attempts to log in does not meet the construction set in my version of Mysql or something to that nature... don't remember and a different computer. but is there anything wrong with the code or is it the way i setup the mysql database.

Link to comment
https://forums.phpfreaks.com/topic/110641-mysql-username-issue/
Share on other sites

please post the exact error so we can help you ;)

 

also putting POST variables in MYSQL is bad practice.

 

you should put POST variables inside a variable then put it in MYSQL

 

like

 

$variable= $_POST[variable];

mysql_query"(INSERT  INTO table ( values here) VALUES ($variable)");

 

 

-thanks

Link to comment
https://forums.phpfreaks.com/topic/110641-mysql-username-issue/#findComment-567609
Share on other sites

you should put POST variables inside a variable then put it in MYSQL

 

like

 

$variable= $_POST[variable];

mysql_query"(INSERT  INTO table ( values here) VALUES ($variable)");

 

Why would you do that?  Assigning it to a intermediate variable, like you have above, is no different than using the $_POST array.  It doesn't matter if you address $_POST directly or assign it to an intermediate variable, use mysql_real_escape_string to prevent SQL injection...

 

$result = mysql_unbuffered_query("INSERT INTO table (column_name) VALUES ('" . mysql_real_escape_string($_POST['value']) . "')") or die(mysql_error());

 

http://www.php.net/mysql_real_escape_string

Link to comment
https://forums.phpfreaks.com/topic/110641-mysql-username-issue/#findComment-567638
Share on other sites

Hey thanks what you advised helped but i get the error

 

could not insert data because column count does not match value count at row 1

 

and when i try to create a new table through php i get the error

 

could not create table because you have an error sql syntax check the manual that corresponds to your mysql server version for the right syntax to use near 'username(username) at line 1

 

 

Link to comment
https://forums.phpfreaks.com/topic/110641-mysql-username-issue/#findComment-568601
Share on other sites

That error would mean that your table name that is in $table doesn't have exactly three columns.

 

This is probably just a mistake in your post, but you said you named the config file 'mysqlcfg.php', but you are including 'config.php'. Make sure you aren't mixing the two up.

Link to comment
https://forums.phpfreaks.com/topic/110641-mysql-username-issue/#findComment-568606
Share on other sites

yes that was a typo sorry.... but how would i fix that error i set up the table with this script if that helps

 

$create = create table $table (
id smallint(5) NOT NULL auto_increment,
username varchar(30) NOT NULL,
password varchar(32)NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY username (username)
);";

Link to comment
https://forums.phpfreaks.com/topic/110641-mysql-username-issue/#findComment-568616
Share on other sites

If that is the same table I would expect a different error. You declare all fields as NOT NULL and you try to put NULL in as a value. Try changing it to this:

$insert = mysql_query("INSERT INTO $table (username, password) VALUES ('".$_POST['username']."', '".$_POST['password']."')")

Link to comment
https://forums.phpfreaks.com/topic/110641-mysql-username-issue/#findComment-568628
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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