【问题标题】:PHP, ERROR: could not prepare SQL staementPHP,错误:无法准备 SQL 语句
【发布时间】:2021-09-19 01:17:12
【问题描述】:

我正在尝试获取一个可以在我的本地计算机上正常运行的 php 页面,以便在我的实时托管帐户上运行,但它不断抛出错误:无法准备 SQL 语句。 它没有向我的数据库返回任何记录,并且抛出错误。

*/
// if the 'id' variable is set in the URL, we know that we need to edit a record
if (isset($_GET['id']))
{
        // if the form's submit button is clicked, we need to process the form
        if (isset($_POST['submit']))
        {
                // make sure the 'id' in the URL is valid
                if (is_numeric($_POST['id']))
                {
                        // get variables from the URL/form
                        $id = $_POST['id'];
                        $firstname = htmlentities($_POST['firstname'], ENT_QUOTES);
                        $lastname = htmlentities($_POST['lastname'], ENT_QUOTES);

                        // check that firstname and lastname are both not empty
                        if ($firstname == '' || $lastname == '')
                        {
                                // if they are empty, show an error message and display the form
                                $error = 'ERROR: Please fill in all required fields!';
                                renderForm($firstname, $lastname, $error, $id);
                        }
                        else
                        {
                                // if everything is fine, update the record in the database
                                if ($stmt = $mysqli->prepare("UPDATE players SET firstname = ?, lastname = ?
                                        WHERE id=?"))
                                {
                                        $stmt->bind_param("ssi", $firstname, $lastname, $id);
                                        $stmt->execute();
                                        $stmt->close();
                                }
                                // show an error message if the query has an error
                                else
                                {
                                        echo "ERROR: could not prepare SQL statement.";
                                }

【问题讨论】:

  • 除了您只分享了一张图片,而不是两张,您还不清楚您希望我们如何仅通过屏幕图片来修复您的代码?机械师可以修理汽车的照片吗?请参阅 How to Ask 以及如何创建 minimal reproducible example 以获取有关使您的问题可回答的更多指导。
  • 对不起,这是我第一次在堆栈溢出,我已经返回并添加了源代码和第一张图片,我深表歉意。
  • 感谢您指点我如何提问。
  • 今天晚些时候当我弄清楚如何正确提问时,我是否应该删除此问题并正确重新发布?
  • @ShadDehart 不,不要删除问题 - 而是编辑和改进。您需要添加错误处理(请参阅上面的评论)以查看原因,读者可能能够猜到 - 但他们不需要:)。您可能会发现“提前返回”的代码模式可以极大地简化您的代码。

标签: php hosting


【解决方案1】:

所需要的只是重置数据库的密码,页面运行良好..感谢所有帮助过我的人..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-24
    • 2018-03-14
    • 2020-05-04
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    相关资源
    最近更新 更多