如何编写php代码来更新web服务
我已经启动并运行了一个web服务,但我只能从数据库中读取数据 有谁能帮助/指导我如何编写php文件,使其能够写入我的数据库如何编写php代码来更新web服务,php,mysql,Php,Mysql,我已经启动并运行了一个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
<?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);
?>
$sql=“插入到表中_名称(第1列)值($value)”
会起作用,您可以扩展任意多个列。这不是PHP的问题(是的,您使用PHP执行查询,但数据插入/检索是SQL的事情)
正如@MarcB所说的,查找语法。您应该更熟悉MySql查询 要更新表,可以使用类似于以下查询的内容
$sql = "
Update table_name
Set column1 = " . $value1 . ",
column2 = value2
Where column3 = matchingValue";
或
额外的行包括可读性。如果不允许用户输入,可以通过连接将值直接放入sql
如果允许用户$\u POST或$\u GET,出于安全原因,您将需要使用准备好的语句
尽管开始使用PDO可能更好
谢谢您的回答!这更像是一个用户跟踪的事情。用户将发布到数据库,但他们不会知道…在这种情况下,如果查询是静态的,并且从不更改,则应该可以。祝你好运!
$sql = "
Inert Into table_name (column1, column2, column3) Values (value1, value2, value3)