weixin_33713350 2015-06-24 08:58 采纳率: 0%
浏览 12

在数据库中创建新数据

It's my first time tryng to use AJAX to send data to php, and i think im doing something wrong because i cant see any new data in mysql, here is what i tried.

This script calls ajax by clicking img:

$s .= "
\t<td>";
$canEdit = getPermission('tasks', 'edit', $a['task_id']);
$canViewLog = getPermission('task_log', 'view', $a['task_id']);
if ($canEdit) {
    $s .= ("
\t\t".'<a href="#">'
           . "
\t\t\t".'<img src="./images/icons/tick.png" alt="' . $AppUI->_('Check') 
           . '" border="0" width="12" height="12" onclick="javascript:insertData()" />' . "
\t\t</a>");
}
$s .= "
\t</td>";
$currentTasken=$a['task_id'];
$currentUser=$AppUI->user_id;

This is my ajax function which sends data to php file:

?>
<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>

And this is PHP file which receives data from AJAX:

<?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!");
}

?>
  • 写回答

2条回答 默认 最新

  • weixin_33671935 2015-06-24 09:09
    关注

    Change as below :

    <?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!");
    }
    
    ?>
    

    Also your script to :

        <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.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("currentUser=<?php echo $currentUser; ?>&currentTasken=<?php echo $currentTasken; ?>");
        }
        </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable