/    Sign up×
Community /Pin to ProfileBookmark

Stripslashes or Htmlspecialchars??

Greetings All,
I wrote a script to update my news article. However, when I go to edit the title and news, if a ‘ exist, then the update fails. I know from previous work, that either slipslashes or htmlspecialchars should do the trick, I am just not sure which one, or where to put them.

Would anyone be able to point me in the right direction of where I should include these statements… or if you have an alternative fix, please post.

Keep in mind that I can update articles once the article Title or News body doesn’t contain a ‘ inside of the content. Once a ‘ exist, then it crashes the script and nothing updates.

Here is the script below:

[code=php]
<script language=”javascript” type=”text/javascript” src=”jscripts/tiny_mce/tiny_mce.js”></script>
<script language=”javascript” type=”text/javascript”>
// Notice: The simple theme does not use all options some of them are limited to the advanced theme
tinyMCE.init({
mode : “textareas”,
theme : “simple”
});
</script>
<?php
//error_reporting(E_ALL);
include (“../class/config.php”);
$today = date(“Y-m-d”);

$submit=$_REQUEST[‘submit’];
$id=$_REQUEST[‘id’];
$del=$_REQUEST[‘del’];
$year=$_REQUEST[‘year’];
$month=$_REQUEST[‘month’];
$day=$_REQUEST[‘day’];

if(isset($submit))
{
$fulldate = $year . “-” . $month . “-” . $date;

if($submit == ‘Delete’)
{
$sql = “delete from news_saxon_saxon where NEWSID = ‘$id'”;

$result = mysql_query($sql, $db);

if($result)
{
echo(“The news article was deleted successfully.”);
echo(“<form action=’news_admin_index.php?action=editnews’ method=’POST’><p>”);
echo(“<input type=’submit’ name=’submit’ value=’OK’ class=”submit button”></form)”);
}

}

else if($submit == ‘Update’)
{
$news=$_POST[‘NEWS’];
$title=$_POST[‘TITLE’];
$postdate = $year.”-“.$month.”-“.$day;
$sql = “update news_saxon_saxon set TITLE=’$title’,NEWS=’$news’, Date=’$postdate’ where NEWSID=’$id'”;

$result = mysql_query($sql, $db);
if($result)
{
echo “$sql”;
echo(“The news article was updated successfully.”);
echo(“<form action=’news_admin_index.php?action=editnews’ method=’POST’ onSubmit=’fnsubmit()’>”);
echo(“<input type=’submit’ name=’submit’ value=’OK’ class=”submit button”></form)”);
}

}

else if (($submit == ‘Cancel’) || ($submit == ‘OK’))
{ //header(“Location:http://www.bviddm.com/control/nav_index.php?action=editstory”);
?>
<script language=”Javascript” type=”text/javascript”>
document.location.href=’news_admin_index.php?action=editnews’
</script>
<?
}
}

else
{

if(isset($del))
{
$sql = “select * from news_saxon_saxon where NEWSID = ‘$id'”;
$navset = mysql_query($sql, $db);
$onenav = mysql_fetch_object($navset);
echo(“Are you sure you want to delete this news article “);
echo($onenav->NEWSID);
echo(“?”);
echo(“<form action=’news_admin_index.php?action=editnews’ method=’POST’ onSubmit=’fnsubmit()’>”);
echo(“<input type=’hidden’ name=’id’ value=” . $onenav->NEWSID . “>”);
echo(“<table><tr><td><input type=’submit’ name=’submit’ value=’Delete’ class=”submit button”></td><td><input type=’submit’ name=’submit’ value=’Cancel’ class=”submit button”></td></tr></table></form)”);
}
else
{
if(isset($id))
{
$sql = “select * from news_saxon_saxon where NEWSID = ‘$id'”;
$postdate = $year.”-“.$month.”-“.$day;
$navset = mysql_query($sql, $db);
$onenav = mysql_fetch_object($navset);
//list($year, $month, $day) = explode (“-“, $row[‘DATE’]);
list($year, $month, $day) = explode (“-“, date(“Y-m-d”));
?>
<form action= “news_admin_index.php?action=editnews” method=”post”>
<input type=’hidden’ name=’id’ value=<?php echo($id); ?>>
<table>
<tr><td>Day:</td><td>
<select name=”day” id=”day” value=<?php echo($day); ?>>
<?php
for ($i = 1; $i <= 31; $i++)
{
if ($i == $day) echo “<option value=”$i” selected=”selected”>$i</option>n”;
else echo “<option value=”$i”>$i</option>n”;
}
?>
</select>
</td>
</tr>
<tr>
<td>Month:</td><td>
<select name=”month” id=”month” value=<?php echo($month); ?>>
<?php
$monthList=array(
1 => “January”,
2 => “February”,
3 => “March”,
4 => “April”,
5 => “May”,
6 => “June”,
7 => “July”,
8 => “August”,
9 => “September”,
10 => “October”,
11 => “November”,
12 => “December”);

foreach($monthList as $code => $monthname)
{
if ($code == $month) echo “<option value=”$code” selected=”selected”>$monthname</option>n”;
else echo “<option value=”$code”>$monthname</option>n”;

}
?>
</select>
</td></tr>
<tr>
<td>Year:</td><td>
<select name=”year” id=”year” value=<?php echo($year); ?>>
<?php
$this_year = date(“Y”);
for ($i = $this_year; $i <= $this_year+10; $i++)
{
if ($i == $year) echo “<option value=”$i” selected=”selected”>$i</option>n”;
else echo “<option value=”$i”>$i</option>n”;
}
?>
</select><?php $postdate = $year.”-“.$month.”-“.$day;?></td>
</tr> <tr><td>Title</td><td><input type=”text” name=”TITLE” value=<?php echo($onenav->TITLE);?>></td></tr>
<tr><td>News</td><td><textarea name=”NEWS” id=”NEWS”><?php echo($onenav->NEWS);?></textarea></td></tr>
<tr><td colspan=”2″><input type=”submit” value=”Update” name=”submit” class=”submit button”></td></tr>
</table>
</form>
<?php
}

else
{
?>

<table width=”100%”>
<tr><th class=’right_title’>Date</th><th class=’right_title’>Title</th>
<th colspan=2><center>Action</center></th></tr>

<?php

$navquery = “select * from news_saxon_saxon”;

$navresult = mysql_query($navquery);
while ($row = mysql_fetch_object($navresult))
{
echo (“<tr>”.”<td>”.$row->DATE.”</td>”.”<td>”.$row->TITLE.”</td>”);

echo(“<td>”.”<p align=’center’>”.”<a href=’news_admin_index.php?action=editnews&id=$row->NEWSID’>”.”<img src=’..imagesbutton_edit.png’ border=’0′ alt=”.”Edit”.”>”.”</td>”.
“<td><p align=’center’>”.”<a href=’news_admin_index.php?action=editnews&id=$row->NEWSID&del=$row->NEWSID’>”.”<img src=’..imagesbutton_drop.png’ border=’0′ alt=”.”Delete”.”></td></tr>”);
}

}
?>

</table>
<?php

}} ?>
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@ZnupiAug 24.2007 — So I suppose the query that fails is the update one? Here's how to fix it, and prevent any MySQL Injection:
[code=php]
$sql = "UPDATE news_saxon_saxon set title='" . mysql_real_escape_string($title) . "', news='" . mysql_real_escape_string($news) . "', date='$postdate' WHERE newsid='$id'";
[/code]

Also, you might want to define $title and $news like this:
[code=php]
$news = stripslashes($_POST['NEWS']);
$title = stripslashes($_POST['TITLE']);
[/code]

So when you show the content in a page, you won't get ' instead of '... like I've seen on sooo many sites...

Hope this helps...

Some helpful info on how to use quotes and backquotes in mysql queries:

- Every mysql keyword (like SELECT, INSERT, UPDATE, WHERE, etc) should be written in upper-case.

- Table names, column names, etc, should be enclosed between backquotes (for example: my_table or my_column).

- Every bit of data should be enclosed between single-quotes (for example: 'this is some data').

(Why these three things? Because if you have a table named "SELECT", mysql won't be able to tell the difference in your query and will think that you're talking about the keyword "SELECT", but if you enclose it between backquotes, like this: SELECT, it will know you're talking about a table named SELECT ?)

So, a good query should look like this: "KEYWORD name 'data'"... example: "SELECT * FROM my_table WHERE my_column = 'my_data'" or: "INSERT INTO my_table (my_column1, my_column2) VALUES ('my_data1', 'my_data2')".

- Now, any data inserted in mysql should be escaped using mysql_real_escape_string($data); Here's why (from the php.net manual):

[code=php]
<?php
// Query database to check if there are any matching users
$query = "SELECT * FROM users WHERE user='{$_POST['username']}' AND password='{$_POST['password']}'";
mysql_query($query);

// We didn't check $_POST['password'], it could be anything the user wanted! For example:
$_POST['username'] = 'aidan';
$_POST['password'] = "' OR ''='";

// This means the query sent to MySQL would be:
echo $query;
?>
[/code]

The query sent to MySQL:
SELECT * FROM users WHERE user='aidan' AND password='' OR ''=''
This would allow anyone to log in without a valid password.

That's why you should use mysql_real_escape_string();
×

Success!

Help @gc40 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...