Text form can't accept apostrophes — WebyPost
Text form can't accept apostrophes
Programming

Text form can't accept apostrophes

My text forms won't allow single " ' " to occur in the input fields. I get an sql syntax error. Is there any good way to allow single apostrophes to be allowed in my text field?

here's my code

html


    
    
    

 

My database

 

  // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "INSERT INTO whatsgood (one, two)
    VALUES ('$one', '$two')";

    if (mysqli_query($conn, $sql)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "
" . mysqli_error($conn); } mysqli_close($conn); ?>

 

Best Answer

Use addslashes PHP function (It Quote string with slashes)

$sql = "INSERT INTO whatsgood (one, two) VALUES ('".addslashes($one)."', '".addslashes($two)."')";

Example:

 

 

Found this helpful? Share it!

Comments

0
No comments yet. Be the first!

Share via WebyChat


Loading connections...