| | |
need help with insert.php
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2005
Posts: 20
Reputation:
Solved Threads: 0
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
{
?>
<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"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Add New User"></td>
</tr>
</table>
</form>
<?php
<?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
{
?>
<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"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Add New User"></td>
</tr>
</table>
</form>
<?php
Well, your code is, shall we say, less than perfect, but, then again, we all have to learn sometime, right? See if this helps:
[php]<?php
if (isset($_POST['add'])) {
include ('config.php');
include ('opendb.php');
mysql_select_db('DB_NAME', $mysql);
// You must tell it what the connection resource is, as well
// as which databse you will be using. Make sure that in opendb.php,
// the function mysql_connect(); is being set to $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')";
// No single quotes are needed for variables in the statement above.
mysql_query($query) or die('Error, insert query failed ');
$query2 = "FLUSH PRIVILEGES";
mysql_query($query2) or die('Error, insert query failed');
include ('closedb.php');
echo "New MySQL user added";
} else {
?>
<html><body>
<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"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Add New User"></td>
</tr>
</table>
</form></body></html>
[/php]
[php]<?php
if (isset($_POST['add'])) {
include ('config.php');
include ('opendb.php');
mysql_select_db('DB_NAME', $mysql);
// You must tell it what the connection resource is, as well
// as which databse you will be using. Make sure that in opendb.php,
// the function mysql_connect(); is being set to $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')";
// No single quotes are needed for variables in the statement above.
mysql_query($query) or die('Error, insert query failed ');
$query2 = "FLUSH PRIVILEGES";
mysql_query($query2) or die('Error, insert query failed');
include ('closedb.php');
echo "New MySQL user added";
} else {
?>
<html><body>
<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"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Add New User"></td>
</tr>
</table>
</form></body></html>
[/php]
Geek and a Half Blog
Oh, shoot, we're both wrong, heh!
This will most DEFINATELY work.
[php]<?php
if (isset($_POST['add'])) {
include ('config.php');
include ('opendb.php');
mysql_select_db('DB_NAME', $mysql);
// You must tell it what the connection resource is, as well
// as which databse you will be using. Make sure that in opendb.php,
// the function mysql_connect(); is being set to $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')";
// You need to concatenate with the '.' command.
// I know this will work, I've used it hunderds of times.
mysql_query($query) or die('Error, insert query failed ');
$query2 = "FLUSH PRIVILEGES";
mysql_query($query2) or die('Error, insert query failed');
include ('closedb.php');
echo "New MySQL user added";
} else {
?>
<html><body>
<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"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Add New User"></td>
</tr>
</table>
</form></body></html>[/php]
This will most DEFINATELY work.
[php]<?php
if (isset($_POST['add'])) {
include ('config.php');
include ('opendb.php');
mysql_select_db('DB_NAME', $mysql);
// You must tell it what the connection resource is, as well
// as which databse you will be using. Make sure that in opendb.php,
// the function mysql_connect(); is being set to $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')";
// You need to concatenate with the '.' command.
// I know this will work, I've used it hunderds of times.
mysql_query($query) or die('Error, insert query failed ');
$query2 = "FLUSH PRIVILEGES";
mysql_query($query2) or die('Error, insert query failed');
include ('closedb.php');
echo "New MySQL user added";
} else {
?>
<html><body>
<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"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Add New User"></td>
</tr>
</table>
</form></body></html>[/php]
Geek and a Half Blog
•
•
Join Date: Jun 2005
Posts: 20
Reputation:
Solved Threads: 0
oh no, thanks for the advice, I got it working ages ago, just got my tutor to look at it for me. Am not too sure how it worked (Not so much of a programming person you see)(
•
•
•
•
Originally Posted by Sp!ke
Oh, shoot, we're both wrong, heh!![]()
This will most DEFINATELY work.
[php]<?php
if (isset($_POST['add'])) {
include ('config.php');
include ('opendb.php');
mysql_select_db('DB_NAME', $mysql);
// You must tell it what the connection resource is, as well
// as which databse you will be using. Make sure that in opendb.php,
// the function mysql_connect(); is being set to $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')";
// You need to concatenate with the '.' command.
// I know this will work, I've used it hunderds of times.
mysql_query($query) or die('Error, insert query failed ');
$query2 = "FLUSH PRIVILEGES";
mysql_query($query2) or die('Error, insert query failed');
include ('closedb.php');
echo "New MySQL user added";
} else {
?>
<html><body>
<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"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Add New User"></td>
</tr>
</table>
</form></body></html>[/php]
•
•
Join Date: Sep 2008
Posts: 1
Reputation:
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:
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
2.config.php
3.opendb.php
I am wondering if the error is here:
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
This is the complete code I am using:
PHP Syntax (Toggle Plain Text)
<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"> </td> <td> </td> </tr> <tr> <td width="100"> </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 Syntax (Toggle Plain Text)
<?php //mysql_free_result($result); mysql_close($conn); ?>
2.config.php
PHP Syntax (Toggle Plain Text)
<?php // db properties $dbhost = 'localhost'; $dbuser = 'xxxxxxx'; $dbpass = 'xxxxxx'; $dbname = 'xxxxxx'; ?>
3.opendb.php
PHP Syntax (Toggle Plain Text)
<?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:
PHP Syntax (Toggle Plain Text)
$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
![]() |
Similar Threads
- Basic PHP Includes (PHP)
- How to insert PHP in HTML language? (PHP)
- phpBB to php-nuke integration help (PHP)
- New to PHP (PHP)
Other Threads in the PHP Forum
- Previous Thread: Linking to other pages
- Next Thread: php+word
| Thread Tools | Search this Thread |
.htaccess ajax apache api array beginner binary broken buttons cakephp checkbox class cms code cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail mediawiki menu mlm mod_rewrite multiple mysql number oop paypal pdf php phpincludeissue phpmyadmin problem query radio random recursion regex remote script search server sessions sms soap source sp space speed sql subdomain syntax system table tag tutorial update upload url validation validator variable vbulletin video web websphere white xml youtube





