简体   繁体   English

通过php中的单选按钮onclick处理程序更新mysql中的标志

[英]updating a flag in mysql via radiobutton onclick handler in php

I want to update a column in a table in mysql. 我想更新mysql表中的列。 Basically the column is the flag for the entries of that db table. 基本上,该列是该数据库表条目的标志。

The modification of the column is resetting all values to 0 and setting the desired row to 1, for this reason I have post.php file which looks like 列的修改是将所有值重置为0并将所需的行设置为1,因此我拥有了post.php文件,看起来像

<?php
require_once('class.uuid.php');

$connection = mysql_connect("---logindetailshere---");
$db = mysql_select_db("---dbnamehere---",$connection);

switch($_REQUEST['action']){
    case ...
        break;
    case ...
            break;
    case 'changeDisp':
        changeDisp($_REQUEST['uid']);
        break;
}

mysql_close($connection);

...

function changeDisp($uid){
    global $connection, $db;
    $q_string = "UPDATE Questions SET Displayed = 0";
    $query = mysql_query($q_string,$connection) or die( sendError(mysql_error() . '<br/><br/>' . $q_string) );
    $q_string = "UPDATE Questions SET Displayed = 1 WHERE Uid='${uid}'";
    $query = mysql_query($q_string,$connection) or die( sendError(mysql_error() . '<br/><br/>' . $q_string) );
}

?>

on the webpage I display the items and radiobuttons next to the items, the purpose is to select the radiobuttons and post to set the flag 1 for the selected item, for this reason I have a item.php file 在网页上,我显示项目和项目旁边的单选按钮,目的是选择单选按钮并发布以为所选项目设置标志1,因此,我有一个item.php文件

    <?php
        $i = 1;
        foreach ($qitem as &$q) {
            $options = explode(";", $q["Options"]);
            $displayed = '';
            if ($q["Displayed"] == 1) { $displayed='checked="yes"'; }

            echo("<div class='item' name='".$q["iUid"]."'>");
            echo("<div class='count'>".$i.".</div>");
            echo ("<div class='radio'><input type='radio' onclick='changeDisp(&#34;".$q["Uid"]."&#34;)' name='disp' ".$displayed."></div>");
            echo("<div class='left'>");
            echo("<h4>".$q["Value"]."</h4>");
            echo("<div class='details'>Typ: ".$q["Type"]."</div>");
            echo("<div class='details'>Skala: ".$options[0]." / ".$options[1]."</div>");
            echo("</div>");
            echo("</div>");
            $i++;
        }   
    ?>

here I am using radiobuttons to select the related item, I checked the unique id values using firebug the values are fine, I just want to click on any radiobutton and want to trigger the onclick=changeDisp() function. 在这里,我使用单选按钮选择相关项,我使用Firebug检查了唯一ID值,这些值都很好,我只想单击任何onclick=changeDisp()按钮,并想要触发onclick=changeDisp()函数。

I have no idea why the page doesn't reload itself and change the selected flag to 1. Could you please help me to solve this problem? 我不知道为什么页面不重新加载自身并将所选标志更改为1。请您帮我解决这个问题吗?

Thanks in advance. 提前致谢。

You cannot use an onclick function to call php function without going there with a javascript, jQuery or ajax call. 您必须先通过javascript,jQuery或ajax调用才能使用onclick函数调用php函数。 You could create an ajax script to call the post.php From the item.php page and return the results to you. 你可以创建一个AJAX脚本调用post.php中item.php页面,并将结果返回给你。

Here is an example of creating the function you want. 这是创建所需功能的示例。 This assumes that $uid is coming from a radio button and not an actual user input. 这假定$uid来自单选按钮,而不是实际的用户输入。 If the user can directly input something you need to use a prepared statment 如果用户可以直接输入某些内容,则需要使用准备好的陈述

 function changeDisp($uid)
{
$Mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
    if ($Mysqli->connect_errno)
    {
        echo "Failed to connect to MySQL: (" . $Mysqli->connect_errno . ") " . $Mysqli->connect_error;
        $Mysqli->close();
    }           
    $query = "UPDATE Questions SET Displayed = 1 WHERE Uid='".$uid."'";     
    $update = $Mysqli->query($query);
    if($update)
    {
        return true;
    }
    return false;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM