Jump to content

Archived

This topic is now archived and is closed to further replies.

Tunnleram

Insert to mysql not working locally

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.

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.