使用输入在phpmyadmin中更新表

时间:2014-01-13 15:48:35

标签: php mysql sql

我一直在努力为我的大学建立一个在线门户,工作人员可以提名学生获得特定的职位或奖品。我使用带有css的php用于前端,而phpmyadmin用于后端。但是,一旦我提供输入(regno)来提名学生,它就不会在表格中更新。有人可以帮忙吗?这是我的代码:

<!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>NOMINATE ENTRIES</title>
<meta author="" content="">
<link rel="stylesheet" type="text/css" href="view.css" media="all">
</head>
<body id="main_body" >
    <img id="top" src="top.png" alt="">
    <div id="form_container">
        <h1><a>Nominate Entries</a></h1>
        <form name="form5" class="appnitro"  method="post" action="test.php">
                    <div class="form_description">
            <center><h2>Students Database</h2></center>
            <p><center><font size='3'>

<?php
$con=mysqli_connect("localhost","staff","12345","mop");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM `student` WHERE `Nominated` = 0");

echo "<table border='1'>
<tr>
<th>Register No</th>
<th>Department &nbsp </th>
<th>Name &nbsp &nbsp &nbsp </th>
<th>Class &nbsp </th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['RegNo'] . "</td>";
  echo "<td>" . $row['Name'] . "</td>";
  echo "<td>" . $row['Department'] . "</td>";
  echo "<td>" . $row['Class'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

if(isset($_POST['submit']))
{
$regno = $_POST['regno'];
$reason = $_POST['reason'];
$sql = "UPDATE `mop`.`student` SET `Nominated` = \'1\' WHERE `student`.`RegNo` = 1106103;";}
mysqli_close($con);
?>
</center></font>
            </p>
        </div>  
        <b>Enter Register Number <font color='red'>*</font> </b> <input type="text" id="regno" name="regno"><br>
        <b>Enter Reason <font color='red'>*</font> </b> <input type="text" id="reason" name="reason"><br>
            <ul >
                    <center><li class="buttons">
                <input type="hidden" name="form_id" value="768845" />
                <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" /></center>
        </li>
            </ul>
        </form> 
    </div>
    <img id="bottom" src="bottom.png" alt="">
    </body>
</html>

2 个答案:

答案 0 :(得分:1)

您实际上并没有发送您的查询。

$result = $connection -> query($sql);

// Or, since it is only an update

$connection -> query($sql);

$connection与数据库的连接

答案 1 :(得分:0)

我已经像这先生一样改变了整个文件。感谢所有人的意见,希望与他人分享:

这是我的form.php文件:

<!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>NOMINATE ENTRIES</title>
<meta author="" content="">
<link rel="stylesheet" type="text/css" href="view.css" media="all">
</head>
<body id="main_body" >
    <img id="top" src="top.png" alt="">
    <div id="form_container">
        <h1><a>Nominate Entries</a></h1>
        <form name="form" class="appnitro"  method="post" action="test.php">
                    <div class="form_description">
            <center><h2>Students Database</h2></center>
            <p><center><font size='3'>
            <?php
$con=mysqli_connect("localhost","staff","123456","mop");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM student");

echo "<table border='1'>
<tr>
<th>Register No</th>
<th>Name &nbsp &nbsp &nbsp </th>
<th>Department &nbsp </th>
<th>Class &nbsp </th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['RegNo'] . "</td>";
  echo "<td>" . $row['Name'] . "</td>";
  echo "<td>" . $row['Department'] . "</td>";
  echo "<td>" . $row['Class'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>
</center></font>
            </p>
        </div>  
        <b>Enter Register Number <font color='red'>*</font> </b> <input type="text" name="regno"><p>
        <b>Enter Reason <font color='red'>*</font> </b> <input type="text" name="reason"><p>

                    <ul >
                    <center><li class="buttons">
                <input type="hidden" name="form_id" value="768845" />
                <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" /></center>
        </li>
            </ul>
        </form> 
    </div>
    <img id="bottom" src="bottom.png" alt="">
    </body>
</html>

它指的是test.php文件,这里也是那个文件:

<?php
$con=mysqli_connect("localhost","staff","123456","mop");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$sql1="UPDATE student SET Reason = '$_POST[reason]' WHERE RegNo ='$_POST[regno]'";
if (!mysqli_query($con,$sql1))
  {
  die('Error: ' . mysqli_error($con));
  }
  else
  {
   $sql2="INSERT INTO nominated select * from student where regno = '$_POST[regno]'";
   if (!mysqli_query($con,$sql2))
    {
    die('Error: ' . mysqli_error($con));
    }
    else
    {
    $sql3="DELETE from student where regno = ".intval($_POST["regno"]);
    if (!mysqli_query($con,$sql3))
   {
    die('Error: ' . mysqli_error($con));
   }
  }
 }
header("location:form5_1.php");

mysqli_close($con);
?>