Connect to go daddy hosted Database using php connection - php

i keep getting a 500 internal server error when trying to submit my form. The Database is hosted in godaddy.com. I think i have hostname wrong. here is my code.
<?php
define('DB_NAME','Survey');
define('DB_USER','BC');
define('DB_PASSWORD','*********');
define('DB_HOST','50.62.209.72.secureserver.net');
$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';*/
$value = $_POST['last'];
$value2 = $_POST['first'];
$value3 = $_POST['phone'];
$value4 = $_POST['email'];
$value5 = $_POST['fax'];
$value6 = $_POST['company'];
$sql = "INSERT INTO crofton (last, first, Phone, Email, Fax, Company) VALUES ('$value', '$value2', '$value3', '$value4', '$value5', '$value6')";
if (!mysql_query($sql)) {
die('Error: '. mysql_error());
}
mysql_close();
?>

yes your host name is wrong, Read Viewing Your Database's Details to know more about godaddy Database Details

Related

mysql api submite towice

I tried many times to submit the form when it submitted it repeated the submission twice on the data. I don't understand why,please help me. and when I put the header location it doesn't work ever
here is the code
<?php
$name= $_POST['form_name'];
$mrn= $_POST['form_mrn'];
$mobile= $_POST['form_mobile'];
$link = mysql_connect('server', 'user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbname');
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
mysql_set_charset('utf8',$link);
$query = "INSERT INTO pharmacy ( name , mrn , mobile ) VALUES ('$name', '$mrn', '$mobile')";
$result = mysql_query($query);
header('Location: form.html');
$link->close();
?>
<?php
$name= $_POST['form_name'];
$mrn= $_POST['form_mrn'];
$mobile= $_POST['form_mobile'];
$link = mysql_connect('server', 'user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbname');
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
mysql_set_charset('utf8',$link);
//Just Put this code before inserting
if($name !=""){
$query = "INSERT INTO pharmacy ( name , mrn , mobile ) VALUES ('$name','$mrn', '$mobile')";
$result = mysql_query($query);
header('Location: form.html');
}
$link->close();
?>
Just check condition before insert. Let me know if facing same issue.

php error when submitting comment

I have an error in the php code for submitting a comment, the problem is the 4th line of code:
php_network_getaddresses: getaddrinfo failed: the requested name is
valid, but no data of the requested type was found.
Any ideas?
<?php
if( $_POST )
{
$con = mysql_connect('%', 'myuser', 'mypassword');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("_mysite", $link);
$users_name = $_POST['name'];
$users_email = $_POST['email'];
$users_website = $_POST['website'];
$users_comment = $_POST['comment'];
$users_name = mysql_real_escape_string($users_name);
$users_email = mysql_real_escape_string($users_email);
$users_website = mysql_real_escape_string($users_website);
$users_comment = mysql_real_escape_string($users_comment);
$articleid = $_GET['id'];
if( ! is_numeric($articleid) )
die('invalid article id');
$query = "
INSERT INTO `_mysite`.`comments` (`id`, `name`, `email`, `website`,
`comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name',
'$users_email', '$users_website', '$users_comment',
CURRENT_TIMESTAMP, '$articleid');";
mysql_query($query);
echo "<h2>Thank you for your Comment!</h2>";
mysql_close($con);
}
?>
The % is mysql's wildcard, when you use it for your user's allowed host, it means 'any host', in your case here it should probably be localhost.
Not related to your question, but if you're not aware of it you should consider moving away from mysql_ functions which are deprecated, mysqli is the recommended replacement.
Replace $link with $con in this line mysql_select_db("_mysite", $link);.
This is the first time i am seeing any server name as %. I don't have much idea about it. But, you can replace % with localhost or port number or IP Address
<?php
if($_POST)
{
$con = mysql_connect('localhost', 'myuser', 'mypassword');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("_mysite", $con);
$users_name = mysql_real_escape_string($_POST['name']);
$users_email = mysql_real_escape_string($_POST['email']);
$users_website = mysql_real_escape_string($_POST['website']);
$users_comment = mysql_real_escape_string($_POST['comment']);
$articleid = $_GET['id'];
if(!is_numeric($articleid))
die('invalid article id');
$query = "INSERT INTO `_mysite`.`comments` (`id`, `name`, `email`, `website`,`comment`, `timestamp`, `articleid`)
VALUES (NULL, '$users_name','$users_email', '$users_website', '$users_comment',CURRENT_TIMESTAMP, '$articleid')";
mysql_query($query);
echo "<h2>Thank you for your Comment!</h2>";
mysql_close($con);
}
?>
Please stop using mysql* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi.
Using MySqli
<?php
if($_POST)
{
$con = mysqli_connect("localhost", "myuser", "mypassword", "_mysite");
if (!$con) {
die('Could not connect: ' . mysqli_connect_error());
}
$users_name = mysqli_real_escape_string($con, $_POST['name']);
$users_email = mysqli_real_escape_string($con, $_POST['email']);
$users_website = mysqli_real_escape_string($con, $_POST['website']);
$users_comment = mysqli_real_escape_string($con, $_POST['comment']);
$articleid = $_GET['id'];
if(!is_numeric($articleid))
die('invalid article id');
$query = "INSERT INTO `_mysite`.`comments` (`id`, `name`, `email`, `website`,`comment`, `timestamp`, `articleid`)
VALUES (NULL, '$users_name','$users_email', '$users_website', '$users_comment',CURRENT_TIMESTAMP, '$articleid')";
mysqli_query($con,$query)
echo "<h2>Thank you for your Comment!</h2>";
mysqli_close($con);
}
?>
Refer php_network_getaddresses: getaddrinfo failed: for more details.

I want to make my password to contain lowercase, uppercase, 8 characters and a character

I want to make my password to contain lowercase, uppercase, 8 characters and a character.
Here is my code:
<?php
define('DB_NAME', 'baza');
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('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
echo 'Your account has been created succesfully! Click here to login.';
$value = $_POST['name'];
$value2 = $_POST['surename'];
$value3 = $_POST['genre'];
$value4 = $_POST['numrikarteles'];
$value5 = $_POST['email'];
$value6 = $_POST['username'];
$value7 = $_POST['password'];
if (!preg_match('/[^A-Za-z0-9]+/', $value7) || strlen($value7) < 8) {
echo "Invalid password!";
} //- kushti per ta kontrollu passwordin
$sql = "INSERT INTO user (name, surename, genre, numrikarteles, email, username,password) VALUES ('$value', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
?>
If you want to restrict user to enter minimum 8 character password which will contain upper and lower case letter and one special character you can use regular expression like
^((?=(?:.*[a-zA-Z]){7})(?=(?:.*[##$%^?])).{8})$

PHP Error: No database selected (Hostgator.com)

I have a problem with inserting data to MySQL with PHP.
When I run this code on my Hostgator hosting I get error like this:
No database selected
Here is my code:
$dbh= mysql_connect("localhost", $username, $password);
// or die ('I cannot connect to the database because: ' . mysql_error());
if(!$dbh)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("contafe_tipster", $dbh);
//$conn = mysqli_connect($servername, $username, $password, $dbname);
//Check connection
/*
if (!$conn) {
// Connection failed return 0
//die("Connection failed " . mysqli_connect_error());
echo "1";
}
*/
//Posted parameters
$pName = $_POST['name'];
$pCountry = $_POST['country'];
$pCity = $_POST['city'];
$pLocationX = $_POST['locationX'];
$pLocationY = $_POST['locationY'];
$pDescription = $_POST['description'];
$pMobileNumber = $_POST['mobile'];
$pOtherNumber = $_POST['phone'];
$pOpens = $_POST['opens'];
$pCloses = $_POST['closes'];
$add_DB = "INSERT INTO Places (Id, Name, Country, City, LocationX, LocationY, Description, Mobile, Phone, Opens, Closes)
VALUES(NULL, '$pName', '$pCountry', '$pCity', '$pLocationX', '$pLocationY', '$pDescription', '$pMobileNumber', '$pOtherNumber', '$pOpens', '$pCloses')";
if (mysql_query($add_DB, $dbh)) {
//if success return 1
echo "0";
}
else {
//if error return -1
//echo "2";
die('Error: ' . mysql_error());
}
mysql_close($dbh);
Are you sure that db name is correct?
Try this way of connection with db:
mysql_connect ($dbhost,$dblogin,$dbpass) or die ("Can't connect to database");
mysql_select_db($db) or die ('Wrong databse!');

Get notified by email when people fill my form

Okey so basically I have a form so if people are interested in getting a website of me so they can fill it out and it was sent directly to my database via a php script. My question is can I do something so that I also get the message with all the information on my email? You can check out my currently script down below.
<?php
define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'server');
$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['ID'];
$value2 = $_POST['name'];
$value3 = $_POST['email'];
$value4 = $_POST['message'];
$sql = "INSERT INTO table (ID, name, email, message) VALUES ('$value', '$value2', '$value3', '$value4')";
if (!mysql_query($sql)){
die('Something went wrong: ' . mysql_error());
}
header("Location: http://example.com");
mysql_close();
?>

Categories