![](/img/trans.png)
[英]How can I make sure PHP won't give me the "array to string conversion error"?
[英]How Can I make sure there is no missing Value in mysql query string php
我有一個 php 類和方法,可以將記錄創建到 Mysql 表中,如下所示;
<?php
class Site{
// database connection and table name
private $conn;
private $table_name = "sites";
// object properties
public $id;
public $name;
public $location;
public $saddress;
public $duration;
public $created;
// create new site record
function create(){
// to get time stamp for 'created' field
$this->created=date('Y-m-d H:i:s');
// insert query
$query = "INSERT INTO " . $this->table_name . "
SET
site_name = :name,
site_location = :location,
site_address = :address,
site_duration = :duration,
created = :created";
// prepare the query
$stmt = $this->conn->prepare($query);
// sanitize
$this->name=htmlspecialchars(strip_tags($this->name));
$this->location=htmlspecialchars(strip_tags($this->location));
$this->saddress=htmlspecialchars(strip_tags($this->saddress));
$this->duration=htmlspecialchars(strip_tags($this->duration));
$this->created=htmlspecialchars(strip_tags($this->created));
// bind the values
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':location', $this->location);
$stmt->bindParam(':address', $this->saddress);
$stmt->bindParam(':duration', $this->duration);
$stmt->bindParam(':created', $this->created);
// execute the query, also check if query was successful
if($stmt->execute()){
return true;
}else{
$this->showError($stmt);
return false;
}
}
public function showError($stmt){
echo "<pre>";
print_r($stmt->errorInfo());
echo "</pre>";
}
}
?>
我的問題是,我嘗試提交 HTML 表單的任何內容都會出現錯誤,內容為:
Array
(
[0] => 23000
[1] => 1048
[2] => Column 'site_address' cannot be null
)
我很困惑,因為我的 HTLM 表單字段和數據庫表列字段名稱寫入正確,每當我從查詢中刪除 site_address 字段時,其他數據都會插入到表中,所以應該有人幫忙。 這是類的實現;
<?php
// get database connection
$database = new Database();
$db = $database->getConnection();
// pass connection to objects
$Construction = new Site($db);
// if the form was submitted -
if(isset($_POST['submit'])){
// set user property values
$Construction->name = $_POST['sitename'];
$Construction->location = $_POST['sitelocation'];
$Construction->saddress = $_POST["siteaddress"];
$Construction->duration = $_POST['siteduration'];
//Check if Construction Site already Exist
if($Construction->siteExists())
{
echo '<div class="alert alert-danger text-center">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Sorry!</strong> User Already Exist.
</div>';
}else{
$Construction->create();
}
}
?>
你在發帖
$_POST["站點地址"];
當它應該是
列“site_address”不能為空
嘗試$_POST['site_address']
代替
站點地址=:地址,
這不應該是地址嗎???
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.