【问题标题】:How to add entries to mysql db using API..? [closed]如何使用 API 向 mysql db 添加条目..? [关闭]
【发布时间】:2011-08-04 07:57:58
【问题描述】:

我是 PHP 和 mysql 的新手,我正在尝试为我的 iphone 应用程序制作 API。 到目前为止,我已经能够连接并从我的 sql 数据库中检索数据,现在我正在尝试使用 API 和参数对其进行输入。

谁能帮帮我。

非常感谢!!

【问题讨论】:

    标签: php mysql api mobile parameters


    【解决方案1】:

    如果to make entries 是指将数据添加到数据库。

    您执行此操作的方式与 select 数据相同。
    而不是发出select 声明,例如:

    SELECT x,y,z FROM table1 
    

    你这样做:

    INSERT INTO table1 (x,y,z) VALUES ('a', 1, 'test')
    

    或者:

    UPDATE table1 SET x = 'b' WHERE x = 'a'
    

    如何传递参数取决于您使用的 API。
    最好(最安全)使用 PDO 来传递参数。

    如何从 url 中获取参数
    为了从 url 中获取参数(例如:example.com/test.php?username=xyz&password=!@#$%),请执行以下操作:

    $username = mysql_real_escape_string($_GET['username']);  
    $password = mysql_real_escape_string($_GET['password']);  
    $query = "SELECT * FROM users WHERE username = '$username' 
              AND passhash = sha2(CONCAT(salt,'$password'),512)";
    

    请注意,在使用mysql_real_escape_string() 时,必须在注入的变量名称周围加上单引号,否则转义将毫无用处。像这样使用代码是 100% 安全的 SQL 注入。

    如果您使用的是 PDO,则可以删除 mysql_real_escape_string(),如果您不需要它以防止 SQL 注入。

    链接
    http://dev.mysql.com/doc/refman/5.5/en/update.html
    http://dev.mysql.com/doc/refman/5.5/en/insert.html
    http://php.net/manual/en/ref.pdo-mysql.php
    https://stackoverflow.com/search?q=%5Bphp%5D+%5Bmysql%5D+pdo
    http://php.net/manual/en/reserved.variables.get.php
    http://php.net/manual/en/function.mysql-real-escape-string.php

    【讨论】:

    • 感谢您的回复 我知道如何使用 INSERT n UPDATE 在表中插入数据,但我想通过点击 URL example.com/test.php?username=xyz&password=!@# 来做到这一点$%
    • 我知道这可能是要问的基本问题,但我不知道要制作 Web 服务。因此,在点击该 url 用户名行和密码后,应在 DB 中更新。
    • Thnx 确实链接有效:) thnx 很多人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2021-03-16
    相关资源
    最近更新 更多