complete novice here so be gentle with me, I am trying to update a mysql database with a string that is likely to contain apostophes, I have it all working except the apostophe issue, I have htmlspecialchars in use. I assume I need mysqli real_escape_string but don't know how to incorporate it into my code. The coding is probably very poorly done but while it works I can live with it, it is for an extra couple of text boxes sneaked into an existing gallery script. Code is:
<?php
$servername = "localhost:3306";
$username = "xxxxxxxxx";
$password = "xxxxxxxx";
$dbname = "my_bodged_gallery";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (empty($_GET)){
$gallery_id = htmlspecialchars($_POST["gall_id"]);
}else{
$gallery_id = $_GET['gall_id'];
}
if(isset($_POST["submit"])){
$new_title = htmlspecialchars($_POST["new_title"]);
$new_story = htmlspecialchars($_POST["new_story"]) ;
$sql = "UPDATE stivagallery_plugin_gallery SET main_title = '$new_title' , story = '$new_story' WHERE foreign_id= '$gallery_id' AND sort= '1'";
if ($conn->query($sql) === TRUE) {
echo "Record updated";
} else {
echo "Error updating record: " . $conn->error;
}
$sql = "UPDATE stivagallery_galleries SET title = '$new_title' , story = '$new_story' WHERE id= '$gallery_id' ";
if ($conn->query($sql) === TRUE) {
echo " successfully";
} else {
echo "Error updating record: " . $conn->error;
}
}else{
$sql = "SELECT title, story FROM stivagallery_galleries WHERE id= '$gallery_id' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$new_title = $row["title"];
$new_story = $row["story"];
}
}
}
$conn->close();
?>
<html><body>
<div>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="735" height="200" bgcolor="#FFFFFF">
<tr>
<td><form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<i><font face="Arial Black" color="#000080">Title:</font></i><br>
<input type="text" name="new_title" size="20" value="<?php echo $new_title ;?>"><br>
<br>
<i><font face="Arial Black" color="#000080">Your story: </font></i>
<br>
<textarea rows="10" name="new_story" cols="80"><?php echo $new_story ;?></textarea></p>
<input type="hidden" name="gall_id" value="<?php echo $gallery_id ; ?>">
<input type="submit" name="submit"><input type="reset" value="Reset" name="B2"></p>
</form>
</td>
</tr>
</table>
</div></body></html>
Any help greatly appreciated, I'm a greasemonkey not a programmer....