如果结果存在然后更新并不插入?
问题描述:
我想调整此代码,以便如果结果已经存在于数据库中,然后更新它,而不是插入?如果结果存在然后更新并不插入?
有人能告诉我我该怎么做。我对mysql和php真的很陌生。
<?php
require_once('includes/session.php');
require_once('includes/functions.php');
require('includes/_config/connection.php');
session_start();
confirm_logged_in();
if (isset ($_GET['to'])) {
$user_to_id = $_GET['to'];
}
if (!isset($_GET['to']))
exit('No user specified.');
$user_id = $_GET['to'];
$result = mysql_query("SELECT * FROM ptb_likes WHERE liked_id ='".$user_to_id."' ");
mysql_query("INSERT INTO ptb_likes (user_id, liked_id) VALUES (".$_SESSION['user_id'].", ".$user_to_id.")");
if($result)
{
mysql_query("UPDATE ptb_likes SET user_id_has_liked='1' WHERE user_id=".$_SESSION['user_id']."")
or die(mysql_error());
mysql_query("UPDATE ptb_likes SET likes='1' WHERE liked_id=\"$profile_id\"")
or die(mysql_error());
header("Location: {$_SERVER['HTTP_REFERER']}");
}
?>
之间有什么区别你的[最后](http://stackoverflow.com/questions/14799814/reverse-mysql-function-instead -of-insert-delete-and-update-enum-from-1-to-0)question and this? – 2013-02-10 16:53:08
我建议你看看那里的所有SQL教程之一。你会在几分钟内找到答案。我可以告诉你没有研究过这个。 – 2013-02-10 16:53:24
你应该改变你的mysql函数。请参阅http://www.php.net/manual/en/function.mysql-query.php上的红框。它已被弃用。你应该切换到mysqli或者pdo。 – chill0r 2013-02-10 16:56:50