簡體   English   中英

如何編寫PHP代碼來更新Web服務

[英]How to write php code to update web service

我已啟動並運行Web服務,但我只能從數據庫中讀取數據。

任何人都可以幫助/指導我如何將php文件編碼為能夠寫入我的數據庫?

<?php

// Create connection
$con=mysqli_connect("server_address","database_name","user_name","table_name");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// This SQL statement selects ALL from the table ’table_name’
$sql = "SELECT * FROM table_name/";

// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();

// Loop through each row in the result set
while($row = $result->fetch_object())
{
    // Add each row into our results array
    $tempArray = $row;
    array_push($resultArray, $tempArray);
}

// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($result);
mysqli_close($con);
?>

您應該更熟悉MySql查詢。

要更新表,您將使用類似於以下查詢的內容。

$sql = "
Update table_name 
Set    column1 = " . $value1 . ", 
       column2 = value2 
Where  column3 = matchingValue";

要么

$sql = "
Inert Into table_name (column1, column2, column3) Values (value1, value2, value3)

包含額外的線條以提高可讀性。 如果不允許用戶輸入,可以通過連接將值直接放入sql中。

如果你讓用戶$ _POST或$ _GET,你會出於安全原因想要使用預准備語句

http://us3.php.net//manual/en/mysqli.prepare.php

雖然開始使用PDO可能更好

http://www.php.net//manual/en/book.pdo.php

$sql = "INSERT INTO table_name (column1) VALUES($value)"; 你會做的伎倆,你可以隨意擴展這么多列。

這不是PHP問題(是的,您使用PHP來執行查詢,但數據插入/檢索是SQL的事情)

正如@MarcB所說,查找INSERT語法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM