I'm new with mysql and php so please bear with me.
I'm trying to connect my first form to a table and I keep running into a new issue every time I "fix" something. I'm trying to test my form to make sure it connects before I move forward.
This is the form:
<form action="demo.php" method="post">
<p>input 1: <input type="text" name="input1"/></p>
<input type="submit" value="Submit" />
</form>
and this is my "demo.php" file:
<?php
define('BD_NAME', 'DEMO');
define('DB_USER', 'DEMO');
define('DB_PASSWORD', 'PASSWORD');
define('DB_HOST', 'HOST');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
echo 'Connected successfully';
mysql_close();
?>
I keep getting this error:
Can't use DB_NAME: Access denied for user 'USER'#'%' to database 'DB_NAME'
Again I'm new at this and I'd appreciate any help. Thanks!!
You have a typo.
define('BD_NAME', 'DEMO'); should be define('DB_NAME', 'DEMO');
Notice the DB_NAME global variable that is not resolve :
Can't use >>>>DB_NAME<<<<: Access denied for user 'USER'#'%' to database 'DB_NAME'
It means that either DB_NAME is not declared OR that the value of DB_NAME is "DB_NAME".
always use error_reporting(E_ALL) and display errors while coding to avoid these mistakes
Related
I have a have rented a server at www.unoeuro.com (I dont know if this matters)
When i submit the form it gives me "HTTP Error 405.0 - Method Not Allowed"
<?php
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_HOST', '')
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' .mysql_error());
}
$db_selected = mysql_slect_db(DB_NAME, $link);
if (!$db_selected) {
die('Could not connect: ' .mysql_error());
}
$value = $_POST['username'];
$sql = "INSERT INTO test (username) VALUES ('$value')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
?>
<!doctype HTML>
<form action="test.php" method="post">
<p>Username: <input type="text" name="username" /><p>
<input type="submit" value="Submit" />
</form>
Can anyone tell me what i am doing wrong?
Just ask me for screenshots of anything you can use to help!
Sorry if the issue is obvious. This is my first time working with any kind of database, PHP and such.
Thanks!
First of all, if those are your actual database username and password, you're going to want to be changing those sharpish before someone else changes them for you :)
It's most likely down to how your server is configured to handle PHP files.
The first thing I would check is that the package you are leasing is designed to host PHP scripts. Looking at the UnoEuro package details (https://en.unoeuro.com/products.php#specs) it states 'ASP or PHP'. Is it possible that you are using a server setup for ASP?
If you are definitely on a PHP package, I would contact the service provider. They should know everything there is to know about configuring your server.
Also; there is a typo in your code 'mysql_slect_db'.
I am testing a website offline with XAMPP. My PHP code connects and works with my local MySQL database (i.e. 'localhost' or '127.0.0.1'). I wish to conduct tests using an online MySQL database.
I am trying to use PHPMyAdmin Demo Server but I get an error when trying to connect to the server. I changed the 'DB_HOST' to '192.168.30.23' (which seems to be the demo server ip address). Am I missing something?
Here is a snippet of my code:
<?php
define('DB_NAME', 'ears');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', '192.168.30.23');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
?>
Thank you in advance.
The phpMyAdmin MySQL demo server is not available to test your script. Google for "public access MySQL server" to maybe find one.
When I press my submit button, I get an error saying :
ErrorTable 'form2.demo' doesn't exist
form2 is my database name which is created in phpmyadmin.
I am a new MAC user.
Below is my php code.
define('DB_NAME','form2');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','localhost:8888');
My ports are :
Apache : 8888
Msql : 8889
Full code is as below:
<?php
define('DB_NAME','form2');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','localhost:8889');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('Could not connect :' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if(!$db_selected){
die('Can\'t use' . DB_NAME . ':' . mysql_error());
}
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
if(!mysql_query($sql)){
die('Error' . mysql_error());
}
mysql_close();
?>
Your problem have nothing to do with Mac. You have to show us more code. How the code in your form looks like? How you are sending stuff to database?
Let's assume that everything in code is set up properly. The main thing you have to check is if you actually have demo table in your database. Go to phpmyadmin and check it, if there is no table create it. You can use phpmyadmin to do that or do it via SQL query like that one
CREATE TABLE IF NOT EXISTS `form2`.`demo` ( `id` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY (`id`));
Of course code above will create table with only ID column so you have to adjust it to your needs
DEFINE('DB_USERNAME', 'root');
DEFINE('DB_PASSWORD', '');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_DATABASE', 'customer');
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
echo 'Connected successfully.';
$mysqli->close();
Try the msqli library instead
I have a simple html form and a php file to execute a database insertion. The problem I am having is that when I press the submit button, my database table receives 3 copies of the same submission and I only need one. Below is the code.
html:
<!DOCTYPE html>
<html>
<form action="demo.php" method="post">
<p>
Input 1: <input type="text" name="input1" />
<input type="submit" value="Submit" />
</p>
</form>
</html>
php:
<?php
define('DB_NAME', 'phpmyadminName');
define('DB_USER', 'phpmyadminUser');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$values')";
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
mysql_close();
?>
The DB_NAME, DB_USER, and DB_PASSWORD have all been changed for obvious reasons, but the code does work.
It just submits too many copies of the form data to the database table. Way back when I was in school, I had this issue, but it seemed like the problem was on the server's end and not our code. The server used here is mine and I do have full control over it. If the server is the issue at fault, I need help correcting that (as I am doing this to learn how to admin these tools, I do not know much more than basic level administration).
Kenneth, the code you have provided here honestly needs some work. First of all, please don't use the mysql API anymore. It's deprecated, will no longer be supported in future PHP versions, and is insecure. For all database operations use the mysqli or PDO API's, preferrably with prepared statements.
Secondly, do not ever INSERT $_POST or $_GET variables directly into the database without validating/sanitizing them first as someone could delete your data or even worse your whole database. PHP has numerous functions to make this very easy such as ctype depending on the data type.
Maybe try something like this in your code:
if (!empty($_POST['input1'])) { //checks if data was received//
$value = $_POST['input1'];
mysql_real_escape_string($value);
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
} else {
echo "form was not received";
exit;
}
I also noticed that your variable names were different, which is corrected above.
EDIT :
Mistakenly used wrong syntax for PHP ctype function.
You are taking the POST input value in the variable named $value and in query you are sending $values
I have corrected the code.
Can you please try the below code
<?php
define('DB_NAME', 'phpmyadminName');
define('DB_USER', 'phpmyadminUser');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value = $_POST['input1'];
if($value!=''){
$sql = "INSERT INTO demo (input1) VALUES ('".$value."')";
}
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
mysql_close();
?>
Below is correct code for the issue. I have checked that when you refresh your page it will create new blank entry in database and also the variable name is wrong.
You have to check for the Request method. This
$_SERVER['REQUEST_METHOD'] === 'POST'
will check the form method and it will prevent the blank entries in database.
<?php
define('DB_NAME', 'test');
define('DB_USER', 'root');
define('DB_PASSWORD', 'mysqldba');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
//Test for request method
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
//echo $sql;die;
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
}
mysql_close();
?>
I am very new to PHP so this is my first attempt to connect to a mysql database through a php file and I am getting this message. I dont know how much anyone can help me with this or if at least someone can guide me to a right direction
Can not use : soum_email1:
And my php looks like this
<?php
define('DB_NAME', 'soum_email1');
define('DB_USER', 'soum_email');
define('DB_PASSWORD', 'Qe232f9');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!link){
die('could not connect:' . mysql_error());
}
$db_selct = mysql_select_db(DB_NAME, $link);
if(!$db_selected){
die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
echo 'connection sucessful';
?>
You are assigning the mysql_select_db() function $db_selct, but then checking $db_selected (which with the code you've posted is always falsey.
Also, link should be $link (on line 9).
Your code should be:
define('DB_NAME', 'soum_email1');
define('DB_USER', 'soum_email');
define('DB_PASSWORD', 'Qe232f9');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('could not connect:' . mysql_error());
}
$db_selct = mysql_select_db(DB_NAME, $link);
if(!$db_selct){
die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
echo 'connection sucessful';
You should note though that the mysql_* family of functions are now deprecated, and you should consider using MySQLi or PDO.
First of all:
Please use the pdo or mysqli database like Quentin wrote in the first comment.
Further you should name your variables right,
$db_selct = mysql_select_db(DB_NAME, $link);
and
if(!$db_selected){
die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
have different variable names.