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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵