简体   繁体   English

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

[英]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">&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. 如果选择了“新建”按钮,我现在拥有的代码将拉出一个新单词,并将“ 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">&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. 该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.

相关问题 如何在 web 页面中存储 MySQL 值以供以后使用 - How to store a MySQL value in a web page for later use 查询mysql表中具有相同月份值的行,并存储为php数组,以备后用在日历上显示它们 - query a mysql table for rows that have the same month value and store as a php array for later use displaying them on a calendar 如何存储来自$ _REQUEST的参数以供以后使用 - How to store parameters from $_REQUEST for later use 如何使用mysql查询的结果 - 例如存储在另一个表中 - How to use results of a mysql query - eg store in another table 存储用户名以供以后使用 - store username to use in a later form 如何在数据库中存储PHP条件文件以供以后使用? - How can I store a PHP conditional staement in the database for later use? PHP-如何在数组中存储数组键并在以后使用它访问值 - PHP - How to store an arrays keys in an array and use it to later access the value 如何在php中的会话变量中存储数组值并在以后使用它 - how to store array values in session variable in php and use it for later 如何将查询结果存储到mysql中的变量中,然后使用另一个查询和回显结果? - How to store a query result into a variable in mysql and then use it another query and echo result? 将mysql结果存储在SESSION数组中以供以后使用(通过in_array检查) - store mysql results in SESSION array for later use (to check via in_array)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM