1

i want to insert selected texts in my page in table with clicking on button in my page but my problem is it doenst insert . select text is correct i tested it. response that i receive is my selected texts

This is my index page and i passed my texts into variable text

    <script src="jquery-3.2.1.min.js"></script>
    <script>
        $(document).ready(function () {
            var text = $("span:not([dir=rtl])").text();
            $("#btn").click(function () {
                $.ajax({
                    type:'post',
                    url:'process.php',
                    data:{'text':text},
                    success:(function (response) {
                        alert(response);
                    })
                })
            })
        })
    </script>

this is my process.php that connects to database and perform query but runs else statement

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sahifeDB";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = 'update sahife_tbl set english ='. $_POST['text'].' where id=1 ';
$result = $conn->query($conn,$sql);
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
sepehr
  • 165
  • 1
  • 2
  • 10

2 Answers2

0

You need to put " in update statement as datatype is text for field

change your query to:

$sql = 'update sahife_tbl set english ="'. $_POST['text'].'" where id=1 ';

Also use Prepared statement to prevent from sql injection

B. Desai
  • 16,414
  • 5
  • 26
  • 47
0

You need to put quotes around your string value in the query otherwise your DB will try to parse the text as part of the syntax and fall over.

$sql = "update sahife_tbl set english ='". $_POST['text']."' where id=1 ";

But again as mentioned in my comment on your question, this is super insecure, you want to use parameterisation instead - https://stackoverflow.com/a/60496/635522

Bananaapple
  • 2,984
  • 2
  • 25
  • 38