单选按钮使用我的fetch数组(如何将值插入MySQL?)

时间:2022-09-11 13:46:25
$result = mysql_query("SELECT * 
                       FROM `tblquestion` 
                       WHERE questiontype = 'Methods' ", $connect);

<?php

echo "<br>";
while($row = mysql_fetch_array($result))
{
    echo "<br>";
    echo "<strong>" . $row["questionno"] . ".</strong> " . $row["question"] . "";
    echo "<br>";
    echo "<input type = radio name = ". $row ["questionid"] . "' id = '". $row ["questionid"] . "' value = 5/>5";
    echo "<input type = radio name = ". $row ["questionid"] . "' id = '". $row ["questionid"] . "' value = 4/>4";
    echo "<input type = radio name = ". $row ["questionid"] . "' id = '". $row ["questionid"] . "' value = 3/>3";
    echo "<input type = radio name = ". $row ["questionid"] . "' id = '". $row ["questionid"] . "' value = 2/>2";
    echo "<input type = radio name = ". $row ["questionid"] . "' id = '". $row ["questionid"] . "' value = 1/>1";
    echo "<br>";
    }
    echo "<br>";
?>

Is it possible to insert/post the values with this kind of radio buttons using mysql_fetch_array $row["questionid"]? I'm asking this since I cant really find a way to get the values of each respective $row["questionid"].

是否可以使用mysql_fetch_array $ row [“questionid”]使用这种单选按钮插入/发布值?我问这个,因为我无法找到一种方法来获取每个$ row [“questionid”]的值。

PICTURES:

图片:

Output on PHP

PHP上的输出

Database on phpMyAdmin

phpMyAdmin上的数据库

1 个解决方案

#1


1  

First off, if you're not using the ID of the radio buttons for something specific, remove that attribute. A HTML-element doesn't need an ID unless you need to target that specific element.

首先,如果您没有使用单选按钮的ID,请删除该属性。除非您需要定位该特定元素,否则HTML元素不需要ID。

Secondly, your name attribute is invalid. In your current set up, you will only get a number as the name. Ex: name='1'. Or rather name=1'(you're missing the first ' in your code). Change your code to:

其次,您的名称属性无效。在您当前的设置中,您只会获得一个数字作为名称。例如:名字='1'。或者更确切地说name = 1'(你错过了代码中的第一个')。将您的代码更改为:

name='questions[". $row ["questionid"] . "]'

This will give you a name like name='question[1]'

这会给你一个名字,比如name ='question [1]'

When the page is posted, you can get the values like this:

发布页面时,您可以获得如下值:

foreach($_POST['questions'] as $questionId => $answer) {

    //...insert into your DB.

}

Don't forget to properly escape $questionId and $answer before you use them in your DB-queries.

在DB查询中使用它们之前,不要忘记正确地转义$ questionId和$ answer。

Oh, yes... your values aren't quoted... value=5 should be value='5'

哦,是的......你的价值没有被引用...值= 5应该是值='5'

These are the most obvious issues.

这些是最明显的问题。

...and as others have pointed out, you shouldn't use mysql_* functions since they are deprecated. Use MySQLi and if you get an error, read the PHP-docs on how to use them.

......正如其他人所指出的,你不应该使用mysql_ *函数,因为它们已被弃用。使用MySQLi,如果出现错误,请阅读有关如何使用它们的PHP文档。

#1


1  

First off, if you're not using the ID of the radio buttons for something specific, remove that attribute. A HTML-element doesn't need an ID unless you need to target that specific element.

首先,如果您没有使用单选按钮的ID,请删除该属性。除非您需要定位该特定元素,否则HTML元素不需要ID。

Secondly, your name attribute is invalid. In your current set up, you will only get a number as the name. Ex: name='1'. Or rather name=1'(you're missing the first ' in your code). Change your code to:

其次,您的名称属性无效。在您当前的设置中,您只会获得一个数字作为名称。例如:名字='1'。或者更确切地说name = 1'(你错过了代码中的第一个')。将您的代码更改为:

name='questions[". $row ["questionid"] . "]'

This will give you a name like name='question[1]'

这会给你一个名字,比如name ='question [1]'

When the page is posted, you can get the values like this:

发布页面时,您可以获得如下值:

foreach($_POST['questions'] as $questionId => $answer) {

    //...insert into your DB.

}

Don't forget to properly escape $questionId and $answer before you use them in your DB-queries.

在DB查询中使用它们之前,不要忘记正确地转义$ questionId和$ answer。

Oh, yes... your values aren't quoted... value=5 should be value='5'

哦,是的......你的价值没有被引用...值= 5应该是值='5'

These are the most obvious issues.

这些是最明显的问题。

...and as others have pointed out, you shouldn't use mysql_* functions since they are deprecated. Use MySQLi and if you get an error, read the PHP-docs on how to use them.

......正如其他人所指出的,你不应该使用mysql_ *函数,因为它们已被弃用。使用MySQLi,如果出现错误,请阅读有关如何使用它们的PHP文档。