Jump to content

I'm not able to use apostrophes in a text box


essaydoctor

Recommended Posts

Whenver I type in apostrophes (and probably other characters - haven't tested) in a text box, I get the following error message: Notice: Use of undefined constant first_name - assumed 'first_name' in C:\Program Files\PHP5\Abyss Web Server\htdocs\post_action.php on line 31 What should I do to allow apostrophes and other characters?

Link to comment
Share on other sites

What does line 31 look like?
This is what the code looks like in it's entirety, and I'll be sure to point out LINE 31. # Check form submitted.if ($_SERVER['REQUEST_METHOD'] == 'POST'){ # Check Subject and Message Input. if ( empty($_POST['subject'] ) ) { echo '<p>Please enter a subject for this post.</p>'; } if ( empty($_POST['message'] ) ) { echo '<p>Please enter a message for this post.</p>'; } # On success add post to forum database. if( !empty($_POST['subject']) && !empty($_POST['message']) ) { # Open database connection. require ( '../connect_db.php' ) ; # Execute inserting into 'forum' database table. $q = "INSERT INTO forum(first_name,last_name,subject,message,post_date) #THIS LINE BELOW IS LINE 31. VALUES ('{$_SESSION[first_name]}','{$_SESSION[last_name]}','{$_POST[subject]}','{$_POST[message]}',NOW() )"; $r = mysqli_query ( $dbc, $q ) ; # Report error on failure. if (mysqli_affected_rows($dbc) != 1) { echo '<p>Error</p>'.mysqli_error($dbc); } else { load('forum.php'); } # Close database connection. mysqli_close( $dbc ) ; } }
Link to comment
Share on other sites

You'll get that error regardless of what you put in the form, that error relates to the PHP syntax and not the data from the form. You just need to quote the array indexes: VALUES ('{$_SESSION['first_name']}','{$_SESSION['last_name']}','{$_POST['subject']}','{$_POST['message']}',NOW() )"; For the issue with single quotes breaking the query, you need to escape the form data before putting it in the query: http://www.php.net/manual/en/mysqli.real-escape-string.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...