使用$ id更新内容不起作用

时间:2014-08-25 23:24:49

标签: php html mysql

我很好奇我的问题似乎在哪里......我试图使用$id=$_GET['id']命令来收集id并允许我的sql行更新。谁能帮助我,看看我做错了什么?

这是我的数据库阵容:

||idtest||testing

和当前的更新PHP:

<?php
  include("database_conn_test.php");
?>
<?php
  //select database table
          $sql = "SELECT testing FROM test";
          $queryresult = mysql_query($sql) or die (mysql_error());

        //Request Values
          while ($res = mysql_fetch_array($queryresult)){
            $algemeneVoorwaarden = $res['testing'];
          }
          if(mysql_num_rows($queryresult) <= 0) {
            echo("");
          }

  if(isset($_POST['update'])){
    $id = $_GET['id'];
    $res= mysql_query("SELECT * FROM test WHERE idtest=$id");
    $avID= mysql_fetch_array($res);

    $algemeneVoorwaarden = mysql_real_escape_string($_POST['testing']);

    $updateAV = "UPDATE test SET testing=$algemeneVoorwaarden WHERE idtest=$avID";
    $result = mysql_query($updateAV);
  }
?>

最后但并非最不重要的HTML:

    <div class="tinyMCEWrapper">
  <script src="tinymce/tinymce.min.js"></script>
  <script type="text/javascript">tinymce.init({selector: "textarea"});</script>
  <form method="post" action="test.php?id=<?php echo $id; ?>">
    <textarea name="testing" id="content" class="algemeneAdmin" style="width:100%"><?php echo $algemeneVoorwaarden; ?>
    </textarea>
    <input type="hidden" name="id" value = "<?php echo $id; ?>">
    <button type="submit">Post</button>
  </form>
</div>

更新:我已经找出了发病区域,但没有找到解决方法。我在使用$ id做错了,因为它从未收到过值...

3 个答案:

答案 0 :(得分:1)

您的表单正在使用POST提交。这意味着您的$_GET['id']应为$_POST['id']。或者,您可以将表单从method="post"更改为method="get",然后使用$_GET代替$_POST

答案 1 :(得分:0)

这是使用PHP在SQL数据库中插入/更新数据的代码,我的作业是:

创建与数据库的连接:

$mysqli = new mysqli("localhost", "username", "pass", "database_name");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

然后在你的代码中,当从POST初始化变量标签时,转义字符串:

$Name = $mysqli->real_escape_string($_POST["txtName"]);
$Street = $mysqli->real_escape_string($_POST["txtStreet"]);
$City = $mysqli->real_escape_string($_POST["txtCity"]); 

现在,准备一个SQL代码来插入你的参数:

$input = $mysqli->query("INSERT INTO customer (MembershipID, Name, Street, City, PostCode, Email, Password, DateJoin, Salt) 
    VALUES ('". $MembershipID."','".$Name."','".$Street."','". $City."','". $PostCode."','". $Email."','". $Password."','". $DateJoined."','". $Salt."')");

这是更新声明:

$updateAddress = $mysqli->query("UPDATE customer SET Street = '".$Street."', City = '".$City."', PostCode = '".$PostCode."' WHERE CustomerID = '".$_SESSION['loggedIn']."'");

答案 2 :(得分:0)

我最终想通了。基本上问题是没有链接到直接链接到ID的页面(因此是URL),并且我在向ID提供值时遇到了问题,因为我从未收到过ID。无论如何,解决方案如下所示:

<?php
  include("database_conn_test.php");
?>
<?php
        //select database table
          $sql = "SELECT idtest, testing FROM test";
          $queryresult = mysql_query($sql) or die (mysql_error());

        //Request Values
          while ($res = mysql_fetch_array($queryresult)){
            $algemeneVoorwaarden = $res['testing'];
            $algemeneID = $res['idtest'];

?>
<body> 
    <?php
          echo "<a href='testUpdate.php?id={$res['idtest']}'>Yo</a>";
         }

       if(mysql_num_rows($queryresult) <= 0) {
        echo("");
       }
    ?>
</body>

然后是直接使用ID的其他页面。对不起给您带来了麻烦,但是您的贡献让我得到了答案!非常感谢大家!