2013-09-24 39 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