想要使用更新查询编辑数据库

时间:2014-04-14 14:42:34

标签: mysql sql

我想从数据库更新我的记录,但是我收到错误,记录没有更新。 我的问题:
我正在处理数据库首先我运行选择查询,然后我将数据插入数据库。我的选择查询和插入查询工作,但当我更新我的记录,也想在其他页面编辑我有错误

我的代码

<?php
  $con=mysqli_connect("localhost","root","","new");
  // Check connection
  if (mysqli_connect_errno())
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $result = mysqli_query($con,"SELECT * FROM img");
  echo "<table border='1'>
  <tr>     
    <th>name</th>
    <th>age</th>
    <th>phonenum</th>
    <th>email</th>
    <th>date</th>
  </tr>";

  while($row = mysqli_fetch_array($result))
  {
    echo '<form action=""> ';
      echo '<input type="hidden" name="rec_id" value="'.$row['id'].'" />';
      echo "<tr>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['age'] . "</td>";
        echo "<td>" . $row['phonenum'] . "</td>";
        echo "<td>" . $row['email'] . "</td>";
        echo "<td>" . $row['date'] . "</td>";
        echo "<td><<a href=edit.php?id=$row[id]'>UPDATE</a></td>";
      echo "</tr>";
    echo '</form>';
  }
  echo "</table>";

  mysqli_close($con);    

  $name = $age = $phonenum = $email = $date = $id="";

  if ($_SERVER["REQUEST_METHOD"] == "GET")
  {
    $id = $_GET['id'];
    if (empty($_POST["name"]))
    {
      $nameErr = "Name is required";
    }
    else
    {
      $name = ($_POST["name"]);
    }

    if (empty($_POST["age"]))
    {
      $emailErr = "Age is required";
    }
    else
    {
      $email = ($_POST["phonenum"]);
    }

    if (empty($_POST["email"]))
    {
      $password = "";
    }
    else
    {
      $password = ($_POST["password"]);
    }

    if (empty($_POST["date"]))
    {
      $phone = "";
    }
    else
    {
      $phone =($_POST["date"]);
    }

    // $name =  $age = $phonenum = $email = $date = "";
  }
  $con=mysqli_connect("localhost","root","","new");
  // Check connection
  if (mysqli_connect_errno())
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $sql = "UPDATE img SET name = '".$name."', age = '".$age."', phonenum = '".$phonenum."', email = '".$email."'
  WHERE id = ".$id;
  if (!mysqli_query($con,$sql))
  {
    die('Error: ' . mysqli_error($con));
  }

  mysqli_close($con);
  ?>

  <div style="clear:both">
    <form action="manage.php" method="post" style="margin-left:502px">
      <table>
        <tr>
          <td>Name</td>
          <td><input type="text" name="name"  autocomplete="off"/></td>
        </tr> 
        <tr>
          <td>Age</td>
          <td><input type="text" name="age"  /></td>
        </tr>
        <tr>
          <td>Phone No</td>
          <td><input type="text" name="phonenum"  /></td>
        </tr>
          <td>E-mail</td>
          <td><input type="text" name="email"  /></td>
        </tr>
        <tr>
          <td>Date</td>
          <td><input type="text" name="date"  /></td>
        </tr>
        <tr>
          <td><input type="submit" name="submit" value="Submit"  /></td>
        </tr>
      </table>
    </form>
  </div>

1 个答案:

答案 0 :(得分:1)

我想我知道你在做什么:

你正在这里发帖:

<form action="manage.php" method="post" style="margin-left:502px">

到同一页面。

你正试图从get方法中获取值:

if ($_SERVER["REQUEST_METHOD"] == "GET")

你必须使用:

if ($_SERVER["REQUEST_METHOD"] == "POST")

获取提交的值。

你的代码中有很多不好的做法。

相关问题