-1

I'm calling a function on a page on a test php website to display a html form to record data to be submitted to my SQL database via ajax, but for some reason no data is being inserted. I have all the required bootstrap, js, ajax links in an included header.php and I have other forms submitting to tables in the same database on the test site which are working with no issues so the database connection seems to be fine, which is why I'm struggling so much to work out why the data isn't being inserted.

This is a localhost test server on MAMP using MySQL 5, PHP 5.

My file with the code to display a html entry form when called is in functions.php:

<?php

    session_start();

    $link = mysqli_connect("localhost", "user", "pass", "tables");

    if (mysqli_connect_errno()) {

        print_r(mysqli_connect_error());
        exit();
}

// other code ...

function displayTextInput() {

  global $link;

  echo '
  <div class="form">
    <div class="form-group">
    <textarea class="form-control" id="textDesc" placeholder="Description"></textarea>
    <textarea class="form-control" id="textType" placeholder="Type"></textarea>
  </div>
    <button id="postTextButton" class="btn btn-primary">Post Text</button>
  </div>';
}
?>

My ajax call is in a script inside a footer.php:

<script>

$("#postTextButton").click(function() {

            $.ajax({
                type: "POST",
                url: "actions.php?action=postText",
                data: {textDesc: $("#textDesc").val(), textType: $("#textType").val()},
                success: function(result) {

                    if (result == "1") {

                      alert("ajax successful");

                    } else if (result != "") {

                        alert("ajax unsuccessful");

                    }
                }

            })

        })

</script>

And finally my SQL is within a actions.php:


if ($_GET['action'] == 'postText') {

          if (!$_POST['TextDesc']) {

            echo "Your text description is empty!";


          } else {

            $textDesc = mysqli_real_escape_string($link, $_POST['textDesc']);
            $textType = mysqli_real_escape_string($link, $_POST['textType']);
            $userIDForTable = mysqli_real_escape_string($link, $_SESSION['id']);


            mysqli_query($link, "INSERT INTO text (`description`, `type`, `userid`) VALUES ('$textDesc', '$textType', '$userIDForTable')");

              echo "1";
            }
          }

I can echo out statements within the actions.php else { } section so the code is definitely connected to there but I really just can't seem to work out why. I've even linked the above code to a different php table within the same database and had it working so I just can't get my head around why this isn't working. There are no errors in the console I can see either. Any help would be greatly appreciated.

Thanks

Luke
  • 37
  • 7
  • [Have you tried to enable MySQLi exception mode?](https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments/22662582#22662582). Are you getting any errors? – Dharman May 14 '19 at 22:41
  • Inside actions.php, are you making sure that $link is properly defined again? Just like in functions.php, $link needs to be the result of a mysqli_connect() call. Also, you should take a look at the mysqli error reporting - https://www.php.net/manual/en/mysqli.error.php – Joshua T May 14 '19 at 22:42
  • My actions.php has in ```include("functions.php");``` at the top so it has access to the $link, so I don't think it's this. I've tried again redefining the link within the if else statement and that hasn't worked. – Luke May 14 '19 at 22:44

1 Answers1

0

The word text is a special word in MySQL and I think your query is throwing an error. add this to your code after your insert statement:

var_dump(mysqli_error($link));

I know I should post this as a comment but I didn't have enough point to post a comment.

  • Thanks very much for this - I've changed the table names and id names of my html elements and it seems to have worked and be inserting fine. Much appreciated. – Luke May 14 '19 at 23:02
  • 1
    Thanks for choosing my answer. Here are some tips if you are new to PHP. Try to use write object oriented style and use [**prepared statement**](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of mysqli_real_escape_string because mysqli_real_escape_string doesn't really prevent sql injection – Soli Technology LLC May 14 '19 at 23:11