logo
Apache Lounge
Webmasters

 


About

Forum Index Downloads Search Register Log in  RSS Apache Lounge
 



Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Apache Lounge is not sponsored.

Your donations will help to keep this site alive and well, and continuing building binaries.



Problem writing to database from PHP using WAMP

 
Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner



View previous topic :: View next topic  
Author Message
Draug



Joined: 23 Jan 2007
Posts: 4

PostPosted: Tue 23 Jan '07 16:41    Post subject: Problem writing to database from PHP using WAMP Reply with quote

Hello!

Found this site while scouting for some assistance, seems to be some generally good guys here with a lot of knowledge. I would really appreciate some help regarding my scenario.

I am brand new to PHP and MySQL, I have some other programming languages behind me however they are not web-oriented.
I have not yet either invested in a book yet as I want to get started with smaller scripts to get a basic understanding of PHP & MySQL which I am certain I can accomplish through online tutorials and these types of forums Smile

Anyway, I downloaded and installed WAMP with the following components:

Apache 2.0.59
PHP 5.2.0
MySQL 5.0.27 (Community Server)
phpMyAdmin 2.9.0.3
SQLiteManager 1.2.0

I am now assuming these are all compatible with eachother Wink


Following a tutorial I created the following:

A database in phpMyAdmin called users, with the table user containing the fields user and password.
(both are varchar(40), not null)

I also created the following files:

connect.php
Code:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');

$dbname = 'users';
mysql_select_db($dbname);
?>



and the closedb.php
Code:

<?php
mysql_close($conn);
?>


and finally the main.php
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 'connect.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>


Now, I have absolutely no problem connecting to, or reading data from the database, I created a few users and passwords in phpMyAdmin and I am able to read them just fine from PHP, however when I run the main.php file and try to write to the database all I get is the error-line from the insert functions.

I am certain something is wrong with my code however my novice skills can't detect the problem. I would be very greatful if anyone could help me out.

Thanks in advance!

/D.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6255
Location: Germany, Next to Hamburg

PostPosted: Tue 23 Jan '07 17:59    Post subject: Reply with quote

You missed the second parameter in mysql_query Wink

Sesondly you can give out a message that helps you.
Code:

if (!mysql_query($query, $conn)) {
    echo mysql_errno($conn) . ": " . mysql_error($conn) . "\n";
}
Back to top
Draug



Joined: 23 Jan 2007
Posts: 4

PostPosted: Tue 23 Jan '07 18:27    Post subject: Reply with quote

Thanks alot, wonderful Smile

Made some progress and got stuck on this:

Code:
1064: 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', 'sdasda', PASSWORD('dsadas'), 'Y', 'Y', 'Y')' at line 1 New MySQL user added


Somehow I am certain this particular error is common, just not used to dealing with it as of yet. I tried looking around a few sites for a solution but could not find anything useful. Seems to me there is something funky with the priviliges in phpMyAdmin/the code, of course I could be way off :p

If anyone could enlighten me I'd be very glad.

Thanks again James!
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 610
Location: Milford, MA, USA

PostPosted: Wed 24 Jan '07 16:05    Post subject: Reply with quote

Your query above appears to have an extra space:
Quote:
INSERT INTO user (host, user, password, select_priv, insert_priv, update_ priv) VALUES ('localhost', '$username', PASSWORD('$password'), 'Y', 'Y', 'Y')


Perhaps it should be update_priv (no space) instead of update_ priv (contains a space before the p).

-tom-
Back to top
Draug



Joined: 23 Jan 2007
Posts: 4

PostPosted: Wed 24 Jan '07 16:24    Post subject: Reply with quote

Haha, stupidity has its consequences. Embarassed
Cannot belive I overlooked that, I guess my php editor (which did not recognize the error) had it highlighted in red and it was honestly not easy to see it until you mentioned it.

Now it gives me a:
Code:

1054: Unknown column 'select_priv' in 'field list'


This is rather easy to figure out ( even for me! Shocked ), since select_priv is not a column it can't find it.

Now I just got to figure out how to write priviligies from PHP to a database created in phpmyadmin...
Back to top


Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner
Page 1 of 1