插入后没有返回任何东西,但数据已被保存

时间:2014-06-09 02:36:02

标签: javascript php

<?php

$host="localhost";
$user="root";
$password="";
$con=mysqli_connect("localhost","root","","recipe");


 if ($_GET['type'] == "upload") 
{
   $title=$_GET['title'];
   $creator=$_GET['creator'];
   $ingredient=$_GET['ingredient'];
   $serving=$_GET['serving'];
   $note=$_GET['note'];
   $prepare=$_GET['prepare'];

    $insertsql = "INSERT INTO upload (title,creator,ingredient,serving,note,prepare)
           VALUE ('$title','$creator','$ingredient','$serving','$note','$prepare')";


if(mysql_query($insertsql,$db))
           {echo 1;  }
else
           {echo 0;  }


      }

     ?> 

      <script>
    $.ajax({
    type : "get",
    url : "dataconn.php",
    data :              "type=upload&title="+title+"&prepare="+prepare+"&creator="+creator+"&ingredient="+ingredien  t+"&serving="+serving+"&note="+note,
    success : function(data){
        alert(data);


    }
});

 </script>
 </head>
 </html>

当我从JavaScript将变量传递给PHP时,它能够保存在数据库中,但是我需要一些值,比如数据已经成功保存并且会发出警报1或0。

但是一旦我连接到数据库,它就不再警告了。像数据库中的一些错误阻止但仍然可以保存只是没有出现任何警报。如果我删除它然后运行所有

它也不确定警报。

1 个答案:

答案 0 :(得分:1)

更改此行:

if(mysql_query($insertsql,$db))

到这条线;使用mysqli_*扩展名并正确使用$con代替$db而不是if(mysqli_query($con,$insertsql)) 这是一个你没有在任何地方设置的连接变量:

$con=mysqli_connect("localhost","root","","recipe") or die(mysqli_connect_errno());

此外,您应该设置MySQL调用以返回如下错误:

$result = mysqli_query($con,$insertsql) or die(mysqli_connect_errno());

if ($result) {
  echo 1;
}
else {
   echo 0;
}

并改变这一点:

VALUE

此外,您在查询中使用VALUES时应该$insertsql = "INSERT INTO upload (title,creator,ingredient,serving,note,prepare) VALUES ('$title','$creator','$ingredient','$serving','$note','$prepare')";

+ingredien  t+

更不用说在您的JavaScript AJAX代码中+ingredien t+应该是data : "type=upload&title="+title+"&prepare="+prepare+"&creator="+creator+"&ingredient="+ingredient+"&serving="+serving+"&note="+note,

$host="localhost";
$user="root";
$password="";
$con=mysqli_connect("localhost","root","","recipe");

最重要的是,为什么要为MySQL连接设置变量,然后将值放入内联?

foreach

最后,我对你的主MySQL逻辑代码进行了清理。我收录了mysqli_stmt_bind_parammysqli_free_result&amp; mysqli_close并为$_GET值设置// Credentials. $host="localhost"; $user="root"; $password=""; // Connecting, selecting database $con = mysqli_connect($host, $user, $password, 'recipe') or die(mysqli_connect_errno()); if (isset($_GET['type']) && !empty($_GET['type']) && $_GET['type'] == "upload") { // Set a '$_GET' array and roll through each value. $get_array = array('title', 'creator', 'ingredient', 'serving', 'note', 'prepare'); foreach ($get_array as $get_key => $get_value) { $$get_value = isset($_GET[$get_value]) && !empty($_GET[$get_value]) ? $_GET[$get_value] : null; } // Set the query. $insertsql = "INSERT INTO `upload` (`title`, `creator`, `ingredient`, `serving`, `note`, `prepare`)" . " VALUES (?, ?, ?, ?, ?, ?)" ; // Bind the params. mysqli_stmt_bind_param($insertsql, 'ssssss', $title, $creator, $ingredient, $serving, $note, $prepare); // Run the query. $result = mysqli_query($con, $insertsql) or die(mysqli_connect_errno()); if ($result) { echo 1; } else { echo 0; } // Free the result set. mysqli_free_result($result); // Close the connection. mysqli_close($con); } 循环。这应该是有效的:

{{1}}