使用php更新数据库中的值

时间:2016-04-26 05:18:52

标签: php mysql

我已经以edit.php的形式显示了数据,并再次将更新页面上的网址中的truk_id更改为update.php字段的数据,但它无法正常工作。任何人都可以检查这个

PHP代码

   <?php
 $truck_id=$_GET['truck_id'];
 print_r($truck_id);
 include("assets/database_con.php");
    if (isset($_POST['submit'])) 
  { 
   $truck_id=$_POST['truck_id'];   
            $truck_number= mysql_real_escape_string(htmlspecialchars($_POST['truck_number']));
            $truck_model= mysql_real_escape_string(htmlspecialchars($_POST['truck_model']));
            $truck_make= mysql_real_escape_string(htmlspecialchars($_POST['truck_make']));
            $truck_type= mysql_real_escape_string(htmlspecialchars($_POST['truck_type']));
            $truck_tierweight= mysql_real_escape_string(htmlspecialchars($_POST['truck_tierweight']));
            $truck_gvm= mysql_real_escape_string(htmlspecialchars($_POST['truck_gvm']));
            $truck_regodate= mysql_real_escape_string(htmlspecialchars($_POST['truck_regodate']));
            $truck_inspectaiondate= mysql_real_escape_string(htmlspecialchars($_POST['truck_inspectaiondate']));
            $sql1 ="UPDATE add_truck SET truck_number='$truck_number', truck_model='$truck_model', truck_make='$truck_make', truck_type='$truck_type', truck_tierweight='$truck_tierweight', truck_gvm='$truck_gvm', truck_regodate='$truck_regodate', truck_inspectaiondate='$truck_inspectaiondate' where truck_id='$truck_id'";
            echo $sql1;
            $results=$conn->query($sql);
        if($results)
        {
            print 'Success! record updated'; 
        }else{
            print 'no! record updated';
        }                   
    }
?>

4 个答案:

答案 0 :(得分:1)

您正在为单引号中的字段分配值。

主字符串用单引号括起来。

不解析单引号内的变量(不计算值)。

这称为变量插值。

对整个字符串使用双引号,对值使用单引号。

更正了SQL:

std::tuple_element

答案 1 :(得分:0)

您正在使用GET作为卡车ID而POST则使用所有其他变量 - 这是故意的吗?

$truck_id=$_GET['truck_id'];

应该是

$truck_id=$_POST['truck_id'];

或所有其他变量是否应来自GET数组?

答案 2 :(得分:0)

<input type="hidden" name="truck_id" value="<?php echo $_GET['truck_id']; ?>">

这是您的问题部分,您提交时无法从get方法获取值。因此,您需要在表单中添加一些隐藏字段,如此

if (isset($_POST['submit'])) 
  { 
   $truck_id=$_POST['truck_id']; 
  }

像这样更改你的代码

//Your code starts now
 <?php
include("header.php");
include("assets/database_con.php");
$truck_id= $_GET['truck_id'];
//print ($truck_id);
?>
<?php
//submit part in top that is better
 if (isset($_POST['submit']))
{
$truck_id=$_POST['truck_id'];
$truck_number= mysql_real_escape_string(htmlspecialchars($_POST['truck_number']));
$truck_model= mysql_real_escape_string(htmlspecialchars($_POST['truck_model']));
$truck_make= mysql_real_escape_string(htmlspecialchars($_POST['truck_make']));
$truck_type= mysql_real_escape_string(htmlspecialchars($_POST['truck_type']));
$truck_tierweight= mysql_real_escape_string(htmlspecialchars($_POST['truck_tierweight']));
$truck_gvm= mysql_real_escape_string(htmlspecialchars($_POST['truck_gvm']));
$truck_regodate= mysql_real_escape_string(htmlspecialchars($_POST['truck_regodate']));
$truck_inspectaiondate= mysql_real_escape_string(htmlspecialchars($_POST['truck_inspectaiondate']));
$sql1 ="UPDATE add_truck SET truck_number='$truck_number', truck_model='$truck_model', truck_make='$truck_make', truck_type='$truck_type', truck_tierweight='$truck_tierweight', truck_gvm='$truck_gvm', truck_regodate='$truck_regodate', truck_inspectaiondate='$truck_inspectaiondate' where truck_id='$truck_id'";
echo $sql1;
die();
$results=$conn->query($sql);
if($results)
{
    print 'Success! record updated';
}else{
    print 'no! record updated';
}
}
?>

我改变了你的代码,请看这个

 <form role="form" method="post"  action="">

在这里改变了一些东西(添加了动作=“”)

<div class="form-group">

<input type="hidden" name="truck_id" value="<?php echo $_GET['truck_id']; ?>">
<input type="submit" value="submit" name="submit" class="btn_full" id="submit-booking">
</div>

在这里也改变了一些东西(添加了名字=“提交”)

Dictionary<string, T>

不需要隐藏另一个div,因为它是'隐藏'

答案 3 :(得分:0)

Used a new code as solution :
    <?php
 if (isset($_POST['submit']))
{
$truck_id=$_POST['truck_id'];

...
}

and one hidden value with the submit name of attribute :
<input type="hidden" name="truck_id" value="<?php echo $_GET['truck_id']; ?>">
                                   <input type="submit" value="submit" name="submit" class="btn_full" id="submit-booking">