Jump to content

escaping ' and " from sql query, but still echo ' and "


wright67uk

Recommended Posts

In the basic form below, how can I prevent an sql error if a user decides to use ' or " ?

Is this where I should be using addslashes?

 

I still would like words such as (I'm) to appear in the php echo below.

 

 

 

<form method="get" action="editabout.php" style="border:0px" /><br />
<input name="title" class="title" readonly="readonly" value="about:" /><br />
<textarea name="about" class="aboutme"><?php echo $about; ?></textarea><br />
<input type="submit" name="submit" value="Save Change"/>
</form>

 

 

 

editabout.php

 

<?php
session_start();


include 'connectionhere.php';


$user_id = $_SESSION['user_id'];


$about = $_GET['about'];


$sql = "UPDATE registration SET about='$about' WHERE user_id='$user_id'";
mysql_query($sql) or die (mysql_error());


header ("Location: profile.php");
?>

Link to comment
Share on other sites

You're $_GET'ing the "about" text? :confused: You want $_POST['about'], not sure why you'd want to send that data via the URL.

 

For the SQL injection nightmare with that code, secure it with the below suggestion.

You should be looking at using different SQL functions, mysql_* PHP functions have been deprecated: https://wiki.php.net...sql_deprecation

Use prepared statements explained in this thread: http://forums.phpfre...ion-protection/

Edited by bashy
Link to comment
Share on other sites

I dont understand the question. are you asking why the " still show up in the echoed string from the database even though it was escaped on insert? if thats the question then the answer is escaped values are handled differently depending on a dbms that you are using. but if you escape a string, you are telling the dtatabase not to use the " for sql execution rather egnore them and save the string as a normal string. thats why when you sometimes echo an escaped string from a database, it will display with the " on. the escape function dont strip the " but does inform the database as i have explained above.

 

thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.