每次我提交我的网站表格时,它只会创建一个新ID。但是所有其他值都是空的,我该怎么办?
<?php
error_reporting(E_ALL);ini_set('display_errors',1);
$servername = "localhost";
$username = "seamaszhou";
$password = "123456";
$dbname = "guest";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO guest
(guestName,guestEmail,guestContent)
VALUES (:guestName, :guestEmail, :guestContent)");
$stmt->bindParam(':guestName', $guestName);
$stmt->bindParam(':guestEmail', $guestEmail);
$stmt->bindParam(':guestContent', $guestContent);
// insert a row
$guestName = "$guestName";
$guestContent = "$guestContent";
$guestEmail = "$guestEmail";
$stmt->execute();
?>
答案 0 :(得分:4)
因为您没有设置任何值。当你这样做时:
$guestName = "$guestName";
变量$guestName
被设置为包含变量$guestName
的当前值的字符串。由于它不包含任何内容,因此您只需将其设置为空字符串。
使用实际值。例如:
$guestName = "Some name";
或者,如果您真的希望将变量名称作为字符串,请使用单引号,这样它就不会被解释为变量:
$guestName = '$guestName';