简体   繁体   English

我正在尝试通过php代码更新记录。 但是我的代码有麻烦

[英]I am trying to update records by php code. but having trouble in my code

I am trying to update records by php code. 我正在尝试通过php代码更新记录。 but having trouble in my code. 但是我的代码有麻烦。

code is running without using functions (get40(),show()) but i want to use buttons to call these functions. 代码运行时未使用函数(get40(),show()),但我想使用按钮来调用这些函数。 code is give below: 代码如下:

this is main code file: 这是主代码文件:

    <?php
$data;

    $count=0;
    $host='localhost';
    $uname='root';
    $pass="";
    $dbname="testing";
    $db=new mysqli($host,$uname,$pass,$dbname);
    $result;
    $query;


    if (mysqli_connect_errno()) {
    echo 'Error: Could not connect to database. Please try again later.';
    exit;
    }

    function get40($off='0', $rcd='2')
    {
        global $db,$result,$query;
        $query="SELECT sword, tword, reviewed from TempDictionary LIMIT $off,$rcd";
        $result = $db->query($query)or trigger_error($db->error);
    }

    function show()
    {
        global $db,$result,$query;
        echo "<form action='testSave.php' method='POST'>";
        echo "<table border=1 >";
        while($row = mysqli_fetch_array($result)) 
        {
            global $count;
            $count+=1;
            echo "<tr>";
            echo "<td><input type='text' name='s[]' value=" . $row['sword'] . "></td>";
            echo "<td><input type='text' name='t[]' value=" . $row['tword'] . "></td>";
            echo "<td><input type='text' name='r[]' size='1' value=" .$row['reviewed'] . "></td>";
            echo "</tr>";
        }
        echo "</table>";
        echo "<input type='text' name='no' size='2' value=" .$count. ">";
        echo "<input type='submit' value='Save'>";
        echo "</form>";
}
//get40(1,2);
//show();
mysqli_close($db);
?>
<script type="text/javascript"> 
function get()
{
 <?php 
    get40(0,3);
    show();
 ?>
 }
</script>

<form action="" method="POST">
<input type=button name=btnGet value=Get onclick="get()">
</form>

and savetest.php file is: 而savetest.php文件是:

    <?php

$no=$_POST['no'];
$i=0;
$s=$_POST['s']; 
$t=$_POST['t'];
$r=$_POST['r'];

    $host='localhost';
    $uname='root';
    $pass="";
    $dbname="testing";
    $db=new mysqli($host,$uname,$pass,$dbname);

    if (mysqli_connect_errno()) {
    echo 'Error: Could not connect to database. Please try again later.';
    exit;
    }

    // display
    foreach ($s as $key=>$value) 
    {
        print "$key = $value $t[$key] $r[$key]";
        echo '<br>';
    }

    // save to DB
    foreach ($s as $key=>$value) 
    {
    //  $query="UPDATE TempDictionary set sword=".$value.",tword=".$t[$key].",reviewed=".$r[$key]."
    //          WHERE sword=".$value;
        $query="UPDATE TempDictionary set sword='$value',tword='$t[$key]',reviewed='$r[$key]'
                WHERE sword='$value'";              

    $result = $db->query($query)or trigger_error($db->error);

    }

mysqli_close($db);  
?>

the problem is while i click button it does not works. 问题是当我单击按钮时,它不起作用。

AFAIK you are trying to run PHP code with Javascript code. AFAIK,您正在尝试使用Javascript代码运行PHP代码。 Use Jquery/Ajax to solve your problem. 使用Jquery / Ajax解决您的问题。 You can Google for that or look at stackoverflow. 您可以为此搜索Google或查看stackoverflow。

Step 1: Learn how a click on a button can run PHP Run php function inside jQuery click 第1步:了解单击按钮如何运行PHP,并在jQuery click中运行php函数

Step 2: Learn how to use data from a database and populate a form with it: ajax call to populate form fields from database query when select value changes 第2步:了解如何使用数据库中的数据并用它填充表单选择值更改时,ajax调用从数据库查询中填充表单字段

That's the first step in solving your problem. 这是解决问题的第一步。 (sorry it's no copy/paste solution, just a pointer in the right direction) (很抱歉,这不是复制/粘贴的解决方案,只是指向正确方向的指针)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的碰撞代码有问题 - I am having trouble with my collision code 将 HTML 转换为 React 特定代码时遇到问题。 我正在使用`@pqina/flip`? - Having trouble converting HTML to React specific code. I am using `@pqina/flip`? 我的 JavaScript 代码在尝试使用 keydown 事件处理程序更改类中的变量时遇到问题 - I am having trouble in my JavaScript code trying to change a variable in a class with a keydown event hander 我正在尝试优化第一块代码。 为什么我的方法不起作用? 缩短代码的最佳方法是什么? - I am trying to optimize the first chunk of code. Why doesn't my approach work? What's the best approach to shorten the code? 我重复了很多代码。 有没有办法可以重复使用相同的代码而不必再次复制它? - I am repeating a lot of code. Is there a way I can reuse that same code with out having to copy it again? 我正在尝试将Material-UI InfoIcon添加到我的代码中。 但我不知道如何在TextField中实现它 - I am trying to add the Material-UI InfoIcon into my code. But I don't know how to implement it in TextField 我可以从javascript函数获取网址。 我需要使用php代码将相同的URL存储在我的数据库中。 - I am able to get the url from javascript function. I need same url to be stored in my database using php code. 这段代码有问题。 js中的Discord清除命令 - Having trouble with this code. Discord purge command in js 我在使用switch语句时遇到麻烦,特别是使它们反复检查我的代码 - I am having trouble with switch statements specifically making them check my code over and over 我试图在plist版本中读取.strings文件。 我有 <key> 和 <string> 。 我不确定如何在我的JavaScript代码中阅读这些输入。 - I am trying to read a .strings file in plist version. I have <key> and <string>. I am not sure how to read these inputs in my javascript code.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM