简体   繁体   English

使用PHP从SQL数据库表中打印值

[英]Print values from an SQL database table with PHP

I am new at PHP/SQL so bear with me if I say something that's obvious or downright wrong. 我是PHP / SQL的新手,所以如果我说出明显的或完全错误的话,请多多包涵。
I have a SQL table (in a database) and I need to take 3 random values (name, race, year), each from a different field in the table, and print it on a website with php. 我有一个SQL表(在数据库中),我需要获取3个随机值(名称,种族,年份),每个取自表中的不同字段,并使用php将其打印在网站上。 The value requirement "race" will be different depending on radiobuttons (lets say RB1, and RB2) and this all has to happen when 根据单选按钮(让我们说RB1和RB2)的不同,值要求“竞赛”将有所不同,而所有这些都必须在

<input type="submit" name="button"> is clicked. 单击<input type="submit" name="button">

<html>  <input type="radio" name="RB1"> Asian  </html>

What could I do in this situation? 在这种情况下我该怎么办?

Example: So if RB1 is selected and the button is clicked I will need to randomly print a "name" with the corresponding "year" (of the name) and it should have a corresponding race of RB1 (which is Asian) 示例:因此,如果选择了RB1并单击了按钮,我将需要随机打印一个带有相应“年”(名称)的“名称”,并且它应具有一个对应的RB1种族(亚洲)。

I think this code will help you to understand this- 我认为这段代码将帮助您理解这一点-

DB thing, you have to take care for that..im giving you an idea to handle this sort of stuff. DB的事情,你必须要小心..im给你一个想法来处理这种事情。

Create one test.php file(this what i did) 创建一个test.php文件(这是我所做的)

Place this code -- 放置此代码-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("input:radio[name=race]").change(function(){
$.ajax({
type : 'GET',
url : 'getdata.php',
data: {race : $('input:radio[name=race]:checked').val()},
success: function(data){
$('#showdata').html(data);
},
});
});
});
</script>
</head>

<body>

<form action="#" method="post" name="myform">
<input type="radio" name="race" value="RB1" /> Asian
<input type="radio" name="race" value="RB2" /> European 
</form>

<div id="showdata"></div>

</body>
</html>

In getdata.php file paste this content - 在getdata.php文件中粘贴以下内容-

<?php

$myvalue = $_GET['race'];

if( $myvalue == 'RB1')
{
echo "I am RB1";
}
if( $myvalue == 'RB2')
{
echo "I am RB2";
}

Run the code in browser now (test.php). 立即在浏览器中运行代码(test.php)。

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

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