1

I'm trying to send some Data to a MySQL Database that I have stored on a server. When I press the submit button, my PHP scripts let me know that the connection to the DB has been successful, but the insertion of data has not. I can't find any issue in the Error logs, or an obvious problem when I examine the code via Google Chrome's developer tools.

Below is the HTML File;

<html>
<head>
    <title>Home</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"/>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

    <script type="text/javascript" src="http://example.com/JS/functions.js"></script> 

    <script type="text/javascript" src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

    //CSS
    <style>
        .ui-page { 
           background: #4990E2;
        }
               p {
           color: #ffffff;
        }
    </style>
</head>
       div data-role="page" id="page1">
        <div data-role="header" data-theme="a">
    <h1>Welcome</h1>
      </div>
                  <!-- Main page content goes under this line -->
 <div data-role="main" class="ui-content">
  <br>
  <br>
  <br>
 <form id="myForm" method="post" action="include.php">
   <label for="rating">How are you feeling?</label>
   <input type="range" name="rating" id="rating" value="50" min="0" max="100" data-popup-enabled="true">
   <input id="submit" type="submit" data-inline="true" value="Submit">
 </form>
  <br>
  <br>
      <span id="result"></span>
  <br>
      <p>Some text</p>
 </div>

 <div data-role="footer" data-position="fixed" data-theme="b">
    <div data-role="navbar">
            <ul>
                <li><a href="#page1" class="ui-btn-active ui-state-persist"  data-transition="none">Home</a></li>
                <li><a href="#page2" data-transition="none">Example</a></li>
                <li><a href="#page3" data-transition="none">Example</a></li>
                <li><a href="#page4" data-transition="none">Example</a></li>
            </ul>
        </div>
    </div>
</div>
                                <!-- Page ends here -->
                                <!--                -->

Below is the PHP file (db.php);

<?php

//put your connection code here
    $servername = 'localhost';
    $username = 'user_admin';
    $password = '12345-678';
    $dbname = 'my_DB';

//create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    } 
    echo "Connected successfully, <br>";
?>

Below is the PHP file(include.php);

<?php

include 'db.php';

$rating = $_POST['rating'];

$sql="INSERT INTO user Values('$rating')";
if(mysqli_query($conn,$sql)){
       echo "Successfully Inserted";
 }  else {
        echo "Did NOT insert";
 }
?>

Below is the Javascript file (functions.js);

// AJAX-ify the slider
$(document).on("pagecreate","#page1",function()
{

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

$.post( $("#myForm").attr("action"),
   $("#myForm :input").serializeArray(),
     function(info){ $("#result").html(info);
   });
});

$("#myForm").submit( function() {
return false;
  });
});

The MySQL table is called 'user' and the column is called 'rating'. The column is set as int(3) [this should match with the slider max value of 100?].

Can anyone pinpoint why the Data isn't being inserted into the DB?

12
  • 2
    Have you captured the contents of your $sql variable and tried to run them manually against your database? You could also change your echo from "Did NOT insert" and use the mysqli_error() function to get a sense of what's going on. Commented Dec 1, 2016 at 1:12
  • Try put mysqli_query($conn,$sql) or die(mysqli_error($conn)) and see if got any errors from mysql Commented Dec 1, 2016 at 1:13
  • 1
    @MichaelO'Brien Thanks for your efforts. I got it working. Using the mysqli_error() function instead of the echo "Did NOT Insert" got it working. Commented Dec 1, 2016 at 1:42
  • 1
    @NorlihazmeyGhazali It's working now. I appreciate your time. Thanks for trying. Commented Dec 1, 2016 at 1:43
  • 1
    @NorlihazmeyGhazali The data wasn't inserting into the DB until I changed the else { echo "Did NOT insert"} to echo("Error description: " . mysqli_error($conn)); Commented Dec 1, 2016 at 1:57

1 Answer 1

1

change this

$sql="INSERT INTO user Values('$rating')";

to this

$sql="INSERT INTO user (`rating`) VALUES ('$rating')";

rating would be the column name for that field in the db, if the table has more than one column then the statement need to say in which column it need to be placed.

also to show errors of the statement use this

if(mysqli_query($conn,$sql) or die(msqli_error($conn))){
    ... your code here > it worked
}else{
    ... your code here > it failed
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.