我正在将姓名、号码和公司插入到数据库中。
我的表格非常简单:
id(primary key)
name
slideView
company
如果传递的名称存在,我需要更新此信息,否则使用这些数据创建新行。我已经查看了REPLACE INTO
,但我认为那对我不起作用...因为我根本不接触ID。
我的代码是:
insertData($name,$count,$company);
function insertData($name, $count, $company) {
#Try/Catch statement to connect to DB, and insert data
try {
#DB username/password
$usernameDB = '****';
$passwordDB = '****';
#Create new PHP Database Object with the address, username, and password as parameters
$pdo = new PDO('mysql:host=localhost;dbname=*****', $usernameDB, $passwordDB);
#Set pdo attributes to handle errors
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
#assign the sth variable (sth means statement handle) to insert the data
$sth = $pdo->prepare("REPLACE INTO ***** SET name = ?, slideView = ?, company = ?");
#Execute the insert
$sth->execute(array($name,$count,$company));
#Error if can't connect or insert
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}#/try
}
我是SQL的新手,尚未找到一个好的方法来做到这一点。
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET slideView=NULL, company=NULL' at line 1
- mdance5.1.61
。 - mdancename
设置为唯一行了吗? - Fluffeh