I want to INSERT data into an existing table using MySQL with PDO. I have no idea how to use the WHERE statement inside the INSERT INTO query. My code:
if(isset($url)){
if(preg_match($pattern, $url) ){
$user = $_SESSION['user'];
try {
// prepared statement to insert user data
$sql = "INSERT INTO user (website) where username = :username VALUES (:website)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':website', $url);
$stmt->bindParam(':username', $user);
$stmt->execute();
}
//Exception handling
catch(PDOException $e)
{
$urlError = $e->getMessage();
}
Each time i try to INSERT it fails. I am new with php so don't go too hard on me please.
I'm going to guess that you don't want insert
. You really want update
:
UPDATE user
SET website = :website
WHERE username = :username;
UPDATE
changes columns in existing rows. INSERT
adds new rows into a table.
Check this:
if(isset($url)){
if(preg_match($pattern, $url) ){
$user = $_SESSION['user'];
try {
// prepared statement to insert user data
$sql = "UPDATE user SET website = :website WHERE username = :username";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':website', $url);
$stmt->bindParam(':username', $user);
$stmt->execute();
}
//Exception handling
catch(PDOException $e)
{
$urlError = $e->getMessage();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.