2014-12-21 60 views
-5

當我運行在本地主機在PHP(XAMPP)我得到直接的錯誤消息之前,我按下提交按鈕「信息客人sistemegönderilemedi」發送表格前,提交使用PHP

所以我不能發送信息。我數據庫。

<body> 

<form action="insert_pro.php" method="post" enctype="multipart/form-data"> 
    <div class="center_content"> 
     <div class="title_box">Insert New Product</div> 
     <div class="prod_box_big"> 
     <div class="center_prod_box_big"> 
      <div class="product_img_big"> <!-- <a href="javascript:popImage('images/big_pic.jpg','Some Title')" title="header=[Zoom] body=[&nbsp;] fade=[on]"><img src="images/p3.jpg" alt="" border="0" /></a> --> 

      </div> 
      <div class="details_big_box"> 


      <!-- TITLE --> 
      <div class="product_title_big" >Product Title :</div> 
      <td><input type="text" name="title" required/></td> 

      <!-- DESCRİPTİON --> 
      <div class="product_title_big">Description : </div> 
      <td><textarea name="descrip" cols="21" rows="5"/></textarea></td> 

      <!-- PRICE --> 
      <div class="product_title_big">Price :</div> 
      <td><input type="text" name="price" required/></td> 

      <!-- PHOTO --> 
      <div class="product_title_big">Product Photo :</div> 
      <td><input type="file" name="way" required/></td> 

      <!-- NOTE --> 
      <h1>IF YOU FINISHED FILLING INFORMATION OF THE PRODUCT CLICK ADD PRODUCT</h1> 
      </div> 

      <!-- BUTTON --> 
      <input type="submit" name="insert_post" value="ADD PRODUCT"> 

     </div> 
     </div> 
    </div> 

</form> 


</body> 
</html> 


<?php 
$title=$_POST['title']; 
$descrip=$_POST['descrip']; 
$price=$_POST['price']; 

if($price){//veri geldi ise sisteme giriyoruz 
    //mysqle bağlanıyoruz 
$ServerAd="localhost";//server ismini girin localde kurduysanız localhost girin. 
$KAd="root";//kullanıcı adı 
$KSifre="3306505";//kullanıcı şifresi 
$VeriAd="shop";//veritabanı adı 
[email protected]_connect($ServerAd,$KAd,$KSifre) or die(@mysql_err());//veri tabanına bağlanılıyor 
[email protected]_select_db($VeriAd);//veri tabanı seçiliyor 
$ekle=mysql_query("insert into product (title,descrip,price) values ($title,$descrip,$price)");//veri tabanına veri giriliyor. 
if($ekle)//veri girişi başarılı ise 
echo "Sayın Yorumunuz sistemimize iletilmiştir.Teşekkürler"; 
else//veri girişi hatalı ise 
echo "bilgiler sisteme gönderilemedi"; 
} ?> 
+0

(有關信息 - 根據Google翻譯,「bilgiler sistemegönderilemedi」僅僅意味着「信息未發送至系統」) – DNA

+0

'($ title,$ descrip,$ price)'需要引用的那些,字符串值被傳遞。將'或者死(mysql_error())'添加到'mysql_query()'中,你會看到MySQL拋出的錯誤。 –

+1

您的查詢中存在語法錯誤,因爲您尚未將變量包裝在撇號中。這是OP的另一個問題,即OP沒有正確地檢查退貨狀態,因此不知道發生了什麼。 –

回答

3

您需要引用字符串值。但是,您可以使用SQL injection,所以我已經使用mysqli做了一些準備工作。

旁註:添加or die(mysql_error())mysql_query()會發出語法錯誤信號。

<?php 
$link = new mysqli('localhost', 'root', 'password', 'db'); 
if ($link->connect_errno) { 
    throw new Exception($link->connect_error, $link->connect_errno); 
} 

// Check that the expected value has been provided via a POST request 
if (isset($_POST['insert_post'])) { 


// now prepare an INSERT statement 
if (!$stmt = $link->prepare('insert into product (title,descrip,price) VALUES (?,?,?)')) { 
    throw new Exception($link->error, $link->errno); 
} 

// bind parameters 
$stmt->bind_param('sss', $_POST['title'], $_POST['descrip'], $_POST['price']); 

if (!$stmt->execute()) { 
    throw new Exception($stmt->error, $stmt->errno); 
    } 

} // brace for (isset($_POST['insert_post'])) 

有關準備語句的詳細信息:

請教the manual on mysqli with prepared statements,或PDO with prepared statements


NB:

如果你仍然熱衷於保持你的MySQL目前的API,然後將其更改爲:

VALUES ('".$title."','".$descrip."','".$price."') 

,並使用** stripslashes() < =(諮詢旁註)和mysql_real_escape_string()

即:

$title = stripslashes($_POST['title']); 
$title = mysql_real_escape_string($_POST['title']); 

,做了別人不一樣。

**旁註:我用stripslashes()的原因是在名稱添加引號的能力,比如奧尼爾的實例。而不是它在數據庫中顯示爲O'Neil。

它(stripslashes())與防止SQL注入無關,它只是一個附加選項。 mysql_real_escape_string()是什麼幫助反對注射,但使用準備好的陳述是真正應該用什麼來代替;無論是使用mysqli_還是使用PDO API。

報價Halfer:

據我所知,你只需要在包含撇號,如果magic quotes已經自動逃脫它的名稱使用stripslashes()。由於PHP不再可用,因此我不再添加這樣的代碼。轉義函數如mysql_real_escape_string()將自行處理「O'Neil」罰款。

+0

「stripslashes」不會防止SQL注入的錯誤方式嗎? Afaict它會採取一個反斜槓的字符串,並刪除斜槓,實際上我們想要添加它們。 – halfer

+0

@halfer你是對的,但是爲什麼我總是使用'stripslashes()'(或者在我的答案中加上它)是能夠在諸如O'Neil這樣的名字中添加撇號。而不是它在數據庫中顯示爲O'Neil。 'mysql_real_escape_string()'接管注入。然而,準備好的陳述是更好的方法。如果我錯了,請糾正我,但這是我自己的處理撇號的經驗,*歡呼*(我將對此進行編輯)。 –

+1

據我所知,如果[魔術引號](https://php.net/manual/en/security.magicquotes.php)已經自動轉義,你只需要在包含撇號的名字上使用'stripslashes'它。由於PHP不再可用,因此我不再添加這樣的代碼。像mysql_real_escape_string()這樣的轉義函數將自己處理「O'Neil」。 – halfer