2017-05-01 12 views
0

hii私のSQLデータベースに画像とともにテキストを挿入するPHPコードがありますので、挿入時に挿入しない記号を挿入すると問題が発生しますシンボル "なしのテキスト、それは罰金 の作品これは私のコードで私は問題を解決する方法を'記号を入力してmysqlデータベースに挿入することはできません

<?php 
session_start(); 
if ((isset($_SESSION['admin']))) 
{ 
require_once("../dbconfig.php"); 

?> 

<form method="post" name="data_table" enctype="multipart/form-data"> 

<?php 
error_reporting(0); 
if(isset($_POST['add2'])){ 
include_once '../dbconfig.php'; 
$username=$_SESSION["admin"]; 
$subject=$_POST['subject']; 
$description=$_POST['description']; 
$image = rand(1000,100000)."-".$_FILES['image']['name']; 
if(!isset($image)) 
    echo"Please Upload Image"; 
else{ 
$file_loc = $_FILES['image']['tmp_name']; 
$file_size = $_FILES['image']['size']; 
$file_type = $_FILES['image']['type']; 
$folder="uploads-about/"; 
$new_size = $file_size/1024; 
$new_file_name = strtolower($image); 
$final_image=str_replace(' ','-',$new_file_name); 
if(move_uploaded_file($file_loc,$folder.$final_image)) 
    { 
$sql="INSERT INTO about (subject,description,image) 
VALUES ('$subject','$description','$final_image')"; 
$result=mysql_query($sql); 
    if($result) 
    { 
echo "<font size='5px'><div align='center'>Successful</div></font>"; 
}else 
{ 
echo "<font size='5px'><div align='center'>errror</div></font>"; 

}  } 
else{ 
echo "<font size='5px'><div align='center'>Select image to upload</div></font>"; 
} 
} 
mysql_close(); 
} 
    ?> 
<div align="center"> 
<br> 
     <label for="Username">Subject: </label>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 
     <input type="text" name="subject" id="Username" style="width:500px;height:40px" /> 
     <br><br> 
     <label for="Password">Description: </label>&nbsp&nbsp 
     <textarea name="description" id="Password" style="width:500px;height:100px" ></textarea> 
     <br><br> 

     <label for="image">Select Image</label> 
     <input type="file" class="button purple image" name="image" accept="image/*" id="username" capture /> 
     <br><br><br> 
     <button name="add2" value="log in" class="button purple add"><span>ADD</span></button> 
     </div> 
    </form> 
<?php } ?> 
+2

あなたの問題の迅速な修正は、文字列の一重引用符をエスケープすることです。 '' ''。しかし、あなたは実際に準備されたステートメントを使用するべきです。これは、この(および他の多くの落とし穴)を自動的に処理します。 –

答えて

1

PHP addslashesは役立ちますが、あなたは使用してデータをサニタイズする必要があります

PPS : Prepared Parameterized Statements。これは、すべてのものは、より安全にする、それはあなたのためにこれを処理します、またPreventing SQL injection

役立つ、となります:)コードより読み

編集:それはまたで述べたように、vampirizeしたくありません@TimBiegeleisenのコメント

関連する問題