Hey i am using Mamp on imac and my problem is that when i hit the submit button (on a post form) to enter the data then nothing shows up and the database remains empty.
Here is my code :
<?php
define('DB_NAME', 'demob');
define('DB_USER','brom');
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('cant use' . DB.NAME . ' : ' .mysql_error());
}
$value = $_POST['input1'];
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_close();
?>
You are not executing a query.
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_query($sql);
You should know that, the method you are using to connect to mysql is deprecated now. please read up about PDO or mysqli
Related
I am having trouble connecting to my localhost database with php. It feels like I have followed every tutorial there is.
current php code:
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="webutvshop";
$username="dbconnect";
$password="password";
//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
die ('Can\'t select database: ' . mysql_error());
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysql_close($link);
?>
The issue stands, when I acess the file locally on my computer I do not get any answer at all from it. I've tried with many other, yet I do not get any answers from them!
I need help in order to keep working on my schoolproject, thanks.
Stop using mysql_*, because they are deprecated officially. Use mysqli_* or PDO for this purpose. An example:-
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="stackquestion";
$username="root";
$password=""; // check once with empty password and once with some password that you tried already
//DO NOT EDIT BELOW THIS LINE
$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysqli_select_db($link,$database);
if (!$db_selected) {
die ('Can\'t select database: ' . mysqli_error($link));
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysqli_close($link);
?>
Output:- http://prntscr.com/7cbr5j
Can you also verify you can connect to the database from a command line:
mysql -u dbconnect -ppassword -h localhost webutvshop
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 trying to link a HTML form to a mySQL database using a wamp server, the server connects to the database perfectly however it wont let me post my form data due to a error message Undefined variable: _post can somebody help solve whats going wrong below is my PHP code.
<?php
define ('DB_NAME', 'feedback');
define ('DB_USER', 'root');
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('cant use' . DB_NAME . ':' . mysql_error());
}
$_value = $_post['name'];
$_value1 = $_post['feedback'];
$sqlname = "INSERT INTO feedback (name) VALUES ('$_value')";
if (!mysql_query($sqlname)){
die('error:' . mysql_error());
}
$sqlfeedback = "INSERT INTO feedback (feedback) VALUES ('$value1')";
if (!mysql_query($sqlfeedback)){
die('error:' . mysql_error());
}<?php */
mysql_close();
?>
Variables are case sensitive in php, so it should be $_POST['name']and not$_post['name'].
Keep in mind tho, fonctions aren't case sensitive.
Ok, Ive been playing around with PHP and databases. Note, Im doing all my work on a webserver hosted by bluehost.
So I went into the cpanel and set up a database called gagalugc_stocks.
I also setup a username (test) and a password (password) and added them to be able to control the databse. Below is my script in an attempt to connect to it, not it is also on the server.
<?php
$connect = mysql_connect('localhost','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
-Thanks
NOTE: The problem is that it can never reach the database.
<?php
$connection = mysql_connect ('localhost', 'test', 'password')
or die ('<b>Could not connect: </b>' . mysql_error());
$result = mysql_select_db("gagalugc_stocks")
or die ('<b>Could not connect: </b>' . mysql_error());
?>
Didn't check for port when adding database make sure to specify host and port
(localhost:2083)
How about
<?php
$connect = mysql_connect('localhost:2083','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
For local system:-
$con = mysql_connect("localhost","root","");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
For website or remote server:-
$con = mysql_connect("localhost","username","password");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
for more please visit : Algosoftwares