【问题标题】:How to fetch data and insert into another table in same database?如何获取数据并插入到同一数据库中的另一个表中?
【发布时间】:2021-01-20 06:38:46
【问题描述】:

我有 2 个表,一个有数据,另一个在同一个数据库中是空白的。

a)- 表“cusrec”是主表,其中包含数据。

b)- 表“订单”为空,我想在其中插入数据。

我试图从“cusrec”表中获取数据并将其插入“order”,当我回显时,它显示了“cusrec”表的数据,但它没有插入“order”表。两个表都在同一个数据库中。

代码是:

<?php
mysql_connect("localhost","root","");
mysql_select_db('dobhighat');
if(isset($_GET['search'])){
$srch = $_GET['srch'];
$que=mysql_query("select * from cusrec where custid='$srch' OR mobile='$srch'");
$ftch=mysql_fetch_array($que);
$scustid=$ftch['custid'];
$sname=$ftch['name'];
$smobile=$ftch['mobile'];
$totcloth=$ftch['clothpackage'];
if(isset($_POST['confirm']))
{
$ordernum=$_REQUEST['ordernum'];
$orderdate=date('d/m/y');
$ordercloth=$_REQUEST['ordercloth'];
$clothrem=$totcloth-$ordercloth;
$abc=mysql_query("insert into order(custid,name,mobile,totcloth,orderno,orderdate,ordercloth,clothrem)values('$scustid','$sname','$smobile','$totcloth','$ordernum','$orderdate','$ordercloth','$clothrem')");
}
}
?>

<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
<?php $orddate=date('d/m/y'); ?>
<form name="form 1" action="" method="get">
<div align="right"><input type="text" name="srch" placeholder="Search by Id or Mobile" size="25">
<input type="submit" name="search" value="Search"></div>
</form>

<form name="form2" action="" method="post">
  <table>
  <tr>
    <td width="103">Order Date</td>
    <td width="94">Customer Id</td>
    <td width="53">Name</td>    
    <td width="71">Mobile</td>
    <td width="144">Order No.</td>    
    <td width="144">No.of Clothes</td>
  </tr>
  <tr>
    <td><?php echo $orddate; ?></td>
    <td><?php echo @$ftch['custid'];  ?></td>
    <td><?php echo @$ftch['name'];  ?></td>
    <td><?php echo @$ftch['lname'];  ?></td>
    <td><?php echo @$ftch['mobile'];  ?></td>
    <td><input type="text" name="ordernum" required></td>    
    <td><input type="text" name="ordercloth" required></td>
  </tr>
  <tr><td colspan="8"><center><input type="submit" name="confirm" value="Confirm"></center></td></tr>
</table>
    </form>
</body>
</html>

需要帮助

【问题讨论】:

  • 在继续之前,请注意mysql_* 函数已被弃用,并将在未来被删除。您应该改用mysqliPDO
  • if(isset($_POST['confirm']))。这永远不会是真的,因为您的表单方法是get
  • 好的。谢谢你的建议。但是我的问题呢
  • 改了但是同样的问题,没有插入
  • 可能是列名出错..echo $abc ,然后在phpmyadmin中运行查询。查询是否正确就知道了。

标签: php mysql database


【解决方案1】:

忘记我对文档的形式和结构所说的一切(尽管它最终可能有用,因为我不知道GETPOST 的可靠性如何)。

我是一只渡渡鸟,刚刚意识到您的表名是orderORDER是SQL中的保留关键字,这就是你的SQL语句不正确的原因!只需将名称放在 ` 之间即可:

$abc=mysql_query("insert into `order`(custid,name,mobile,totcloth,orderno,orderdate,ordercloth,clothrem)values('$scustid','$sname','$smobile','$totcloth','$ordernum','$orderdate','$ordercloth','$clothrem')");

或者您可能希望(如果可能)重命名表格以避免混淆和未来的问题。

这并不意味着代码是完美的。您仍然应该查看其他用户的建议和下面的列表,并对其进行改进。特别是安全问题。


一旦你解决了这个问题,插入就可以工作了;但仍有许多问题需要解决:

  • 您不应该使用mysql 函数,而是转到mysqliPDO
  • 您应该清理所有进入数据库或页面的值:
    • 您的代码受到 SQL 注入的影响(请参阅相关的 cmets)。
    • 您的代码受 XSS 的影响(请参阅相关的 cmets)。
  • 您页面中的 doctype 不正确(如果您需要 html5,则应该是 &lt;!doctype html&gt; 而不是 &lt;!doctype&gt;
  • 名称和 ID 中不得包含空格(请阅读此处了解名称和 ID 表示法:http://www.w3.org/TR/html401/types.html#type-name
  • 如其他答案所述,可以改进 SQL 语句。您的解决方案可能运行良好(我没有发现任何明显的错误),但它并不理想,并且可能会出现性能问题和其他类型的问题(例如:如果两个不同的用户拥有相同的电话号码)。

当我快速浏览代码时,我可能没有注意到更多的事情。

【讨论】:

  • 您的答案听起来很合理,但其中大部分都超出了头脑。您可以纠正我的代码并将其发布为答案吗?感谢您的真诚努力。
  • cusrecorder 表的结构是什么?
  • 你看到的地方,#ftch 是 cusrec 表中的数据,你看到的地方是 $_REQUEST,这是我要插入数据的表顺序。
  • 我不知道你的评论是什么意思。每个表中的字段类型是什么?即:您将全部插入为 varchar,但其中一些是数字,orderdate 是日期还是 varchar?
  • @Pawan 完成。这实际上是一件非常简单的事情,只是一个order 而不是订单。我应该早点意识到:$
【解决方案2】:

您是否考虑过从选择中插入?比如……

INSERT INTO ORDER 
   ( custid, name, mobile, otherFields, etc... )
   SELECT same, ordered, fields, as, theInsert
      from custrec where custid='$srch' OR mobile='$srch'

我看到您的 select where 子句的唯一问题是您可能通过 OR mobile='%srch' 获得多个客户并导致重复订单。

【讨论】:

  • 我按照你的方法试过了,但仍然是同样的问题。真的很失望,我的代码
【解决方案3】:

您可以直接从SQL进行操作

INSERT INTO order
       ( custid,
         name,
         mobile,
         totcloth,
         orderno,
         orderdate,
         ordercloth,
         clothrem) 
     SELECT 
            custid,
            name,
            mobile,
            totcloth,
            orderno,
            orderdate,
            ordercloth,
            clothrem
       FROM cusrec
           WHERE custid='$srch' OR mobile='$srch'"

;

您只需要将from table 的列匹配到to table

【讨论】:

  • 从 select 语句中插入时,您的语法不正确。您不使用 VALUES ( ) 子句部分,选择是值的基础。
【解决方案4】:

我认为使用下面提到的 PHP MYSQLI 代码可以插入和获取相同的数据。

$id=trim($_POST['id']);
$email=trim($_POST['email'];
$env_name=trim($_POST['env_name'];
$sql="INSERT INTO integration (id,email,env_name) VALUES ($id,$email'$envName')";
if(mysqli_query($conn,$sql)){
    $selectSql="SELECT id,email,env_name from integration WHERE id=$id and email='$email'";
    $result=mysqli_query($conn,$selectSql){
        if($result){ 
            $row=mysqli_fetch_array($result, MYSQLI_ASSOC);
            return $row;
        }
    }    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2015-03-31
    相关资源
    最近更新 更多