954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need help with insert.php

A little help, required with this insert.php script, which is meant to add a new user to mysql user, unfortunately when this script is executed, and username and password inserted, it doesn't actually add the new MYSQL USER, instead i just get the error message .

<?php
if(isset($_POST['add']))
{
include ('config.php');
include ('opendb.php');

mysql_select_db($mysql);
$username = $_POST['username'];
$password = $_POST['password'];

$query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_ priv) VALUES ('localhost', '$username', PASSWORD('$password'), 'Y', 'Y', 'Y')";
mysql_query($query) or die('Error, insert query failed ');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');
include ('closedb.php');
echo "New MySQL user added";
}
else
{
?>

Username

Password

 
 
 


<?php

nike123
Newbie Poster
20 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 



Username

Password

 
 
 

Sp!ke
Light Poster
45 posts since Jul 2005
Reputation Points: 14
Solved Threads: 0
 

oh yeah, what a stupid mistake. Have not actually changed the database name in my config file. Thanks for pointing that out.

nike123
Newbie Poster
20 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Just change your insert code with this:

$query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_ priv) VALUES ('localhost', '$username','$password', 'Y', 'Y', 'Y')";
mysql_query($query) or die('Error, insert query failed ');

I hope this works.
If do so then reply me.
bye.

aasahil
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 



Username

Password

 
 
 

Sp!ke
Light Poster
45 posts since Jul 2005
Reputation Points: 14
Solved Threads: 0
 



Username

Password

 
 
 

nike123
Newbie Poster
20 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

hello

hemalatha.a.s
Newbie Poster
3 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

This has not been resolved since the original poster found an alternative solution and has not posted back for others.

This is the complete code I am using:

<html>
<head>
<title>Add New MySQL User</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
if(isset($_POST['add']))
{
include 'library/config.php';
include 'library/opendb.php';

$username = $_POST['username'];
$password = $_POST['password'];

$query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_ priv) VALUES ('localhost', '$username', PASSWORD('$password'), 'Y', 'Y', 'Y')";
mysql_query($query) or die('Error, insert query failed');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');

include 'library/closedb.php';
echo "New MySQL user added";
}
else
{
?>
<form method="post">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Username</td>
<td><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td width="100">Password</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td><input name="add" type="submit" id="add" value="Add New User"></td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>

I have tried the solutions above but the last one produced a syntax error

There must be a simpler solution

The three external files for these are as follows

1. closedb.php

<?php
//mysql_free_result($result);
mysql_close($conn);
?>


2.config.php

<?php
// db properties
$dbhost = 'localhost';
$dbuser = 'xxxxxxx'; 
$dbpass = 'xxxxxx';
$dbname = 'xxxxxx';
?>


3.opendb.php

<?php
$conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbname);
?>


I am wondering if the error is here:

$query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_ priv) VALUES ('localhost', '$username', PASSWORD('$password'), 'Y', 'Y', 'Y')";
mysql_query($query) or die('Error, insert query failed');


INSERT INTO user

I have tried $user and even tried changing the database table name.
On the database there are two tables

user
tbl_auth_user

phpnukes
Newbie Poster
1 post since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You