[英]How to store random MySQL query for later use?
$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">   
<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. 如果选择了“新建”按钮,我现在拥有的代码将拉出一个新单词,并将“ a”替换为“ b”。 I want the 'edit' button to take the exact same word just pulled up and replace the 'r''s with 'c''s.
我希望“编辑”按钮使用完全相同的单词,然后将其替换为“ c”。 However, as the code is now, the edit button will either pull up a new word or not pull up anything at all.
但是,就像现在的代码一样,编辑按钮将拉出一个新单词或根本不拉出任何东西。
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.
我不确定如何通过ID存储它,但是如果有帮助的话,所有列都有ID号。 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.
我也知道“ SELECT * FROM words ORDER BY RAND()LIMIT 1”的格式确实不好,但这是我想在解决此问题后解决的一个问题。 Thanks for your help.
谢谢你的帮助。
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.
使用编辑按钮时,您将传递隐藏的输入字段值,该值可用于提取与最近一次使用get new word函数调用提取的记录相同的记录。
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">   
<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. 该ID将随您的帖子一起传递,然后您可以对其进行检查并在选择查询中使用它来获取记录。 Just be sure to be careful with data handling.
只要确保小心处理数据即可。 Consider updating to a current API (mysql~ functions are deprecated) and prepared statements .
考虑更新到当前的API(不建议使用mysql〜函数)和准备好的语句 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.