简体   繁体   English

如何在数据库表php中插入和更新值

[英]How to insert and update value into database table php

I am beginner in PHP and i want to insert value into my database table for my drop-down value using PHP insert query, I am having an error to insert data and also want to update the existence value of database table when insert new value into database table, here is my code help me to solve it, My Code is below : 我是PHP的初学者,我想使用PHP插入查询将值插入到数据库表中以获取下拉值,我在插入数据时遇到错误,并且还想在将新值插入数据库时​​更新数据库表的存在值数据库表,这是我的代码帮助我解决它,我的代码如下:

<?php 
include 'dbconfig.php'; 
?>
<html>
<head>
<title>Sales Panel App</title>
</head>

<body>

<form method="POST" name="myForm" action="<?php $_PHP_SELF ?>">
<table border="1">
 <tr>
<td>Sales Person List</td>
<td>
<?php


 $sql="select name,id from sales order by name"; 
    echo "<select name = 'salelist' value=''>Sales Person Name</option>";
    echo "<option value = 'Select Sales Person' selected>Select Sales    Person</option>";
   {
    foreach ($conn->query($sql) as $row){
    echo "<option value=$row[id]>$row[name]</option>"; 
   }
    echo "</select>";
   }
    echo "<br>";

   ?>

   </td>
  </tr>

   <tr>
    <td>No of Panels</td>
    <td><input type="text" name="panelnumber" size="1"></td>
    </tr>



   </table>
    <p><input type="submit" value="Submit" name="submit" id="submitdata"></p>
    </form>
  <?php

     $salelist = $_POST['salelist'];
     $panelnumber = $_POST['panelnumber'];

        $sql2 = "select * from sales where name = $salelist";
        $result = mysqli_query($sql2);

        if ($row = mysqli_fetch_array($result)) {
          $oldvalue = $panelnumber;
          $newvalue = $oldvalue + $panelnumber;
          $query = 'update sales where name = $panelnumber';
       }
         else {
         $qry = "insert into sales (id, name, panelnumber) values ('', $salelist, $panelnumber)";
        }
        ?>


            </body>
            </html>

Your problem is here 你的问题在这里

$query = 'update sales where name = $panelnumber';

You are not saying what to update. 您不是在说要更新什么。 You have to use one of the methods available to update an SQL row. 您必须使用一种可用的方法来更新SQL行。

For example, a correct one should be: 例如,正确的应该是:

$query = "update sales set city='rio' where name = '$panelnumber'";

Here is the official MySQL Documentation. 是官方的MySQL文档。 And you have one more error, if you write this: 如果您编写以下代码,则会遇到另一个错误:

$number = 5;
$variable = '$number';
echo $variable;

The output of $variable will not be "5", will be "$number", literally. 从字面上看, $variable的输出将不是“ 5”,而是“ $ number”。 If you want to set a variable in a string, you must us double quote ( " ) not single quote ( ' ) because single quote will literally save what is between them, and double will try to "translate" variables and other stuff. 如果要在字符串中设置变量,则必须使用双引号( )而不是单引号( ' ),因为单引号将字面意义上保存它们之间的内容,并且双引号将尝试“翻译”变量和其他内容。
The correct way would be: 正确的方法是:

$number = 5;
$variable = "$number";
echo $variable;

Here you have a detailed PHP official explanation about what I am describing. 在这里,您对我正在描述的内容有详细的PHP官方解释。

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

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