php - 如何获取数据并插入到同一数据库中的另一个表中?

标签 php mysql database

我有 2 个表,一个包含数据,另一个在同一数据库中为空白。

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

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

我尝试从表“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>

需要帮助

最佳答案

忘记我所说的有关文档的表单和结构的所有内容(尽管它最终可能有用,因为我不知道 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 注入(inject)(请参阅相关评论)。
    • 您的代码容易受到 XSS 的影响(请参阅相关评论)。
  • 您页面中的文档类型不正确(如果您需要 html5,则应该是 <!doctype html> 而不是 <!doctype>
  • 名称和 ID 中不得包含空格(有关名称和 ID 表示法,请阅读此内容: http://www.w3.org/TR/html401/types.html#type-name )
  • 正如其他答案中所述,SQL 语句可以改进。您拥有的解决方案可能工作正常(我没有发现任何明显的错误),但它并不理想,并且可能会出现性能问题和其他类型的问题(例如:如果两个不同的用户具有相同的电话号码)。

而且可能还有更多我没有注意到的事情,因为我只是快速浏览了代码。

关于php - 如何获取数据并插入到同一数据库中的另一个表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26122283/

相关文章:

PHP:连接表查询以创建类别和子类别?

MySQL MariaDB 不会用外键创建表

php - 如何在 PHP 和 MySQL 中同时将数据插入到两个或多个表中?

php - 如何减少多个依赖查询

php - 预期响应代码 250 但得到代码 "554",消息“554 消息被拒绝 | Laravel 使用 TO 电子邮件

mysql - 如何使用 XSL 模板清理 Mysql 查询?

Mysql触发器帮助-插入之前

php - 如何创建有效的 mysql 数据库设计,为多个用户存储不同的值?

java - 如何将数组保存到 PHP 数据库中

php - Canvas 的 Facebook getLoginUrl