I am trying to create a simple html form that hold the information via MySQL however I am having some problem connecting to the database. I think $db_selected = mysql_select_db(DB_NAME, $link); is giving me the problem. FYI DB_NAME is the database table I want to use.
My HTML
<form action="demo.php" method="post">
<div class="form-group">
<label for="firstname">First Name:</label>
<input type="text" class="form-control" id="firstname" name="first_name">
</div>
<div class="form-group">
<label for="lastname">Last Name:</label>
<input type="text" class="form-control" id="lastname" name="last_name">
</div>
<div class="form-group">
<label for="dob">Date of birth:</label>
<input type="password" class="form-control" id="dob" name="date_of_birth">
</div>
<div class="form-group">
<label for="pob">Place of birth:</label>
<input type="text" class="form-control" id="pob" name="place_of_birth">
</div>
<div class="form-group">
<label for="government">Your Local government:</label>
<input type="text" class="form-control" id="government" name="government">
</div>
<div class="form-group">
<label for="qualifications">Qualifications:</label>
<textarea class="form-control" rows="5" id="qualifications" name="qualifications"></textarea>
</div>
<div class="form-group">
<label for="skills">Skills:</label>
<textarea class="form-control" rows="5" id="skills" name="skills"></textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
My PHP
<?php
define('DB_NAME', 'forms1');
define('DB_USER', 'c88888');
define('DB_PASSWORD', '*******');
define('DB_HOST', 's********.com');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db_selected = mysqli_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysqli_error());
}
$value = $_POST['first_name'];
$value2 = $_POST['last_name'];
$value3 = $_POST['date_of_birth'];
$value4 = $_POST['place_of_birth'];
$value5 = $_POST['government'];
$value6 = $_POST['qualifications'];
$value7 = $_POST['skills'];
$sql = "INSERT INTO demo (first_name, last_name, date_of_birth, place_of_birth, government, qualifications, skills) VALUES ('$value', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7')";
if (!mysqli_query($sql)) {
die('Error: ' . mysqli_error());
}
mysqli_close();
?>
When writting your SQL query as a string, separate the database fields using backticks (`).
You're using '$value'
inside a single quote, which means PHP understands that as a literal $value
value, instead of a variable.
You should change query to:
$sql = "INSERT INTO `demo` (`first_name`, `last_name`, `date_of_birth`, `place_of_birth`, `government`, `qualifications`, `skills`)
VALUES ('" . $value . "', '" . $value2 . "', '" . $value3 . "', '" . $value4 . "', '" . $value5 . "' , '" . $value6 . "', '" . $value7 . "')";
Just change your code where you have db_selected to:
$db_selected = mysqli_select_db($link, DB_NAME);
And make sure you escape your values before entering them into database by using prepared statements or mysqli_real_escape_string()
and its better to write your values with brackets like ('{$value1}', '{$value2'})
Try giving DB_HOST to "localhost", because almost every server stores their database in localhost itself. Try below code to connect to your database and also make sure to give the right database name.
<?php
header("Access-Control-Allow-Origin: *");
define('DB_NAME', 'name');
define('DB_USER', 'user');
define('DB_PASSWORD', '****');
define('DB_HOST', 'localhost');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(!$dbc){
die('could not connect: ' .mysql_error());
}
?>