简体   繁体   English

如何使用PDO从php文件插入数据?

[英]How do I insert data from a php file using PDO?

I'm trying to log some data for my website using PDO from a PHP file. 我正在尝试使用来自PHP文件的PDO为我的网站记录一些数据。 I have the following code which is called by by a javascript library, D3. 我有以下由JavaScript库D3调用的代码。 The call works fine, but when I run this code I get an "internal server error". 该调用工作正常,但是当我运行此代码时,出现“内部服务器错误”。

What am I doing wrong? 我究竟做错了什么? I have been following a guide on a website and I am basically using the same principles as them. 我一直在遵循网站上的指南,并且基本上使用与它们相同的原理。 If anyone can help I'd appreciate it. 如果有人可以提供帮助,我将不胜感激。 Thanks a lot in advance, my code is pasted below. 在此先感谢,下面粘贴了我的代码。 (Of course the database information is something valid) (当然,数据库信息是有效的)

    $hostname="xxxx";
    $username="xxxxxx";
    $pw="xxxxxxxx";
    $dbname="xxxx";

    try {
        $pdo = new PDO ("mysql:host=$hostname;dbname=$dbname","$username","$pw");
    } catch (PDOException $e) {
        echo "Failed to get DB handle: " . $e->getMessage() . "\n";
        exit;
    }

    //Gets IP for client.
    $ip = get_client_ip();
    //An email, format of string.
    $email = "test@test.dk";
    //An int, in this case 19.
    $prgm_name = $_GET["prgm"];
    //Piece of text, format of string of course.
    $prgm_options.=$prgm_name;
    $prgm_options.= " - ";
    $prgm_options.=$_GET["gene"];
    $prgm_options.=" - ";
    $prgm_options.=$_GET["data"];
    //Datasize, int.
    $data_size = 0;
    //Timestamp.
    $now = "NOW()";

    //Table name.
    $STAT_TABLE = "stat";

    $query = $pdo->prepare("INSERT INTO $STAT_TABLE (ip, email, prgm, options, datasize, date) VALUES (:ip, :email, :prgm_name, :prgm_options, :data_size, :now);");
    $query->execute(array(  ':ip'=>'$ip',
                             ':email'=>'$email',
                             ':prgm_name'=>$prgm_name,
                             ':prgm_options'=>'$prgm_options',
                             ':datasize'=>'$datasize',
                             ':now'=>$now));

Try the following Code to Insert 尝试以下代码插入

$count = $pdo->exec("INSERT INTO $STAT_TABLE(ip, email, prgm, options, datasize, date) VALUES ('$ip','$email',$prgm_name,'$prgm_options','$datasize',$now)))");
/*** echo the number of affected rows ***/
echo $count;

I like to bind each parameter individually. 我喜欢分别绑定每个参数。 I think it gives you more control over data types and sizes. 我认为它使您可以更好地控制数据类型和大小。

try {
  $sth = $pdo->prepare("INSERT INTO..."); 
  $sth->bindParam(":ip", $ip, PDO::PARAM_STR, 16);   
  $sth->bindParam(":email", $email, PDO::PARAM_STR, 30);     
  // etc...
  $sth->execute();
} catch (Exception $e) { 
  print_r($e);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何将数据从 javascript 插入到 php 表中,或者如何使用数据库而不是使用 localStorage 执行以下任务? - how do i insert data into a php table from javascript or How can i do the following task using database instead of using localStorage? 如何从选定的ajax下拉PHP插入数据 - How do I insert data from selected ajax dropdown PHP 如何使用PHP中的Foreach将数据从jquery文本框插入到MySql数据库中,或通过其他任何方法来实现此目标? - How do I INSERT data from jquery textBox into MySql database using Foreach in Php, or any other way to accomplish this goal? 如何使用 Ajax 和 JSON 从 PHP 文件访问变量? (数据未定义错误) - How do I access variables from PHP file using Ajax and JSON? (data is not defined error) 如何将PHP返回的输出插入HTML <div> ? (我正在使用WordPress) - How do I insert output from a PHP return into an HTML <div>? (I'm using WordPress) 如何使用 Google Sheets 中的数据将数组插入到 Google Doc 中? - How do I insert an array into a Google Doc using data from Google Sheets? 如何使用javascript将二进制文件插入mongodb? - How do I insert a binary file into mongodb using javascript? 如何将表格中的数据插入到表格中 - How do I insert data from a table to a form 如何使用 Nodejs 从文本文件读取数据并将数据插入 PostgreSQL? - How to read from text file and insert the data into PostgreSQL using Nodejs? 如何在同一 HTML 页面上显示 HTML GET 请求数据,使用单独的 PHP 文件来获取它? - How do I display the HTML GET request data on the same HTML page, using a separate PHP file to fetch it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM