Error adding data to mysql with javascript and php

42 Views Asked by At

I cannot connect to the database in any way. I installed xampp on my computer and ran Apache and mysql services. But I cannot record anything. I can't query.

I wanted to save the value entered in the textboxes to the mysql database with the help of a button, but I could not succeed. This time, I thought I would at least enter the fixed record into the database, but I couldn't do that either. I wish I could save the value entered in the inputs to the database. I guess it's not connecting to the database.

<input type="button" value="Home" class="homebutton" id="btnHome" onClick="Javascript:window.location.href = '127.0.0.1\xampp\htdocs\tester\conn.php';" />
<?php
$servername = "localhost:3306";
$username = "deneme";
$password = "deneme";
$dbname = "deneme";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO please (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";


if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?> 
1

There are 1 best solutions below

3
Abhishek Kumar On BEST ANSWER

What you are trying to do should be done using JavaScript function you cannot bind onchange function directly to php script. you have to use either pure javascript function or using javascript library like jquery to make ajax request call to the php script and add the data to mysql database

Check Below code using pure javascript

index.html

 <input type="button" value="Home" class="homebutton" id="btnHome" onClick="add_record();" />

 <script type="text/javascript">
    function add_record(){
     
        var data= document.getElementById("btnHome").value; // get value from input
        xhttp.open("GET", "php_file.php?field="+data, true); // get request url with field parameter  
        xhttp.send(); // make request

    }
 </script>

php_file.php

 <?php
$servername = "localhost:3306";
$username = "deneme";
$password = "deneme";
$dbname = "deneme";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$field_data = $_GET['field'];  // get field parameter from request url 


// insert $filed_data into database . You can add as much as fields in request parameter
$sql = "INSERT INTO please (field) VALUES ('$field_data')";



if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?> 

Both You html file and php file should be in same folder. you cannot add xampp folder and htdocs folder in url as htdocs is the main public folder to access by url and anything above this cannot be accessed via url. please have a look on how to access url on xampp apache server. Ex: your request url on xampp installed on C: drive should be like this

http://127.0.0.1/my_project/index.html

folder structure should be C:\xampp\htdocs\my_project\
my_project should have both files.

EDIT :

How are you doing it can you please explain

are you able to access http://127.0.0.1/ try using http://localhost/ reply what you are getting.

Your URL should be
http://127.0.0.1/tester/conn.php or http://localhost/tester/conn.php