检查INSERT是否成功

时间:2013-07-17 19:13:44

标签: php mysql mysqli

这里我试图在表course_student中插入值。查询运行成功,但在数据库中插入值后,我没有收到任何警报。

<?php
if(isset($_POST['submit']))
{
$connect =new mysqli("localhost","root","","new");
if(!$connect)
{
    die("Database connection Error".mysql_error());
}

//select database

if(mysqli_connect_errno())
{
    die("Database selection Error".mysqli_connect_error());
}
$course=$_POST['select1'];
$userid=$_SESSION['user_id'];

$sql="INSERT INTO course_student (Courseid,Studentid)VALUES ($_POST[select1],'$userid')";
$result=$connect->query($sql);  
if ($result)
{  ?>
<html>
<body>
<script>
alert("fail");
</script>
</body>
</html>

<?php
}
else
{
?>
<script>
alert("fail");
</script>
<?php
}
}
?>

2 个答案:

答案 0 :(得分:0)

第1天 - 你可以{2}比{2}更清晰 - echo "<script>alert('something')</script>";并检查结果。

答案 1 :(得分:-1)

  $sql="INSERT INTO course_student (Courseid,Studentid)VALUES ('".$_POST[select1]."','$userid')";
  $check = mysql_query($sql);
  if($check)
  {
  //If succeed
  }