database is connected but data is not storing in xamp server - php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "youtube";
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
//if ($conn->connect_error) {
// die("Connection failed: " . $conn->connect_error);
//}
//echo "Connected successfull
if(isset($_POST['Submit']))
{
if(mysqli_num_rows(mysqli_query($conn," INSERT INTO admin where
Name='".$_POST['Name']."' , Password='".$_POST['Password']."', E-
mail='".$_POST['E-mail']."' and country='".$_POST['country']."'"))>0)
{
echo 'signup successfull';
}
else
{
echo 'incorrect username password';
}
}
?>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
<?php echo "<br>"?>
Your country is: <?php echo $_POST["country"]; ?>
<?php echo "<br>"?>
Your password is: <?php echo $_POST["pwd"]; ?>
<?php echo "<br>"?>

the problem in data inserts query.
$sql = "INSERT INTO admin(name, Password, mail,country)
VALUES ("$_POST['Name']", "$_POST['Password']", "$_POST['E-mail']","$_POST['country']")";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
try this

Related

not able to input date type data properly in my database

every thing is getting inputted correctly but date is being submitted into database like 0000-00-00. please help!! i tried to echo my date before posting it into dob and it coming out to be correct as inserted into the form. but when i check my database the dob columnsgets updates with a 0000-00-00.
here is the code:
<?php
if (isset($_POST['submit'])) {
echo "<pre>";
var_dump($_POST);
echo "</pre>";}
$username = $_POST['username'];
$password = $_POST['password'];
//$salary = $_POST['salary'];
$birthdate = $_POST['year'] . '-' . $_POST['month']. '-' .$_POST['day'];
echo $birthdate."<br>";
if(!empty($username)){
if(!empty($password)){
if(!empty($birthdate)){
$servername = "localhost";
$usernamei = "root";
$passwordi = "password";
$databasei = "regis";
// Create connection
$conn = new mysqli($servername, $usernamei, $passwordi, $databasei);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
echo $birthdate;
$sql = "INSERT INTO iocl (user_name, password,dob)
values ('$username','$password','$birthdate')";
if($conn->query($sql)){
echo "new record is inserted sucess";
}
else{
echo "error: ".$sql."<br>".$conn->error;
}
$conn->close();
}}}}
?>

Why I am getting a lines under the table in phpmyadmin(localhost)

I am trying to send a data from android studio, but I am getting lines under the table instead of assigning data.
Dont know where I am gone wrong.Plz help me.Thanks in advance.
This is my PHP code
add_employee
<?php
include('connection.php');
if (isset($_POST["name"])){
$emp_name = $_POST["name"];
echo $emp_name;
echo "is your name";
}
else{
$emp_name = NULL;
echo "POST filename is not assigned";
}
$success = 0;
$status = "Active";
$sqli = "INSERT INTO `employee` (`emp_name`) VALUES ('$emp_name')";
if(mysqli_query($conn,$sqli)){
$success=1;
}
$response["success"]=$success;
die(json_encode($response));
mysqli_close($conn);
?>
Connection.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(!$conn) {
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($conn,'student');
?>
You have so many errors in your code. no db name, no proper query definition. you can use this simple code:
Connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "slim";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Inserting employee code:
<?php
if (isset($_POST["name"])){
$emp_name=$_POST["name"];
echo $emp_name;
echo "is your name";
}
else{
$emp_name = null;
echo "POST filename is not assigned";
}
$success=0;
$status="Active";
$sql = "INSERT INTO employee (name)
VALUES ('$emp_name')";
if ($conn->query($sql) === TRUE) {
$success=1;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
It's working and easy to understand for you.

Deleting a data in Database using php

I need to delete a data on database by using PHP code, i have written the code but there is some error message.
here is the php code(del.php):-
<!DOCTYPE html>
<html>
<body>
<?php
$conn = mysql_connect('localhost', 'root','');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM register WHERE name='' ";
if ($conn->query($sql) === TRUE)
{
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
</body>
</html>
The database name is 'selva' and the table name is 'register', in database the file names are "Name,Email,Contact,Address", i need to delete the name or email or contact . how to delete!!
//this will delete the whole row:
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "your_password"; // add your pw from the here
$dbname = "selva";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//this will delete the whole row
$sql = "DELETE FROM register WHERE 1 ";
if ($conn->query($sql) === TRUE)
{
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
</body>
</html>
//this will delete only the COLUMNS you set here:
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "your_password"; // add your pw from the here
$dbname = "selva";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "ALTER TABLE register DROP Name, DROP Email; DROP Contact; ";
if ($conn->query($sql) === TRUE)
{
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
</body>
</html>
hope it will help you :)

Config file doesn't connect with mysql database

I'm using MAMP and the problem here is the config file that won't to connect with a database,
<?php
define("HOST","localhost");
define("USER", "root");
define("PASS", "root");
define("DB", "inserttest");
$con = mysqli_connect(HOST,USER,PASS,DB);
?>
...
<?php
include 'config.php';
$firstname = $_POST['firstname'];
$fathername = $_POST['fathername'];
$grandfathername = $_POST['grandfathername'];
$familyname = $_POST['familyname'];
$sql = mysqli_query($con,"INSERT INTO subventions (firstname,fathername,grandfathername,familyname) VALUES ('$firstname','$fathername','$grandfathername','$familyname')");
if ($sql == true) {
echo '<b> تم الإرسال </b>';
} else {
echo '<b> فشل الإتصال </b>';
}
mysqli_close($con);
?>
...............................................................
You can use some functions to check weather the connection has established or not
you can do like this
<?php
define("HOST","localhost");
define("USER", "root");
define("PASS", "root");
define("DB", "inserttest");
$con = mysqli_connect(HOST,USER,PASS,DB);
if(mysqli_connect_errno())
{
die("Some Error Occured While COnnection to database :".mysqli_connect_error());
}
?>
Hope this will help
You can check existing file path.
Try Again.
<?php
//include(foldername/filename);
include('config.php') or die('File path error');
try this
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "inserttest";
// Create connection
$conn = new mysqli($servername, $username,
$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); } ?>
include('config.php');
$firstname = $_POST['firstname'];
$fathername = $_POST['fathername'];
$grandfathername = $_POST['grandfathername'];
$familyname = $_POST['familyname'];
$sql = "INSERT INTO subventions
(firstname,fathername,grandfathername,familyname) VALUES
('$firstname','$fathername','$grandfathername','$familyname')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully"; } else {
echo "Error: " . $sql . "" . $conn->error; }
?>

Unable to select student: Access denied for user ''#'localhost' to database 'student'

Unable to select student: Access denied for user ''#'localhost' to
database 'student'
Error when tried to run the following codes.
none of the method described here works this is the code i've used
<?php
$servername = "localhost";
$username = "amal";
$password = "ZtFnzcDQB5K9hutM";
$dbname = "student";
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!mysql_select_db("student")) {
echo "Unable to select student: " . mysql_error();
exit;
}
$sql = "SELECT id as Name, Course, DOB, Gender FROM application WHERE userstatus = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["id"];
echo " ";
echo $row["Name"];
echo " ";
echo $row["Course"];
echo " ";
echo $row["DOB"];
echo " ";
echo $row["Gender"];
echo "<br><br>";
}
mysql_free_result($result);
$conn->close();
?>
Try this something like this :
<?php
$servername = "localhost";
$username = "amal";
$password = "ZtFnzcDQB5K9hutM";
$dbname = "student";
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!mysqli_select_db("student")) {
trigger_error('Database connection failed : ' .$conn->connect_error , E_USER_ERROR);
exit;
}
?>
You used both mysqli and mysql function. Use any one of then it will solve.

Categories