Jump to content


PHP and MySQL help!


8 replies to this topic

#1 davidk20

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male

Posted 27 February 2008 - 10:29 AM

Hello Guys,

Im having problems with inserting data into a database from a PHP driven form. Im sure its somthing simple but im not to clued up on all this and am still learning.

The script im using is from a tut i am leading from but cant get any help from the publisher.

You can view the page online here. I have added the code hoping this will help.
<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 'config.php';
include '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(mysql_error());

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

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
}
?>
</body>
</html>

Many thanks guys.

#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 27 February 2008 - 11:01 AM

What errors are you getting?

#3 davidk20

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male

Posted 27 February 2008 - 11:27 AM

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 'priv) VALUES ('localhost', 'user', PASSWORD('password'))' at line 1

Any help would be great

Edited by davidk20, 27 February 2008 - 04:13 PM.


#4 Leibrockoli

    Young Padawan

  • Members
  • Pip
  • 27 posts

Posted 27 February 2008 - 05:32 PM

I think you may have skipped a space at "update_ priv)".

Try that and come back with any other problem. It doesn't seem like there is any error besides that space.

#5 davidk20

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male

Posted 28 February 2008 - 04:43 AM

Sorry not sure what you mean skipped a space at "update_ priv)".

This is the code where the error is coming from im guessing and from what i can make out there are spaces after each priv command.

$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(mysql_error());

If you was to click here and type any info in the form your see what im getting.

Thanks for the help so far though.

Edited by davidk20, 28 February 2008 - 04:59 AM.


#6 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 28 February 2008 - 07:03 AM

He means that there is a space in the last column name:

update_ priv

should be

update_priv

so your final sql is

$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(mysql_error());


#7 davidk20

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male

Posted 28 February 2008 - 09:24 AM

Simple fix thanks but there is still a problem. I am now getting "Table 'readingwhouse_tt.user' doesn't exist"

Im guessing it is trying to put the information into the database and not the table. The table name is "contact" but i can not see anywhere to tell it to go to that table, confused. Here are the include scripts.

config.php
<?php
$dbhost = 'readingwhouse.dns-systems.net';
$dbuser = 'readingwhouse_tt';
$dbpass = 'password';
$dbname = 'readingwhouse_tt';
?>

opendb.php
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>

and all the other code is the same but with that little bug fixed. Im doing somthing really silly again im sure.

Thanks for all help so far guys.

Edited by davidk20, 28 February 2008 - 10:59 AM.


#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 28 February 2008 - 11:39 PM

It's trying to insert it onto the correct table.

'readingwhouse_tt.user' = Databse: readingwhouse_tt - Table: user

This looks suspiciously to me like you are trying to insert something into the mysql user configuration table. I'm not sure about the rules on how you would actually going about doing that (being root user would probably be at the top of the list), but i doubt it can be done by the method you are trying. Not to mention, it wouldn't be in any other database besides, i believe, "mysql."

Could you show us the readingwhouse_tt database export file?

phpMyAdmin>click the database>export (select all the tables, make sure you're not saving it to a file, hit ok, copy/paste the result here).

#9 davidk20

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male

Posted 29 February 2008 - 05:21 AM

Okay i have worked out part of the error but still getting errors, there was a typo in the table name but i fixed that.

The SQL code that im using is below
-- 
-- Table structure for table `user`
-- 

CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `host` varchar(30) collate latin1_general_ci NOT NULL default '',
  `user` varchar(20) collate latin1_general_ci NOT NULL default '',
  `password` varchar(20) collate latin1_general_ci NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1;

-- 
-- Dumping data for table `user`
--

Sorry for being so stupid with all this at the early earning curve. Thanks for all the help





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users