search

Home  >  Q&A  >  body text

Something went wrong there, why can't I add information?

<?php

header("content-type:text/html;charset=utf-8");

$dbms = "mysql";
$dbhost = "localhost";
$dbuser = "root";
$dbpwd = "";
$dbname = "student";
$dsn = "$dbms:host=$dbhost;$ dbname";
try{
$pdo = new PDO($dsn,$dbuser,$dbpwd);
$pdo->query("set names utf8");
$sql = "insert into stu(uid,name,sex,age,grade,class,cre_time) values ​​('450000','susan','male','23 years old','kindergarten','first class',now( ))";
$row = $pdo->exec($sql);
echo "Affected the ".$row." row";
}catch(PDOException $e){
die("An error occurred:".$e->getMessage());
}
?>

//




stu table structure

id int(6) automatic increment

uid varchar(8)

##name varchar(12) NULL

sex varchar(4) NULL

##age varchar(10) NULL

grade varchar(10) NULL

##class varchar(6) NULL

cre_time datetime NULL

mod_time datetime NULL

不吃玉米糖不吃玉米糖2605 days ago1012

reply all(4)I'll reply

  • 不吃玉米糖

    不吃玉米糖2017-11-13 20:41:55

    . . . . .

    I was careless, $dsn = "$dbms:host=$dbhost;$dbname"; This is wrong


    It should be $dsn = "$ dbms:host=$dbhost;dbname = $dbname"; missing dbname=

    reply
    0
  • PHP中文网

    PHP中文网2017-11-11 09:31:38

    What does the execution result show? No information? Or is there an error in the report?

    reply
    0
  • 不吃玉米糖

    Thank you, I found it. I accidentally missed dbname=.

    不吃玉米糖 · 2017-11-13 20:42:46
  • Cancelreply