我需要将name
,lastname
,number
和email
发送到数据库。我创建了这段代码:
<?php
echo $name;echo "<br>";
echo $lastname;echo "<br>";
echo $email;echo "<br>";
echo $celnumber;echo "<br>";
$servername = "localhost";
$username = "usertest1";
$password = "1234";
$dbname="usertest1";
try {
$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 tabela_script (name, lastname, email, celnumber)
VALUES (:name, :lastname, :email, :celnumber)");
$stmt->bindParam(':firstname', $name);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':celnumber', $celnumber);
$stmt->execute();
echo "New record created successfully";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
echo "<br>";
echo "<a href=\"main_html.html\"><h1>TRY AGAIN</h1></a>";
}
$conn = null;?>
得到以下错误:
错误:SQLSTATE [HY093]:参数号无效:参数未定义
答案 0 :(得分:1)
我必须在本地服务器上尝试以下代码。
更改
$stmt->bindParam(':firstname', $name);
要
$stmt->bindParam(':name', $name);
问题是确切的名称与“Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined