dongqian6554 2017-11-22 23:44
浏览 59
已采纳

从JavaScript函数通过AJAX调用PHP在mySQL(AMPPS localhost)中存储数据似乎不起作用

After one whole day of trying various things I learned from other threads, I still can't get my code to work. I'm basically just trying to send data from a JavaScript function to a localhost database (server created using AMPPS).

Currently, I have a JavaScript file with the following function:

function WritetoDB() {
    alert('Reached. No problem getting here.');
    $.get("savedata.php");
}

I've also tried $.post, $.ajax, (with their appropriate parameters and such) but none of them seem to work either. Originally, the function was supposed to receive and pass on some variables that need to be stored on the database, but I'm trying to get it to work first before passing on any variables.

This is the 'savedata.php':

<?php
$servername = "localhost";
$username = "root";
$password = "mysql";
$databaseName = "database";
$tableName = "table";

$conn = new mysqli($servername, $username, $password);

$A = mysqli_real_escape_string($conn, 'this is A');
$B = mysqli_real_escape_string($conn, 'this is B');
$C = mysqli_real_escape_string($conn, 'this is C');

$sql = "INSERT INTO `database`.`table` (columnA,columnB,columnC)
    VALUES ('$A', '$B', '$C')";

$conn->close();

I tried executing the PHP code in the index.php file and it works (data was sent to the database). However, it doesn't work when I'm trying to call it from a function in the JavaScript file. Most solutions from guides/forums I referred to seems to work for others, but not on mine.

Any help is greatly appreciated and thank you in advance.

  • 写回答

1条回答 默认 最新

  • dougua9165 2017-11-23 11:08
    关注

    In such conditions it is preferred that you try to run the php code standalone with some predefined / dummy values to check if it is working correctly or sending appropriate response that we need from it.

    To troubleshoot your php file not saving data into the database first of all you need to do the following.

    Remove syntax error on these line

    $B = mysqli_real_escape_string(($conn, 'this is B'));
    $C = mysqli_real_escape_string(($conn, 'this is C'));
    

    because of extra parenthesis wrapped change them to

    $B = mysqli_real_escape_string($conn, 'this is B');
    $C = mysqli_real_escape_string($conn, 'this is C');
    

    then you are not using the database parameter in you connection change the line

    $conn = new mysqli($servername, $username, $password);
    

    to

    $conn = new mysqli($servername, $username, $password,$databaseName);
    

    after that you need to execute this query which is the main thing in the insertion of the data into the database, which you are missing kindly add these line before the $conn->close()

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

    see here for basic help on how to connect php and mysql database and insert data with sql query

    w3schools

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试