【问题标题】:php i can't insert into mysql database but no errorphp我无法插入mysql数据库但没有错误
【发布时间】:2015-10-29 12:42:25
【问题描述】:

我正在尝试使用 PHP 将数据插入 MySQL 数据库,但它不起作用。

在添加插入代码之前,我检查了 POST 是否正常工作并从输入中获取我需要的数据。

这是存储过程

BEGIN 

    INSERT INTO students
         (
           signatoryid, 
           signatoryname, 
           signatoryposition, 
           signatoryoffice                       

         )
    VALUES 
         ( 
           p_signatoryid, 
           p_signatoryname, 
           p_signatoryposition, 
           p_signatoryoffice                
         ) ; 
END

这是我将数据插入 MySQL 的 php。我收到了成功的警报,但它没有插入到数据库中。

<?php 
if (isset($_POST['submit'])){
    $signatory_name = $_POST['sig_name'];
    $signtory_position = $_POST['sig_position'];
    $signatory_office = $_POST['sig_office'];
    require_once 'dbconfig.php';
    try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname",$username, $password);
    $stmt = $conn->prepare("CALL sp_insertsignatory (?), (?), (?), (?)");
    $value = '0001';
    $stmt->bindParam(1, $value, PDO::PARAM_STR, 10); 
    $stmt->bindParam(2, $signatory_name, PDO::PARAM_STR, 30);
    $stmt->bindParam(3, $signtory_position, PDO::PARAM_STR, 30);
    $stmt->bindParam(4, $signatory_office, PDO::PARAM_STR, 30);
    $stmt->execute();
    echo '<script language="javascript">';
    echo 'alert("successful")';
    echo '</script>';
    } catch (PDOException $pe) {
        die("Error occurred:" . $pe->getMessage());
    }   
}
?>

【问题讨论】:

  • “但没有错误” - 或许检查一下?
  • 为什么每个参数都有额外的括号,而不是所有参数的一对括号?
  • SQL 很可能是错误的。您缺少值周围的引号。
  • php.net/manual/en/function.error-reporting.php --- php.net/manual/en/pdo.error-handling.php 并将其应用于您的代码,然后回来告诉我们您从中得到了什么。
  • @Fred-ii- 我不明白您的意思,先生。我找不到我已经做了几个小时的错误了。所以我希望有人可以帮助我。像这样启动? ("CALL sp_insertsignatory (?,?,?,?)");它不起作用。但我认为你是对的,应该是这样的

标签: php mysql pdo sql-insert


【解决方案1】:

问题是我在存储过程中使用的表名 在弗雷德二世指出这个网站之后http://php.net/manual/en/pdo.error-handling.php 并将其应用于我的代码。我收到错误并知道是什么问题

$conn = new PDO("mysql:host=$host;dbname=$dbname",$username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
$stmt = $conn->prepare("CALL sp_insertsignatory (?,?,?,?)");

【讨论】:

    猜你喜欢
    • 2016-09-24
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多