Welcome to WebDesignForums.net!
You're currently viewing WDF as a guest. By registering for a free account, you'll be able to participate with other members in our friendly community. Being a member allows you to ask questions and get answers for those troublesome web development tasks!

In addition, as a member you'll be able to post your websites up for review. Using our unique website review system you can gain some amazing feedback from some of the best web developers around. This is a completely free service to all registered members.

Ready to register yet? Registration is 100% free. Click Here To Join Now!

Copying information in a MySQL table with an apostrophe

Discussion in 'Database Systems Help' started by Glenn, Oct 31, 2011.

  1. Offline

    Glenn Member

    Message Count:
    135
    Likes Received:
    2
    Trophy Points:
    18
    Gender:
    Male
    I have a web site set up where I allow users to share(copy) information with each other. If one user has something stored that has an apostrophe, it messes up the copying process. How can this be fixed?


  2. Offline

    Glenn Member

    Message Count:
    135
    Likes Received:
    2
    Trophy Points:
    18
    Gender:
    Male
    The php code looks like this.


    $results = mysql_query("SELECT * FROM questions WHERE user = '$shareduserName' AND subject = '$subject' AND topic = '$topic'");
    mysql_query("INSERT INTO topics (userid, subject, topic) VALUES ('$user', '$mysubject', '$mytopic')");

    while ($row = mysql_fetch_array($results)) {
    $question = $row['question'];
    $answer = $row['answer'];


    mysql_query("INSERT INTO questions (user, subject, topic, question, answer, ordernum) VALUES ('$user', '$mysubject', '$mytopic', '$question', '$answer', $count)");

    $count++;
    }


  3. Offline

    onlinespider Member

    Message Count:
    81
    Likes Received:
    3
    Trophy Points:
    8
    Gender:
    Male
    if i am understanding you well, let us say that you want to insert Maria'Ann as user into your database. You have to use the following code:

    "INSERT INTO questions (user, subject)
    VALUES('Maria\'Ann',$subject)";

    In this way you are escaping the ' as \' otherwise the mysql will treat is an the end of the string to be inserted and will cause an error.

    i hope this explains.


  4. Offline

    Glenn Member

    Message Count:
    135
    Likes Received:
    2
    Trophy Points:
    18
    Gender:
    Male
    Actually, what I'm trying to do is copy it from one table to another. Users are able to copy information from someone else's profile to their own.


  5. Offline

    onlinespider Member

    Message Count:
    81
    Likes Received:
    3
    Trophy Points:
    8
    Gender:
    Male
    have you passed all inputs through mysqli_real_escape_string() function before being saved in the first table?

    $escaped_data=mysqli_real_escape_string($database_connection,$_POST['data']);


  6. Offline

    Glenn Member

    Message Count:
    135
    Likes Received:
    2
    Trophy Points:
    18
    Gender:
    Male
    No, I haven't.


  7. Offline

    onlinespider Member

    Message Count:
    81
    Likes Received:
    3
    Trophy Points:
    8
    Gender:
    Male
    mysqli_real_escape_string() function will escape dangerous characters when inputted by the user and therefore increase security.


Share This Page