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>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 Js代码报错问题不知道怎么解决
  • ¥15 gojs 点击按钮node的position位置进行改变,再次点击回到原来的位置
  • ¥15 计算决策面并仿真附上结果
  • ¥20 halcon 图像拼接
  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥100 求ASMedia ASM1184e & ASM1187e 芯片datasheet/规格书
  • ¥15 求解决扩散模型代码问题