0

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

code is running without using functions (get40(),show()) but i want to use buttons to call these functions. 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:

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

1 Answers1

1

AFAIK you are trying to run PHP code with Javascript code. Use Jquery/Ajax to solve your problem. You can Google for that or look at stackoverflow.

Step 1: Learn how a click on a button can run PHP Run php function inside jQuery click

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

That's the first step in solving your problem. (sorry it's no copy/paste solution, just a pointer in the right direction)

Community
  • 1
  • 1
Rick
  • 806
  • 7
  • 15
  • just to add a little to this answer (that is 100% correct): PHP is executed in the server and the javascript/HTML response you are creating is sent to the client (web browser), there the function get40() does NOT exist and has no meaning since the web brownser does not execute PHP code, just outputs the text/html/javascript/css result of said code. – Naryl Jun 16 '14 at 11:12