【问题标题】:why get unexpected error?为什么会出现意外错误?
【发布时间】:2011-05-19 09:59:05
【问题描述】:
<?php
$title = $_POST['title']; 
$date = $_POST['date'];
$time = $_POST['time'];
$channel = $_POST['channel'];
mysql_connect("localhost","root","");
mysql_select_db("imammuda");
$sql=mysql_query("insert into Program (ID, Title, Date, Time, Channel) values ('NULL', $title, $date, $time, $channel);
mysql_close();
?>

这是我插入数据的 POST

private void adddataintophp(String title, String date, String time, String channel){
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    //http post
    try{
        nameValuePairs.add(new BasicNameValuePair("title", title));
        nameValuePairs.add(new BasicNameValuePair("date", date));
        nameValuePairs.add(new BasicNameValuePair("time", time));
        nameValuePairs.add(new BasicNameValuePair("channel", channel));
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/insertprogram.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e){
        Log.e("log_tag", "Error in http connection"+e.toString());
    }
}

当运行 php 时,它给了我

Parse error: syntax error, unexpected $end in C:\wamp\www\InsertProgram.php on line 10

我不知道为什么它不能结束。

请在 POST 中帮助我,我认为那里有一些我没有找到的错误。

【问题讨论】:

  • 正如您在语法突出显示中所见,您缺少引号。确保使用带有语法高亮的 IDE 或编辑器
  • @Pekka - 甚至 SO 的语法高亮显示它。 :-)

标签: java php android


【解决方案1】:

用)"关闭这个sql查询

$sql=mysql_query("insert into Program (ID, Title, Date, Time, Channel) values ('NULL', $title, $date, $time, $channel)");

【讨论】:

    【解决方案2】:

    关闭双引号并将列值放在单引号中

    $sql=mysql_query("insert into Program (ID, Title, Date, Time, Channel) 
                    values ('NULL', '$title','$date', '$time', '$channel'");
    

    附注:在将用户输入发送到 mysql 服务器之前对其进行转义。

    【讨论】:

    • 这个怎么样?注意:未定义索引:第 2 行 C:\wamp\www\InsertProgram.php 中的标题
    • @newbie:$_POST['title'] 不存在。使用之前检查变量是否使用isset() 方法设置将删除此通知
    • nameValuePairs.add(new BasicNameValuePair("title", title)); 我在java帖子中添加了这个,但我不知道如何在php中定义
    【解决方案3】:
    $sql=mysql_query("insert into Program (ID, Title, Date, Time, Channel) values ('NULL', $title, $date, $time, $channel)");
    

    你忘记了结束语。

    【讨论】:

      【解决方案4】:
      mysql_query("insert into Program (ID, Title, Date, Time, Channel) values ('NULL', $title, $date, $time, $channel);
      

      您没有结束 SQL 字符串。在字符串末尾添加 "。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-14
        • 2019-01-02
        • 1970-01-01
        • 2013-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多