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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀