I have an insert statement I am querying to the DB.
$notes_sql = 'INSERT into notes SET
order_id = "'.$_GET['order_id'].'",
date_added = "'.$date_added.'",
note_message = "'.$_SESSION['note_message'].'"';
$notes_result = $conn->query($notes_sql) or die(mysqli_error($conn));
I know the sql statement is correct, because when I try to echo it out and run that echo'd statement in phpMyadmin, it inserts it fine.
When I try to run the insert statement, I get the following error:
INSERT command denied to user 'ideapale_amquery'@'localhost' for table 'notes'
Line 74 is the line line of code that queries the statement.
I also know that I am correctly connecting to the DB through an included connection.php file, since I am using variables from that included file on the page I am working on.
I'm guessing I am somehow not correctly connecting to the DB, but I am not sure what it is. I copied the code for this page from another page that works fine, so I don't understand why it is not working.
Why is my insert not working in php?
***** UPDATE *****
This is my connection script:
function dbConnect($type) {
if($type == 'query'){
$user = 'ideapale_amquery';
$pwd = 'xxxxxxxxxxx';
} else if($type == 'admin') {
$user = 'ideapale_amadmin';
$pwd = 'xxxxxxxxxxxx';
} else {
exit('Unrecognized connection type');
}
$conn = new mysqli('localhost', $user, $pwd, 'ideapale_offorders') or die('Cannot ope开发者_如何转开发n database');
return $conn;
}
This is how I am connecting to the connection script from the page I am on:
$conn = dbConnect('admin');
I am connecting to the DB using the admin privileges, so I don't understand why it thinks I want to use the 'amquery' username.
You claim to be connecting with the ideapale_amadmin
user, but the error message INSERT command denied to user 'ideapale_amquery'@'localhost' for table 'notes'
clearly shows that you are not. You are using the wrong MySQLi object (i.e. a connection you created earlier on in the script) or you are passing the wrong parameter to dbConnect()
.
Try
$notes_result = $conn->query($notes_sql) or die(mysqli_error($conn));
To get an updated error.
If the echoed SQL works fine you have to guess it's a connection or permissions issue. Are you doing an insert anywhere else?
Edit: So it's a permissions issue, run this:
GRANT INSERT PRIVILEGES ON *.* TO 'ideapale_amquery'@'localhost'
However, if you don't believe you need to, try this first:
SHOW GRANTS FOR 'ideapale_amquery'@'localhost'
You should check and see what the error being returned is.
Change: die(mysqli_error())
To: die(mysqli_error($conn))
精彩评论