$ _POST PHP UPDATE错误而不是发送选项

时间:2014-10-17 21:45:03

标签: php post mysqli isset

我正在尝试$ _POST更新到以下mysql表:
注意:有3个下拉菜单:状态,类别和访问权限

app_generalData
---------------
app_id
table
status_id
category_id
tags
access_id

使用print_r($ _ POST)和echo(s)进行的测试确保表单$ _POST正常工作:

Array ( 
        [MAX_FILE_SIZE] => 100000
        [app_id] => 1 
        [title] => Nothing Special 
        [status] => 
        [category] => 
        [tags] => new tag 
        [access] => 
        [update] => Update ) 


表单中的数据。

  • ID: 1
  • 标题:没什么特别的
  • 状态:
  • 分类
  • 标签:新标签
  • 使用:

产生的错误消息:

Error querying database for General Data.

期望的结果:

  1. $ _在db
  2. 中更新POST项目

    更新代码段:

    // Post the UPDATE to app_generalData
    if (isset($_POST['update'])) {
      // print_r($_POST);
      // echo '<br />';
    
    // Connect to the database
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
    // Grab the data from the POST
    
      // General Data
      $app_id = mysqli_real_escape_string($dbc, trim($_POST['app_id']));
      $title = mysqli_real_escape_string($dbc, trim($_POST['title']));
      $status = mysqli_real_escape_string($dbc, trim($_POST['status']));
      $category = mysqli_real_escape_string($dbc, trim($_POST['category']));
      $tags = mysqli_real_escape_string($dbc, trim($_POST['tags']));
      $access = mysqli_real_escape_string($dbc, trim($_POST['access']));
    
    // Confirm success with the user
    
        echo '<h1>Data from the form.</h1><br />';
        echo 'ID: ' . $app_id .'<br />';
        echo 'Title: ' . $title .'<br />';
        echo 'Status: ' . $status .'<br />';
        echo 'Category: ' . $category .'<br />';
        echo 'Tags: ' . $tags .'<br />';
        echo 'Access: ' . $access .'<br />';
    
        echo '<br />';
    
    // Write the data to the database
      $query = "UPDATE      app_generalData
                SET         title = $title,
                            status_id = $status,
                            category_id = $category,
                            tags = $tags,
                            access_id = $access
                WHERE       app_id = $app_id
                ";
    
      mysqli_query($dbc,$query)
            or die('Error querying database for General Data.');
    
    // close MySQL
      mysqli_close($dbc);
    
      exit();
    }
    else {
      echo 'Please enter all details below.';
    }
    

2 个答案:

答案 0 :(得分:1)

每当你得到mysql_errors时,如果只是通过查看代码找不到问题,我建议你做的第一件事是回应查询。

这里似乎是你的问题是你没有在''(引号)中包围你的字符串,如:

$query = "UPDATE      app_generalData
    SET         title = '$title',
                status_id = '$status',
                category_id = '$category',
                tags = '$tags',
                access_id = '$access'
    WHERE       app_id = '$app_id'
    ";

答案 1 :(得分:0)

运行print_r($query)后,我注意到该查询发布了以下内容:

UPDATE app_generalData
SET title = Nothing Special,
    status_id = ,
    category_id = ,
    tags = new tag,
    access_id = 
WHERE app_id = 1

错误原因:值周围没有标记(&#34;或&#39;)?

工作解决方案:

我已将查询更新为以下

UPDATE app_generalData
SET title = '".$title."',
    status_id = '".$status."',
    category_id = '".$category."',
    tags = '".$tags."',
    access_id = '".$access."'
WHERE app_id = $app_id
            ";