<? if($_SERVER['REQUEST_METHOD'] == "POST"){
include_once("includes/connect.php");
session_start();
$error = array();
$success = "The artist has been added!";
if(!empty($_POST['artist-name'])){
$artistName = $_POST['artist-name'];
} else{
$error[] = "Please enter a valid artist name!";
}
if(empty($error)){
//prepared statement goes here "INSERT INTO artist VALUES(?)"
try{
$pdo = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare("INSERT INTO artist(name) VALUES(:artist)");
$stmt->execute(array("artist" => $artistName));
while($row = $stmt->fetch()) {
print_r($row);
}
} catch(PDOException $e){
echo $e->getMessage();
}
//$_SESSION["success"] = $success;
} else {
$_SESSION["error"] = $error;
}
header("Location: ../add-artist.php");
exit();
}
?>
答案 0 :(得分:1)
$stmt = $pdo->prepare("INSERT INTO artist VALUES(:artist)");
该参数名为:artist
。
$stmt->execute(array('artist' => $artistName));
您使用artist
调用它。冒号也应该在那里,即使它是在内部添加的。
关于错误,在这种情况下唯一不会被异常捕获的错误是致命错误。在php.ini中使用error_reporting = -1
和display_errors = On
会使错误可见。否则,请使用error_log
.ini设置。