doufu9145 2017-12-19 17:27 采纳率: 0%
浏览 88
已采纳

将php表单信息更新到sql server数据库时出错

I've created forms in php and want to update the data in the sql server database.

Following is my ad.php file.

<html>
<title>FORM</title>
<body><br><br>
<form action ="db_connection.php" method="post">
Activity:<input type="text" name="name"><br><br>
Units: <input id="number"><br><br>

<input type="submit" value="submit">
</form>

The data inserted in the above form should be updated in the table 'ad' of the database.

Following is my db_connection.php file:

<?php

class DB{
protected static $db;
const SERVER = "";
const SCHEMA = "";
const USER = "";
const PASS = "";
/*
 * Sets up the database connection
 * @return the database instance
 */
public static function databaseConnection(){
    if(!self::$db){
        try {

            $databse = "sqlsrv:server=" . self::SERVER . ";Database=" . self::SCHEMA;
            self::$db = new PDO($databse, DB::USER, DB::PASS);  
            self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
        } catch(PDOException $e){
            die("Error " . $e->getMessage());
        }   
    }
    return self::$db;
}

}


class ad{

  public function dbAdmin(vat $Activity, int $Units): bool {
    $conn = databaseConnection::getConnection();
    $conn->beginTransaction();
    $sql = "INSERT INTO ad('Activity', 'Units') VALUES (:Activity, :Units)";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':Units', $Units);
    $stmt->bindParam(':Activity', $Activity);
    if ($stmt->execute()) {
        $conn->commit();
        return true;
    } else {
        $conn->rollback();
        return false;
    }
}
}

I'm getting a blank page after submitting the form and the database is not updating with the new data being submitted. Can you please check the code and help me with the corrections.

  • 写回答

3条回答 默认 最新

  • douba8048 2017-12-19 20:35
    关注

    I had to reproduce this using Docker. The container "microsoft/mssql-server-linux" is being used.

    I am using this extension **pdo_sqlsrv** and this driver https://breeto.id.au/2017/05/docker-alpine-linux-php-sql-server/
    

    index.html

    <!DOCTYPE>
    <html>
    <head>
        <title>FORM</title>
    </head>
    <body>
    <br/>
    <br/>
    <form action="index.php" method="post">
        <label for="activity">Activity: </label>
        <input id="activity" type="text" name="activity">
        <br/>
        <br/>
        <label for="number">Units: </label>
        <input id="number" type="number" name="units">
        <br/>
        <br/>
        <input type="submit" value="submit">
    </form>
    </body>
    </html>
    

    index.php

    <?php
    declare(strict_types=1);
    
    require_once __DIR__ . '/Db.php';
    require_once __DIR__ . '/Ad.php';
    
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    ini_set('log_errors', '1');
    
    $post = ['activity', 'units'];
    if (!isset($_POST['activity'], $_POST['units'])) {
      header('Invalid form', TRUE, 422);
      exit;
    }
    
    $activity = filter_input(INPUT_POST, 'activity', FILTER_SANITIZE_SPECIAL_CHARS);
    $units    = (int) filter_input(INPUT_POST, 'units', FILTER_SANITIZE_NUMBER_INT);
    if (!is_int($units)) {
      header('A number must be given', TRUE, 422);
      exit;
    }
    
    $adTable = new Ad();
    $adTable->dbAdmin($activity, $units);
    
    echo '<h1>Done</h1>';
    echo "<p>Activity: {$activity}</p>";
    echo "<p>Units: {$units}</p>";
    

    Db.php

    <?php
    declare(strict_types=1);
    
    
    /**
     * Class Db
     */
    class Db {
    
      protected static $db;
    
      public const SERVER = 'mssql';
    
      public const SCHEMA = 'stackoverflow';
    
      public const USER = 'sa';
    
      public const PASS = '';
    
      /**
       * Sets up the database connection
       *
       * @return \PDO
       */
      public static function databaseConnection(): \PDO {
        if (!self::$db) {
          try {
            $dsn      = 'odbc:Driver={FreeTDS};Server=' . self::SERVER . ';Database=' . self::SCHEMA . ';Port=1433';
            self::$db = new PDO($dsn, DB::USER, DB::PASS);
            self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          } catch (PDOException $e) {
            die('Error ' . $e->getMessage());
          }
        }
        return self::$db;
      }
    }
    

    Ad.php

    <?php
    declare(strict_types=1);
    
    
    /**
     * Class Ad
     */
    class Ad {
    
      public function dbAdmin(string $activity, int $units): bool {
        $conn = Db::databaseConnection();
        $conn->beginTransaction();
        $sql  = /** @lang TSQL */
          'INSERT INTO stackoverflow.dbo.ad (Activity, Units) VALUES (:Activity, :Units)';
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':Activity', $activity, PDO::PARAM_STR);
        $stmt->bindParam(':Units', $units, PDO::PARAM_INT);
        if ($stmt->execute()) {
          $conn->commit();
          return TRUE;
        }
        $conn->rollBack();
        return FALSE;
      }
    }
    

    SQL Server (Microsoft) Table

    CREATE TABLE ad
    (
      id       INT IDENTITY
        PRIMARY KEY,
      Activity VARCHAR(255),
      Units    INT
    )
    GO
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低