简体   繁体   English

从多个SQL表中获取信息,并使用php放入一个表中

[英]Grab info from multiple sql tables and put into one using php

At the moment this code updates the user info in MYSQLTABLE1. 目前,此代码更新了MYSQLTABLE1中的用户信息。 What I also want done is user info to be copied to WMYSQLTABLE which I can do but I also want a code from MYSQLTABLE2 to be copied over into a column in WMYSQLTABLE as well. 我还想做的是将用户信息复制到WMYSQLTABLE中,我可以这样做,但是我也希望将MYSQLTABLE2中的代码也复制到WMYSQLTABLE中的列中。 Here is the part I need changing: 这是我需要更改的部分:

   $sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
    ('','$ip','$date','$address')";      
    $res_insert2 = mysql_query($sql_insert2)or die(mysql_error());
//Need to also insert code from first row from      column named 'codes' in MYSQLTABLE2.

Actual code, it's a bit messy at the moment and the if/else statements do the exact same at the moment. 实际的代码,此刻有点混乱,而if / else语句此刻完全相同。 It works but I get the error " Column count doesn't match value count at row 1" because I cannot fill the last column with the code from MYSQLTABLE2. 它可以工作,但出现错误“列数与第1行的值数不匹配”,因为我无法用MYSQLTABLE2中的代码填充最后一列。

<?php
include("includes/config.php");
include("includes/mysql.php");
include("amount.php");

if(isset($_POST['func']))

    {

        $address        = $_POST['address'];
        $ip             = $_POST['ip'];
        $date           = $_POST['date'];
        $time           = $_POST['time'];
        $price      = $_POST['price'];
        $increment              = $_POST['increment'];
        $id             = $_POST['id'];
        $num = 0;



        $sql_check_address = "SELECT * FROM ".MYSQLTABLE1." WHERE address='$address'";
        $res_check_address = mysql_query($sql_check_address)or die(mysql_error());
        $num               = mysql_num_rows($res_check_address);
        $row               = mysql_fetch_assoc($res_check_address);


        if($num > 0)
            {   
                $address        = $row['address'];
                $ip             = $row['ip'];
                $date           = $row['date'];
                $oldprice   = $row['price'];
                $id             = $row['id'];

                $newprice   = $oldprice - $payAmount*200;
                $newinc        = $increment - 200;

                $sql_update1 = "UPDATE ".MYSQLTABLE1." SET ip='$ip',date='$date',price='$newprice',increment='$newinc',address='$address' WHERE id='$id'";  
                $res_update1 = mysql_query($sql_update1)or die(mysql_error());

                /////////////////Insert user info and copy code from MYSQLTABLE2 to WMYSQLTABLE2

                $sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
                ('','$ip','$date','$address')";
                $res_insert2 = mysql_query($sql_insert2)or die(mysql_error());

            }
        else{
            $address        = $row['address'];
            $ip             = $row['ip'];
            $date           = $row['date'];
            $oldprice   = $row['price'];
            $id             = $row['id'];

            $newprice   = $oldprice - $payAmount*200;
            $newinc        = $increment - 200;

            $sql_update = "UPDATE ".MYSQLTABLE1." SET ip='$ip',date='$date',price='$newprice',increment='$newinc',address='$address' WHERE id='$id'";   
            $res_update = mysql_query($sql_update)or die(mysql_error());

            /////////////////Insert user info and copy code from MYSQLTABLE2 to WMYSQLTABLE2

            $sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
                ('','$ip','$date','$address')";
            $res_insert2 = mysql_query($sql_insert2)or die(mysql_error());

            e
                }

    }

Any help will be greatly appreciated. 任何帮助将不胜感激。

The general form of the query would be: 查询的一般形式为:

$sql_insert = "INSERT INTO " . WMYSQLTABLE2 . "(code, ip, date, address)
               SELECT code, '$ip', '$date', '$address'
               FROM OtherTable
               WHERE <put something here to select the row>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM