Jump to content

Insert to mysql not working locally


Tunnleram

Recommended Posts

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

[code]<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>
[/code]

Any help much appreciated.
Link to comment
Share on other sites

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:

[code]mysql_query($query) or die('Error, insert query failed');[/code]

to:

[code]$result = mysql_query($query) or die('Error '. mysql_error(). ' with query '. $query);[/code]
Link to comment
Share on other sites

I get this now:

[code]
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')
[/code]
Link to comment
Share on other sites

Hi Tunnleram,

looks like you need to change this line:

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

into this:

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

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

Jeff
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.