2013-09-24 38 views
0

我有一個腳本,其中插入2通知到數據庫表中,但它只是添加第一個音符,而不是第二個。我已經試過周圍交換他們,它仍然只是張貼了第一個2。這裏是我的代碼:(!他們都==「」)PHP腳本不執行第二個命令

<?php 
$type = "---"; 
$title = "---"; 
$note1 = "Note 1"; 
$note2 = "Note 2"; 
?> 
<?php 
if($business1 !== ""){ 
    $insert_note1_sql = "INSERT INTO notes (id, recipient, type, title, post, message, date) VALUES('$id', 'Business 1', '$type', '$title', '$event', '$note1', '$time')"; 
    $insert_note1_res = mysqli_query($con, $insert_note1_sql); 
}; 
?> 
<?php 
if($business2 !== ""){ 
    $insert_note2_sql = "INSERT INTO notes (id, recipient, type, title, post, message, date) VALUES('$id', 'Business 2', '$type', '$title', '$event', '$note2', '$time')"; 
    $insert_note2_res = mysqli_query($con, $insert_note2_sql); 
}; 
?> 

任何人都可以看到,爲什麼第二個不行就不貼了?

+1

您需要了解如何使用錯誤處理程序。 – Kermit

+0

爲什麼你不斷關閉和打開php標籤?沒有html,所以沒有理由這樣做。 –

+0

什麼是business2? –

回答

2

也許id字段是主鍵字段,因爲你正在使用兩個相同的$id查詢插入,你會得到一個重複的密鑰違規。

您應該檢查失敗的返回值,例如

$insert_note1_res = mysqli_query(...); 
if ($insert_note1_res === FALSE) { 
    die(mysqli_error()); 
} 
+0

沒有更多信息,我認爲馬克B是正確的。 –

+0

啊,當然,我已經修改它產生一個$ id2,現在所有工作正常 – user2737457

+0

@ user2737457你真的應該讓你的主鍵自動增量。 –

0

IF語句不使用;

if($business2 !== ""){ 
    $insert_note2_sql = "INSERT INTO notes (id, recipient, type, title, post, message, date) VALUES('$id', 'Business 2', '$type', '$title', '$event', '$note2', '$time')"; 
    $insert_note2_res = mysqli_query($con, $insert_note2_sql); 
}; // <--------- ISSUE 

有道

<?php 
$type = "---"; 
$title = "---"; 
$note1 = "Note 1"; 
$note2 = "Note 2"; 

echo $business1; 

if($business1 !== ""){ 
    $insert_note1_sql = "INSERT INTO notes (id, recipient, type, title, post, message, date) VALUES('$id', 'Business 1', '$type', '$title', '$event', '$note1', '$time')"; 
    $insert_note1_res = mysqli_query($con, $insert_note1_sql); 
} 

echo $business2; 

if($business2 !== ""){ 
    $insert_note2_sql = "INSERT INTO notes (id, recipient, type, title, post, message, date) VALUES('$id', 'Business 2', '$type', '$title', '$event', '$note2', '$time')"; 
    $insert_note2_res = mysqli_query($con, $insert_note2_sql); 
} 
?> 
+0

他們不需要它,但它不是一個語法錯誤。 –

+0

這不會破壞代碼。 –

+0

謝謝,但我沒有;之前和它不工作,所以我添加它們以查看它是否有所作爲 – user2737457

1

嘗試中都插入使用此語句查詢

$insert_note1_sql = "INSERT INTO notes (recipient, type, title, post, message, date) VALUES('Business 1', '$type', '$title', '$event', '$note1', '$time')"; 

$insert_note2_sql = "INSERT INTO notes (recipient, type, title, post, message, date) VALUES('Business 2', '$type', '$title', '$event', '$note2', '$time')"; ,看看它是否工作正常,那麼...如果它現在工作得很好,那麼就意味着問題這是您的主要密鑰ID