2015-04-20 46 views
8

我想用新數據更新數據庫,以便當您將文本放在文本框中然後單擊提交按鈕時,數據將被髮送到數據庫,並使用特定的ID 。我想發送的是亮度,下面的代碼。當我寫這樣的東西,我運行它,我收到一個403錯誤:訪問禁止。我怎樣才能解決這個問題?使用提交按鈕更新數據庫數據

<?php 
    function updater($value,$id){ 
// Create connection 
    $conn = new mysqli('localhost' , 'user_name' , '' , 'data_base_name'); 
// Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
    $sql = "UPDATE table_name SET name=$value WHERE id=$id"; 
    if ($conn->query($sql) === TRUE) { 
     echo "Record updated successfully"; 
    } else { 
     echo "Error updating record: " . $conn->error; 
    } 
//$conn->close(); 
} 
?> 

<!DOCTYPE html> 
<html> 
<header> 
</header> 
<body> 
    <form action="<?php updater($_POST['name'],1); ?>" method="post" style="height:50px;width:50px;"> 
     <input type="text" name="name" /><br><br> 
     <input type="submit" /><br/> 
    </form> 
</body> 
</html> 
+4

此'動作=「「''是絕對值得懷疑的,另外,你的值是一個字符串;在你的SET中這樣處理。 –

+0

你不在action屬性中調用函數,你在那裏放置表單將被處理的url – Ghost

+0

我知道....你能告訴我真正的方法嗎? – Sonoo

回答

2

是這樣的:

<?php 
function updater($value,$id){ 
    // Create connection 
    $conn = new mysqli('localhost' , 'user_name' , 'pass' ,'data_base_name'); 
    $value =mysqli_real_escape_string($conn,$value); 
    $id =mysqli_real_escape_string($conn,$id); 
    // Check connection 

    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
    $sql = "UPDATE table_name SET name='{$value}' WHERE id='{$id}'"; 
    if ($conn->query($sql) === TRUE) { 
     echo "Record updated successfully"; 
    } else { 
     echo "Error updating record: " . $conn->error; 
    } 
    $conn->close(); 
} 

if(isset($_POST['name'])){ 
    updater($_POST['name'],$_POST['id']) 
} 
?> 

<!DOCTYPE html> 
<html> 
<header> 
</header> 
<body> 
<form action="" method="post" style="height:50px;width:50px;"> 
    <input type="hidden" name="id" value="1" />   
    <input type="text" name="name" /><br><br> 
    <input type="submit" /><br/> 
</form> 
</body> 
</html> 
4

你需要把URL,做形式處理,而不是功能的action屬性裏面:

action="<?php updater($_POST['name'],1); ?>" // not this 
action="<?php echo $_SERVER['PHP_SELF']; ?>" // path to this page 

如果這是在同一頁上,你可以省略,或者使用$_SERVER['PHP_SELF'] ,然後抓住表單提交。在該過程中,然後調用您的自定義函數。

if($_SERVER['REQUEST_METHOD'] === 'POST') { 
    $value = $_POST['name']; 
    $id = 1; 

    updater($value, $id); 
} 

一個簡單的辦法是隻引用裏面的字符串:

$sql = "UPDATE table_name SET name='$value' WHERE id=$id"; 

但是,這是開放的SQL注入,另一種方式做更安全的查詢是讓他們做好準備:

function updater($value,$id) { 
    // Create connection 
    $conn = new mysqli('localhost' , 'user_name' , '' , 'data_base_name'); 
    // Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
    $sql = "UPDATE table_name SET name = ? WHERE id= ?"; 
    $update = $conn->prepare($sql); 
    $update->bind_param('si', $value, $id); 
    $update->execute(); 
    if ($update->affected_rows > 0) { 
     echo "Record updated successfully"; 
    } else { 
     echo "Error updating record: " . $conn->error; 
    } 
} 
+0

只是一個說明,這是極端不安全的。但這是你如何做到這一點的問題。最終你可能想看看Prepared語句。 –

+0

我以爲我會添加我的2美分;現在我真的必須飛行;沒有運行大聲笑 –

+0

看起來像我的2美分已經消失大聲笑是'SET name ='$ value'' –

相關問題