0

After many attempts and researches online I could not find any solution to why I have an error in the database insertion query. The error in question is:

data: Error in query: INSERT INTO Signaling (Severity) VALUES ('gravitaAlta').

I ask desperately where the problem is. Whether it can be in the database itself or in PHP or in my Ajax

HTML

            <form method="post" id="formHomepage">
            <div>
                <input type="email" name="email" required="" placeholder="Email" maxlength="30" autocomplete="on">
            </div>
                <div >
                <a  href="#" id="fotocamera-button" >
                    <label for="fileInput">Carica/Scatta Foto</label>
                </a>
                        <input type="file" accept="image/*" name="file" required="" id="fileInput">
                <img class="img-thumbnail d-none" src="#" id="foto-screen">
            </div>
                <div>
                <select name="categoria" required="">
                    <optgroup label="Seleziona il problema">
                        <option value="mantoStradale" selected="" >Manto Stradale</option>
                        <option value="idrico">Idrico</option>
                        <option value="luceGas">Luce e Gas</option>
                        <option value="rfifiuti">Rifiuti</option>
                        <option value="telecomunicazioni">Telecomunicazioni</option>
                    </optgroup>
                </select>
            </div>
                <div class="form-group">
                <select class="form-control" name="gravita">
                    <optgroup label="Seleziona la tua gravità">
                        <option value="gravitaAlta" selected="">Gravità Alta</option>
                        <option value="gravitaMedia">Gravità Media</option>
                        <option value="gravitaBassa" >Gravità Bassa</option>
                    </optgroup>
                </select>
            </div>
                <div class="form-group">
                <textarea class="form-control" rows="5" name="descrizione" placeholder="Descrizione" maxlength="120"></textarea>
            </div>
                <div class="d-flex flex-row">
                <button type="submit" >Invia Segnalazione</button>
                <button type="reset">Reset</button>
            </div>
         </form>

AJAX

                      var gravita = $("#formHomepage select[name='gravita'] :selected").val();

                      var item = '&gravita=' + gravita;

                      $.ajax(
                      {
                          type: 'POST',
                          url: 'https://civicsensethecitizen.altervista.org/php/formCopy.php',
                          data: item,
                          success: function(data)
                          {
                            console.log("data: " + data);
                          },
                          error: function()
                          {
                            alert("Connessione non riuscita");
                          }
                      });

PHP

<?php
mysqli_set_charset('utf8');

if(isset($_POST['gravita']))
{ 
    $gravita = mysqli_real_escape_string($_POST['gravita']);

    $connessione = mysqli_connect('localhost','civicsensethecitizen','') or die (mysqli_errno ($connessione). mysqli_error ($connessione));
    $db = mysqli_select_db($connessione, "my_civicsensethecitizen" ) or die ('Database non trovato!');

    $query = "INSERT INTO Segnalazione(Gravità) VALUES('.$gravita.')";

    $risultato = mysqli_query($connessione,$query) or die ("Error in query: $query. " . mysqli_connect_error());

    mysqli_close($connessione);

    echo $gravita;
}
?>
5
  • Looks like your ajax doesn't get triggered in any way. If it would, according to api.jquery.com/jQuery.post data should be { gravita: gravita } Commented Oct 24, 2018 at 11:54
  • Ajax works correctly. I tried to return the value sent and print it with the console.log () and it comes quietly Commented Oct 24, 2018 at 12:04
  • if table Segnalazione contains other columnsalso other than Gravità, then you should provide value to those columns in insert query if it had not any default values. Commented Oct 24, 2018 at 13:12
  • why in your question data: Error in query: INSERT INTO Signaling (Severity) VALUES ('gravitaAlta'). and in your code you have query like INSERT INTO Segnalazione(Gravità) VALUES('.$gravita.'), why table and column is different? Commented Oct 25, 2018 at 13:10
  • Because gravitaAlta is the value I passed to the PHP variable via Ajax to fill the Gravity field. However, the problem has been changed by changing the Gravity to Severity field and changing data from the given type to String. Commented Oct 26, 2018 at 6:26

4 Answers 4

2

Try changing...

$query = "INSERT INTO Segnalazione(Gravità) VALUES('.$gravita.')";

to this...

$query = "INSERT INTO Segnalazione(Gravità) VALUES('" . $gravita . "')";

Also, make sure that there is a 'Segnalazione' table in your database, and in that table, there is a column named 'Gravità' and that it is set up to except the string you are providing.

Sign up to request clarification or add additional context in comments.

1 Comment

I have tested this solution of yours but I still get the same error
1

Use this

$query = "INSERT INTO Segnalazione(Gravità) VALUES('$gravita')";

In Place of

$query = "INSERT INTO Segnalazione(Gravità) VALUES('.$gravita.')";

1 Comment

String concatenation is not required inside the double code(" ")
1
$query = "INSERT INTO Segnalazione(Gravità) VALUES('$gravita')";

This will work for you. You tried to concatenate the string but did not work as it should!!! You should use double quotes nested inside the single quotes or simplier use the above way.

This solution though will have you open to SQL Injection so i would suggest to use prepared statements:

$stmt = $conn->prepare("INSERT INTO Segnalazione(Gravità) VALUES (?)");
$stmt->bind_param("s", $gravita);
$stmt->execute();

Comments

-1

Change,

$query = "INSERT INTO Segnalazione(Gravità) VALUES('.$gravita.')";

To this,

$query = "INSERT INTO Segnalazione(`Gravità`) VALUES('{$gravita} ')";

You seemed to trying to do a string concatenation and it has a issue/ typo Also try quoting column name

2 Comments

OK. With this solution I get this new error: data: Error in query: INSERT INTO Signaling (Severity) VALUES ('gravitaAlta'). gravitaAlta is the value of the input form
Can you echo the $query and post here?

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.