如何存储随机MySQL查询供以后使用?

时间:2021-08-31 06:17:03
$user_name = "xxx";
$password = "xxx";
$database = "xxx";
$server = "xxx";

$db_handle = mysql_connect($server, $user_name, $password);

$db_found = mysql_select_db($database, $db_handle);

$randWordDesc = mysql_query("SELECT * FROM words ORDER BY RAND() LIMIT 1");
$db_field = mysql_fetch_assoc($randWordDesc);

if(isset($_POST['new'])){

$unaltered = $db_field['word'];

$noA = str_replace("a", "b", $unaltered);


}

if (isset($_POST['edit'])){

global $noA;
$noR = str_replace("r", "c", $unaltered);


}

?>


<form action="http://xxx" method="post" ><br><br>
<input type="submit" value="edit current" name = "edit" id="gobutton">&nbsp&nbsp&nbsp
<input type="submit" value="new" name = "new" id="gobutton">
</form>

The code I have now will pull up a new word if the 'new' button is selected and replace the 'a''s with 'b''s. I want the 'edit' button to take the exact same word just pulled up and replace the 'r''s with 'c''s. However, as the code is now, the edit button will either pull up a new word or not pull up anything at all.

如果选中“新”按钮,我现在的代码将会弹出一个新单词,并将'a'替换为'b'。我希望“编辑”按钮取出刚刚拉出的完全相同的单词,并将'r'替换为'c'。但是,正如代码现在一样,编辑按钮将拉出一个新单词或者根本不拉出任何内容。

How could I get the edit button to edit the query that the 'new' button pulled up? I'm not sure how I could store it by the id, but all the columns have an id number, if that helps. Also I know that "SELECT * FROM words ORDER BY RAND() LIMIT 1" is really bad form, but that's an issue I want to tackle after this problem. Thanks for your help.

如何让编辑按钮编辑“新”按钮拉出的查询?我不确定如何通过id存储它,但所有列都有一个id号,如果有帮助的话。另外我知道“SELECT * FROM words ORDER BY RAND()LIMIT 1”是非常糟糕的形式,但这是我想在这个问题后解决的问题。谢谢你的帮助。

1 个解决方案

#1


0  

As the first query returns only one result, you could take the primary key column data of the resulting row and store it in a hidden input element on the page. When you use the edit button, you would then pass the hidden input field value which can be used to pull the same record as you pulled with the most recent get new word function call.

由于第一个查询只返回一个结果,因此您可以获取结果行的主键列数据并将其存储在页面上的隐藏输入元素中。当您使用编辑按钮时,您将传递隐藏的输入字段值,该值可用于拉出与最近获取新单词函数调用时相同的记录。

Consider something like the following:

考虑以下内容:

<form action="http://xxx" method="post" ><br><br>
<input type="hidden" value=<?php echo $db_field['primary_key']; ?> name="record_id">
<input type="submit" value="edit current" name = "edit" id="gobutton">&nbsp&nbsp&nbsp
<input type="submit" value="new" name = "new" id="gobutton">
</form>

The id will be passed with your post, which you can then check for and use in a select query to get the record. Just be sure to be careful with data handling. Consider updating to a current API (mysql~ functions are deprecated) and prepared statements.

id将与您的帖子一起传递,然后您可以检查并在选择查询中使用以获取记录。请务必小心数据处理。考虑更新到当前的API(不推荐使用mysql函数)和准备好的语句。

#1


0  

As the first query returns only one result, you could take the primary key column data of the resulting row and store it in a hidden input element on the page. When you use the edit button, you would then pass the hidden input field value which can be used to pull the same record as you pulled with the most recent get new word function call.

由于第一个查询只返回一个结果,因此您可以获取结果行的主键列数据并将其存储在页面上的隐藏输入元素中。当您使用编辑按钮时,您将传递隐藏的输入字段值,该值可用于拉出与最近获取新单词函数调用时相同的记录。

Consider something like the following:

考虑以下内容:

<form action="http://xxx" method="post" ><br><br>
<input type="hidden" value=<?php echo $db_field['primary_key']; ?> name="record_id">
<input type="submit" value="edit current" name = "edit" id="gobutton">&nbsp&nbsp&nbsp
<input type="submit" value="new" name = "new" id="gobutton">
</form>

The id will be passed with your post, which you can then check for and use in a select query to get the record. Just be sure to be careful with data handling. Consider updating to a current API (mysql~ functions are deprecated) and prepared statements.

id将与您的帖子一起传递,然后您可以检查并在选择查询中使用以获取记录。请务必小心数据处理。考虑更新到当前的API(不推荐使用mysql函数)和准备好的语句。