2011-05-19 70 views
-1
<?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運行PHP時插入數據爲什麼會出現意想不到的錯誤?

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()); 
    } 
} 

,它給了我

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

我不知道爲什麼就不能結束。

請幫助我在郵政那裏,我認爲有一些錯誤,我沒有找到它。

+3

正如您在語法高亮顯示中看到的那樣,您缺少一個引號,請確保您使用的IDE或編輯器的語法突出顯示 – 2011-05-19 10:00:41

+2

@Pekka - 甚至是SO的語法高亮顯示它。 - ) – 2011-05-19 10:05:47

回答

9

接近這個sql查詢與)"

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

關閉雙引號,把列值在單引號

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

一個側面說明:將它們發送到MySQL服務器之前逃離用戶輸入。

+0

注意:未定義的索引:在第2行的C:\ wamp \ www \ InsertProgram.php中的標題 – newbie 2011-05-19 11:33:48

+0

@newbie:'$ _POST ['title']'不存在檢查變量是否已設置在使用它們之前使用'isset()'方法將清除此通知 – 2011-05-19 11:36:30

+0

'nameValuePairs.add(new BasicNameValuePair(「title」,title));'我在java post中添加了這個,但是我不知道如何定義在PHP中 – newbie 2011-05-19 11:44:36

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

你忘了結束報價。

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

您沒有結束SQL字符串。添加「在字符串的末尾

相關問題