Jump to content


Photo

Insert to mysql not working locally


  • Please log in to reply
4 replies to this topic

#1 Tunnleram

Tunnleram
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 10 September 2006 - 01:11 AM

I'm going through a tutorial (this is after doing several things with php/mysql so I'm not a complete noob, just relearning sorta) and I'm having issues getting it to work locally. I have Xampp installed, php, apache and mysql are all working. I can insert from mysql without an issue, but whenever I try to insert any data using php it fails.

When I run the page below I get 'Error, insert query failed', but everything looks right. Am I missing something?

PHP Version 5.1.4
Mysql 5.0.21
Apache 2.2.2

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

Any help much appreciated.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 10 September 2006 - 01:17 AM

You can get more useful information by changing the error messages in each case.  You can't tell with your present messages which query failed or why.

Change:

mysql_query($query) or die('Error, insert query failed');

to:

$result = mysql_query($query) or die('Error '. mysql_error(). ' with query '. $query);

Legend has it that reading the manual never killed anyone.
My site

#3 Tunnleram

Tunnleram
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 10 September 2006 - 05:14 PM

I get this now:

Error 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', 'testuser', PASSWORD('password'), 'Y', 'Y', 'Y')' at line 1 with query INSERT INTO user (host, user, password, select_priv, insert_priv, update_ priv) VALUES ('localhost', 'testuser', PASSWORD('password'), 'Y', 'Y', 'Y')


#4 jefkin

jefkin
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 11 September 2006 - 02:47 AM

Hi Tunnleram,

looks like you need to change this line:

$query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_ priv) VALUES ('localhost', '$username', PASSWORD('$password'), 'Y', 'Y', 'Y')";

into this:

$query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv) VALUES ('localhost', '$username', PASSWORD('$password'), 'Y', 'Y', 'Y')";
                                                                            ^^^^^^^^^^^

The 'update_'  and the 'priv' need to go together!!

Jeff


#5 Tunnleram

Tunnleram
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 September 2006 - 06:46 AM

lol thx! Damn tutorials. I copy and pasted that hehe doh!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users