Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在数据库中创建新数据_Php_Mysql_Ajax - Fatal编程技术网

Php 在数据库中创建新数据

Php 在数据库中创建新数据,php,mysql,ajax,Php,Mysql,Ajax,这是我第一次尝试使用AJAX将数据发送到php,我认为我做错了什么,因为我在mysql中看不到任何新数据,下面是我尝试的 此脚本通过单击img调用ajax: $s .= "\n\t<td>"; $canEdit = getPermission('tasks', 'edit', $a['task_id']); $canViewLog = getPermission('task_log', 'view', $a['task_id']); if ($canEdit) { $s .=

这是我第一次尝试使用AJAX将数据发送到php,我认为我做错了什么,因为我在mysql中看不到任何新数据,下面是我尝试的

此脚本通过单击img调用ajax:

$s .= "\n\t<td>";
$canEdit = getPermission('tasks', 'edit', $a['task_id']);
$canViewLog = getPermission('task_log', 'view', $a['task_id']);
if ($canEdit) {
    $s .= ("\n\t\t".'<a href="#">'
           . "\n\t\t\t".'<img src="./images/icons/tick.png" alt="' . $AppUI->_('Check') 
           . '" border="0" width="12" height="12" onclick="javascript:insertData()" />' . "\n\t\t</a>");
}
$s .= "\n\t</td>";
$currentTasken=$a['task_id'];
$currentUser=$AppUI->user_id;
$s.=“\n\t”;
$canEdit=getPermission('tasks','edit',$a['task_id']);
$canViewLog=getPermission('task_log','view',$a['task_id']);
如果($canEdit){
$s.=(“\n\t\t”。”);
}
$s.=“\n\t”;
$currentTasken=$a['task_id'];
$currentUser=$AppUI->user\u id;
这是我的ajax函数,它将数据发送到php文件:

?>
<script type="text/javascript">

    function insertData()
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("POST","datafile.php",true);
xmlhttp.send("<?php echo $currentUser; ?>");
xmlhttp.send("<?php echo $currentTasken; ?>")
    }

</script>
?>
函数insertData()
{
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“POST”,“datafile.php”,true);
xmlhttp.send(“”);
xmlhttp.send(“”)
}
这是从AJAX接收数据的PHP文件:

<?php
$currentUser = $_POST['$currentUser'];
$currentTasken = $_POST['$currentTasken'];
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
if(!$con)
    die('Could not connectzzz: ' . mysql_error());
mysql_select_db("foxi" , $con) or die ("could not load the database" . mysql_error());

$check = mysql_query("SELECT * FROM dotp_task_log");
$numrows = mysql_num_rows($check);
if($numrows == 0)
{
    $pass = md5($pass);

        $ins = mysql_query("INSERT INTO dotp_task_log (`task_log_creator`,`task_log_Task`) VALUES ('$currentUser' , '$currentTasken')" ) ;

   if($ins)
        die("Succesfully Created Log!");

    else
        die("ERROR");

}
else
{
    die("Log already exists!");
}

?>
更改如下:

<?php
$currentUser = $_POST['currentUser'];
$currentTasken = $_POST['currentTasken'];
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
if(!$con)
    die('Could not connectzzz: ' . mysql_error());
mysql_select_db("foxi" , $con) or die ("could not load the database" . mysql_error());

$check = mysql_query("SELECT * FROM dotp_task_log");
$numrows = mysql_num_rows($check);
if($numrows == 0)
{
    $pass = md5($pass);

        $ins = mysql_query("INSERT INTO dotp_task_log (`task_log_creator`,`task_log_Task`) VALUES ('$currentUser' , '$currentTasken')" ) ;

   if($ins)
        die("Succesfully Created Log!");

    else
        die("ERROR");

}
else
{
    die("Log already exists!");
}

?>

使用ajax需要不同的思维方式。我将向您介绍一些概念(如果您还没有),这些概念不仅可以帮助您解决这个问题,还可以帮助您解决未来的问题并节省时间:

  • 在编写服务器端和客户端逻辑之后,您必须分别对其进行测试。因此,首先测试服务器端(php部分):如何?制作一个简单的静态表单,仅用于将数据发布到服务器端逻辑,以测试它是否正确地将数据保存在数据库中,并返回导出的数据

  • 在测试服务器端后,您可以确定如果应用程序不工作,问题肯定出在客户端。因此,我们需要测试它。您是否已经知道如何使用浏览器控制台?在Firefox中,MacOS是[Command]+[Option]+[k],在Windows中应该是[ctrl]+[shift]+[k]它将在浏览器底部打开,您应该标记[network]以记录网络请求,如果您的客户端正在工作,您可以通过ajax查看您正在请求的页面以及http状态代码(应为200)

  • 如果客户端能够通过ajax调用服务器端,并且您确信这样做是因为您在浏览器控制台上看到post请求,但仍然没有在数据库中正确保存数据,那么可能是因为数据正在发送,或者不是通过ajax发送。您也可以在浏览器的日志上进行测试,单击页面使用右键在日志中拥有并标记该选项,可能会用英语写为:“存储请求/响应内容”(我的浏览器是葡萄牙语).标记此选项后,再次发出请求,并用左键单击请求的页面。您将看到通过ajax发送到服务器端的所有标题和数据。因此,请检查是否没有问题

<>强>这是.< /强>但是,考虑使用jQuery,这并不难理解,Ajax的工作方式更简单。
var request = $.ajax({
  url: "script.php",
  method: "POST",
  data:$('#form-id').serialize(), // gets all form fields serialized creating a string fieldName1=value1&fieldName2=value2...
  dataType: "html"
}).done(function( msg ) { // after it's done
  $( "#log" ).html( msg );
  alert(msg)
}).fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});
顺便说一下,如果您必须在html的任何部分(包括javascript部分)上回显一些php值,就像在中所做的那样:

xmlhttp.send("$currentUser");
xmlhttp.send("$currentTasken");
要获取变量值,应将其作为以下内容进行回显:

xmlhttp.send("<?=$currentUser?>");
xmlhttp.send("<?=$currentTasken?>");
xmlhttp.send(“”);
xmlhttp.send(“”);

祝你好运!

你有什么错误吗?不,只要按一下那个img,什么都不会发生…$currentUser=$\u POST['currentUser'];$currentTasken=$\u POST['currentTasken'];//去掉$_POST传递的$in变量,也在php中回显$currentUser以查看您是否有正确的POST值。更改后,以下是firebug的结果:错误失败xmlhttp.send($currentTasken),因此您的变量未定义,如果您有它,则不包括在“”更改中,以下是firebug的结果:错误失败xmlhttp.send('$currentTasken“)脚本在同一个php文件中。对吗?不,我认为不在同一个文件中,因为他使用Ajax发送帖子。我建议他发出警报($currentTasken),看看他是否真的定义了该变量。@TorresAlGrande:脚本是否在php文件中?ReferenceError:$currentUser未定义xmlhttp.send($currentUser);Thanx在开始编写ajax代码之前,我会一直这样做
xmlhttp.send("<?=$currentUser?>");
xmlhttp.send("<?=$currentTasken?>");