Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何防止更新数据库中的多个字段_Php_Mysql_Database_Forms - Fatal编程技术网

Php 如何防止更新数据库中的多个字段

Php 如何防止更新数据库中的多个字段,php,mysql,database,forms,Php,Mysql,Database,Forms,我正在尝试编辑数据(存储在DB中)。这是display.php。首先显示数据库中的数据(如果没有数据,则显示空白字段)。然后编辑按钮编辑数据库 <html> <body> <?php if(!isset($_POST['edit_pro'])) { ?> //get data from DB and display in table. <form> <input type="submit" name= "edit" valu

我正在尝试编辑数据(存储在DB中)。这是display.php。首先显示数据库中的数据(如果没有数据,则显示空白字段)。然后编辑按钮编辑数据库

<html>
<body>
<?php

if(!isset($_POST['edit_pro']))
{
?>
      //get data from DB and display in table.


<form>
<input type="submit" name= "edit" value="edit">
</form>

<?php
}
else
{
?>
<form name="edit_DB" action="edit.php">

//edit ...2 <select> fields and 1 text field.

//submit button
</form>
<?php
}
?>

//从数据库中获取数据并显示在表中。
//编辑…2个字段和1个文本字段。
//提交按钮
在edit.php中 我只是更新数据库。但如果我只想更改1个字段(问题是所有字段都会更新),该怎么办呢

<?php
include_once 'db_connect.php';

$db_con = dbConnect("dbname");

$uid = $_SESSION['uid'];

if(isset($_POST['edit']))
{

    $c = $_POST['c'];

    $s = $_POST['list'];

    $t = $_POST['nm'];

    $a = $_POST['a'];

    $sql = "UPDATE `user` SET `c` = ?, `s` = ?, `t` = ? WHERE u_id = ?";    

    $q = $db_con->prepare($sql);

    $q->execute(array($c,$s,$t,$uid));



    header("Location:display.php");

}
?>

这意味着请求的WHERE子句不起作用。检查是否在变量$t中传递了引号“以便在WHERE子句之前关闭$sql

$sql = "UPDATE `user` SET `c` = ?, `s` = ?, `t` = ? WHERE u_id = ?"; 
此查询意味着:

  • 更新表用户
  • 对于此表中的每一行,其中u_id=[某些值]
  • 将字段C、S和T设置为其他一些不同的值
  • 因此,您的查询一次更新3个字段,这是确定的,因为它应该做什么

    如果要更改此逻辑,要仅更新某些字段,则需要更改查询和参数,例如,如果只想更改
    c
    请使用:

    $sql = "UPDATE `user` SET `c` = ? WHERE u_id = ?";    
    $q = $db_con->prepare($sql);
    $q->execute(array($c, $uid)); // this array binds values to question marks, so count should be the same, we have 2 ? - we must use 2 variables
    
    对于c和t:

    $sql = "UPDATE `user` SET `c` = ?, `t` = ? WHERE u_id = ?";    
    $q = $db_con->prepare($sql);
    $q->execute();
    
    如果不知道参数的确切数量,则需要动态查询生成,如:

    $arr = array();
    $sqlA = array();
    if (isset($_POST['c']) && $_POST['c']) {
        $arr[] = $_POST['c'];
        $sqlA[] = '`c`=?';
    }
    if (isset($_POST['s']) && $_POST['s']) {
        $arr[] = $_POST['s'];
        $sqlA[] = '`s`=?';
    }
    if (isset($_POST['t']) && $_POST['t']) {
        $arr[] = $_POST['t'];
        $sqlA[] = '`t`=?';
    }
    
    if (count($arr)) {
        $sql = 'UPDATE `user` SET '.implode($sqlA, ',').' where u_id = ?';
        $arr[] = $uid;
    
        $q = $db_con->prepare($sql);
        $q->execute($arr);
    }
    

    请检查您的sql查询字符串,我想一定是有问题。您确定字段名中有单引号吗?是的,没错。我需要在那里添加一些条件。例如if(strlen($\u POST['t']){然后更新t值..}。