php 判断是否有相同的ID,如果有就修改数据库字段,没有就插入数据库字段

前端之家收集整理的这篇文章主要介绍了php 判断是否有相同的ID,如果有就修改数据库字段,没有就插入数据库字段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

PHP代码

<pre class="has">
<?php

header("Content-Type:text/html;charset=utf8"); 
header("Access-Control-Allow-Origin: *"); //解决跨域
header('Access-Control-Allow-Methods:POST');// 响应类型  
$link=mysql_connect("localhost","root","root"); 
mysql_select_db("business_card",$link); //选择数据库
mysql_query("SET NAMES utf8");//解决中文乱码问题
error_reporting(E_ALL &amp; ~E_NOTICE);

header("Content-Type:text/html;charset=utf8");
if ($_FILES["file"]["error"] > 0)
{
echo "错误: " . $_FILES["file"]["error"] . "
";
}
else
{
}
$if_up_img = $_POST['if_up_img'];
//提交文本
if($if_up_img==0){
$nickname = $_POST['nickname'];
$head = $_POST['head'];
$phone = $_POST['phone'];
$mail = $_POST['mail'];
$name = $_POST['name'];
$sex = $_POST['sex'];
$wx = $_POST['wx'];
$region = $_POST['region'];
$position = $_POST['position'];
$geology = $_POST['geology'];
$detail = $_POST['detail'];
$now_time = $_POST['now_time'];
$id = $_POST['id'];

    $q = "SELECT * FROM business_card"; //<a href="/tag/SQLchaxun/" target="_blank" class="keywords">SQL查询</a>语句 SELECT * FROM 表名
    $result = <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("SELECT * FROM business_card
    WHERE id='$id'");
    $num=<a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_num_rows($result);  //<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>行数
    if($num>0){

                //<a href="/tag/xiugai/" target="_blank" class="keywords">修改</a><a href="/tag/shujuku/" target="_blank" class="keywords">数据库</a>字段
                <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("UPDATE business_card SET phone = '$phone'
                WHERE id = '$id'");
                <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("UPDATE business_card SET mail = '$mail'
                WHERE id = '$id'");
                <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("UPDATE business_card SET name = '$name'
                WHERE id = '$id'");
                <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("UPDATE business_card SET sex = '$sex'
                WHERE id = '$id'");
                <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("UPDATE business_card SET wx = '$wx'
                WHERE id = '$id'");
                <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("UPDATE business_card SET region = '$region'
                WHERE id = '$id'");
                <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("UPDATE business_card SET position = '$position'
                WHERE id = '$id'");
                <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("UPDATE business_card SET geology = '$geology'
                WHERE id = '$id'");
                <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("UPDATE business_card SET detail = '$detail'
                WHERE id = '$id'");
    }else{
        //插入数据到<a href="/tag/shujuku/" target="_blank" class="keywords">数据库</a> 
        $str<a href="/tag/sql/" target="_blank" class="keywords">sql</a> = "insert into business_card (nickname,head,phone,mail,name,sex,wx,region,position,geology,detail,id,now_time) values('$nickname','$head','$phone','$mail','$name','$sex','$wx','$region','$position','$geology','$detail','$id','$now_time')";
        $result = @<a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query($str<a href="/tag/sql/" target="_blank" class="keywords">sql</a>);

    }
     echo $num;




}else{
//提交<a href="/tag/tupian/" target="_blank" class="keywords">图片</a>
    $now_time = $_POST['now_time'];
    $img = $_FILES['file']['name'];
    $q = "SELECT * FROM business_card"; //<a href="/tag/SQLchaxun/" target="_blank" class="keywords">SQL查询</a>语句 SELECT * FROM 表名
    $rs = <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query($q); //<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>数据集
    while($row=<a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_fetch_array($rs)){
        $result = @<a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query($str<a href="/tag/sql/" target="_blank" class="keywords">sql</a>);
            if($row["now_time"]==$now_time){
                if($row["img"]!=""){
                $ab =$row["img"].','.$img;
                }else{
                $ab =$img;
                }
                //<a href="/tag/xiugai/" target="_blank" class="keywords">修改</a><a href="/tag/shujuku/" target="_blank" class="keywords">数据库</a>字段
                <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query("UPDATE business_card SET img = '$ab'
                WHERE now_time = '$now_time'");
                echo urldecode(json_encode($ab));
            }
    }

    move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]); 
}       

?>

猜你在找的PHP相关文章