need help with insert.php

Reply

Join Date: Jun 2005
Posts: 20
Reputation: nike123 is an unknown quantity at this point 
Solved Threads: 0
nike123 nike123 is offline Offline
Newbie Poster

need help with insert.php

 
0
  #1
Jul 25th, 2005
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">&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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 45
Reputation: Sp!ke is an unknown quantity at this point 
Solved Threads: 0
Sp!ke's Avatar
Sp!ke Sp!ke is offline Offline
Light Poster

Some better-worded code

 
0
  #2
Jul 25th, 2005
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">&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></body></html>
[/php]
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 20
Reputation: nike123 is an unknown quantity at this point 
Solved Threads: 0
nike123 nike123 is offline Offline
Newbie Poster

Re: Some better-worded code

 
0
  #3
Jul 26th, 2005
oh yeah, what a stupid mistake. Have not actually changed the database name in my config file. Thanks for pointing that out.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 10
Reputation: aasahil is an unknown quantity at this point 
Solved Threads: 0
aasahil's Avatar
aasahil aasahil is offline Offline
Newbie Poster

Re: need help with insert.php

 
0
  #4
Jul 27th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 45
Reputation: Sp!ke is an unknown quantity at this point 
Solved Threads: 0
Sp!ke's Avatar
Sp!ke Sp!ke is offline Offline
Light Poster

Re: need help with insert.php

 
0
  #5
Jul 27th, 2005
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">&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></body></html>[/php]
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 20
Reputation: nike123 is an unknown quantity at this point 
Solved Threads: 0
nike123 nike123 is offline Offline
Newbie Poster

Re: need help with insert.php

 
0
  #6
Oct 18th, 2005
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">&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></body></html>[/php]
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 3
Reputation: hemalatha.a.s is an unknown quantity at this point 
Solved Threads: 0
hemalatha.a.s hemalatha.a.s is offline Offline
Newbie Poster

Re: need help with insert.php

 
0
  #7
Oct 19th, 2005
hello
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1
Reputation: phpnukes is an unknown quantity at this point 
Solved Threads: 0
phpnukes phpnukes is offline Offline
Newbie Poster

Re: need help with insert.php

 
0
  #8
Sep 24th, 2008
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:

  1. <html>
  2. <head>
  3. <title>Add New MySQL User</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. </head>
  6.  
  7. <body>
  8. <?php
  9. if(isset($_POST['add']))
  10. {
  11. include 'library/config.php';
  12. include 'library/opendb.php';
  13.  
  14. $username = $_POST['username'];
  15. $password = $_POST['password'];
  16.  
  17. $query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_ priv) VALUES ('localhost', '$username', PASSWORD('$password'), 'Y', 'Y', 'Y')";
  18. mysql_query($query) or die('Error, insert query failed');
  19.  
  20. $query = "FLUSH PRIVILEGES";
  21. mysql_query($query) or die('Error, insert query failed');
  22.  
  23. include 'library/closedb.php';
  24. echo "New MySQL user added";
  25. }
  26. else
  27. {
  28. ?>
  29. <form method="post">
  30. <table width="400" border="0" cellspacing="1" cellpadding="2">
  31. <tr>
  32. <td width="100">Username</td>
  33. <td><input name="username" type="text" id="username"></td>
  34. </tr>
  35. <tr>
  36. <td width="100">Password</td>
  37. <td><input name="password" type="text" id="password"></td>
  38. </tr>
  39. <tr>
  40. <td width="100">&nbsp;</td>
  41. <td>&nbsp;</td>
  42. </tr>
  43. <tr>
  44. <td width="100">&nbsp;</td>
  45. <td><input name="add" type="submit" id="add" value="Add New User"></td>
  46. </tr>
  47. </table>
  48. </form>
  49. <?php
  50. }
  51. ?>
  52. </body>
  53. </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
  1. <?php
  2. //mysql_free_result($result);
  3. mysql_close($conn);
  4. ?>

2.config.php
  1. <?php
  2. // db properties
  3. $dbhost = 'localhost';
  4. $dbuser = 'xxxxxxx';
  5. $dbpass = 'xxxxxx';
  6. $dbname = 'xxxxxx';
  7. ?>

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

I am wondering if the error is here:
  1. $query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_ priv) VALUES ('localhost', '$username', PASSWORD('$password'), 'Y', 'Y', 'Y')";
  2. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC