使用php中的单选按钮从数据库中检索值,并仅显示选择的字段名称而不是其他名称

时间:2014-07-23 21:14:47

标签: php mysql

我认为这个问题的答案可以很简单,但我无法得到答案的基本想法。

上下文:

我通过单选按钮制作了一个简单的预订电影票的页面,能够存储和检索价值,并在检索价值后。

我在该值中添加了单选按钮,然后尝试在其他页面中检索单个字段的值,但是我只显示一个显示的值而不是其他值。

仅显示一个表中第一行的值,并且无关紧要选择哪个值。

标记:

<html>
<head></head>
<body bgcolor= cyan>
<form method="post" action="sqlmovie1.php">
<table cellpadding="20" cellspacing="40" bgcolor="red"width="100%"><center>
<tr>
<td> <center><font size=+5 color="MAGENTA">BOOK TICKET </FONT><center>
</td></tr></center>
</table>
<p>&nbsp;</p>
<center>
<table border="1" cellspacing="10" cellpadding="5">
<tr><td>

audi1: <input type = "radio" name="audi" value="1">

audi2: <input type = "radio" name="audi" value="2">

audi3: <input type = "radio" name="audi" value="3">
</td>
</tr>
<tr><td><CENTER>
<input type="submit" value ="submit"></CENTER></td>
</tr>
</table>

</center>
</table>
</form>
</html>
</body>

php代码是sqlmovie1.php

<?php
$cxn = mysqli_connect("localhost","root","","movie");
if(mysqli_connect_errno())
{
    echo "invalid login :",$mysqli_connect_error();
}

$name = $_POST['audi'];
echo "<html> <body> 
<table border='1'>
<tr><td> movie name </td>
<td> timing</td>
<td> ticket price </td>
<td> book movie </td> </tr>";

if(isset($name) && ($name =="1"))
{
    $sql="select * from  movie1";
    $result=mysqli_query($cxn,$sql);
    $column_count = mysqli_num_fields($result);

    if(mysqli_num_rows($result)!="")
    {
        while($row=mysqli_fetch_array($result))
        {
            echo("<tr>");
            for ($column_num = 0;$column_num < $column_count;$column_num++)
                echo "<TD>$row[$column_num]</TD>\n";
                echo "<td><form method='post' action ='moviebooked1.php'>  <input type ='radio' name = 'book1' value='1'></td><td><input type = 'submit' name = 'submit' value = 'book show'></form></td>";
                echo("</TR>\n");
         }    
    }

//echo "<form method='post' action='moviebooked1.php'>  <input type ='radio' name = 'book1' value='1'><input type='submit' name='submit' value= 'book show'></form>";
}
elseif(isset($name) && ($name=="2"))
{
    $sql="select * from  movie2";
    $result=mysqli_query($cxn,$sql);
    $column_count = mysqli_num_fields($result);
    if(mysqli_num_rows($result)!="")
    {
        while($row=mysqli_fetch_array($result))
        {
            echo("<TR ALIGN=LEFT VALIGN=TOP>");
            for ($column_num = 0;$column_num < $column_count;$column_num++)
                echo("<TD>$row[$column_num]</TD>\n");
                echo "<td><form method='post' action ='moviebooked1.php'>  <input type ='radio' name = 'book2' value='2'></td><td><input type = 'submit' name = 'submit' value ='book show'></form></td>";
            echo("</TR>\n");
        }
    }
}
elseif(isset($name) && ($name=="3"))
{
    $sql="select * from  movie3";
    $result=mysqli_query($cxn,$sql);
    $column_count = mysqli_num_fields($result);
    if(mysqli_num_rows($result)!="")
    {
        while($row=mysqli_fetch_array($result))
        {
            echo("<TR ALIGN=LEFT VALIGN=TOP>");
            for ($column_num = 0;$column_num < $column_count;$column_num++)
                echo("<TD>$row[$column_num]</TD>\n");
                echo "<td><form method='post' action ='moviebooked1.php'>  <input type ='radio' name = 'book3' value='3'></td><td><input type = 'submit' name = 'submit' value = 'book show'></form></td>";
                echo("</TR>\n");

        }
    }
}
echo "</table>";

?>

我在表movie1 movie2movie3中应用了不同的方法,但没有任何新内容,问题就在于此处和代码之上。

movie1

我存储了3行moviename, movietimingticketprice

值为row1 is kick 11.00am 150row2 is hulk 12.00pm 150 row3 is fastandfurious 1pm 150

如果我尝试回复我预订的电影的名称,它没有给我正确的电影名称,它只显示所有表格中第一部电影的名称。

我通过单选按钮预订。如何获得我选择的正确电影名称

预先提供帮助的大问题,问题非常小,但问题很大。

这是我的上一个代码moviebooked1.php

<?php
$cxn=mysqli_connect("localhost","root","","movie");
if(mysqli_connect_errno())
{
echo "invalid login :",$mysqli_connect_error();
}
if (isset($_POST['book1']))
{
$sql="select moviename from movie1";
$result=mysqli_query($cxn,$sql);
if($row=mysqli_fetch_array($result))
{
foreach($row as $value['moviename'])
{
echo " your show for ,$row['moviename'],"has been booked";}

}}

elseif (isset($_POST['book2']))
{
$sql="select moviename from movie2";
$result=mysqli_query($cxn,$sql);
if($row=mysqli_fetch_row($result))
{
echo "your ticket for",  $row['moviename'], "movie has been booked";
echo "<br>";
}
if (isset($_POST['book3']))
{
$sql="select moviename from movie3";
$result=mysqli_query($cxn,$sql);
if($row=mysqli_fetch_array($result))
{
echo "your ticket for",  $row['moviename'], "movie has been booked";
echo "<br>";
}
?>

0 个答案:

没有答案