0

我想做的是:

将数据插入具有两列并在同一 php 页面中显示更新值的表中。我能够获取数据并显示它,但无法插入任何数据。请指导我。

文件名为 mypage.php

到目前为止我的代码:

<?php
$con=mysqli_connect("localhost","test","test","test");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to mysqli: " . mysqli_connect_error();
  }
  else
  {

    if(isset($_POST['url']) && isset($_POST['desc'])) 
    {
        $url=$_POST['url'];
        $desc=$_POST['desc'];
        $sql=   "INSERT INTO urldesc ".
                "(url,desc) ".
                "VALUES ('".$url."', '".$desc."')";
        echo $sql;
        echo $url;
        echo $desc;
        mysqli_query($con, $sql);
        //mysqli_query($con,"INSERT INTO urldesc (url, desc) VALUES ('". $_POST['url'] ."', '". $_POST['desc'] ."')");
    }
  }

?>

<html>
    <body>
    <form action="mypage.php" method='post'>
        <input type="text" name='url' />
        <input type="text" name='desc' />
        <input type="submit" />
    </form>
        <table border=1>
            <th>URL</th>
            <th>Description</th>
            <?php
            $values=mysqli_query($con,"select * from urldesc");
            if(! $values )
            {
              die('Could not get data: ' . mysqli_error());
            }
            while($row = mysqli_fetch_array($values))
            {
                echo "<tr><td width='200px'><center>".$row['url']."</center></td><td width='600px'><center>".$row['desc']."</center></td></tr>";
            } 
            mysqli_close($con);
            ?>  
        </table>
    </body>
</html>
4

2 回答 2

0

更改以下语句

 $sql=   "INSERT INTO urldesc ".
            "(url,desc) ".
            "VALUES ('".$url."', '".$desc."')";

 $sql=   "INSERT INTO urldesc ".
            "(`url`,`desc`) ".
            "VALUES ('".$url."', '".$desc."')";
于 2013-09-28T06:31:49.007 回答
0

您给定的参数mysqli_qurey格式错误。你使用mysqli_query($con, $sql),但它应该是mysqli_query($sql, $con)

<?php
$con=mysqli_connect("localhost","test","test","test");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to mysqli: " . mysqli_connect_error();
  }
  else
  {

    if(isset($_POST['url']) && isset($_POST['desc'])) 
    {
        $url=$_POST['url'];
        $desc=$_POST['desc'];
        $sql=   "INSERT INTO `urldesc` ".
                "(`url`,`desc`) ".
                "VALUES ('".$url."', '".$desc."')";
        echo $sql;
        echo $url;
        echo $desc;
        mysqli_query($sql, $con);
        //mysqli_query($con,"INSERT INTO urldesc (url, desc) VALUES ('". $_POST['url'] ."', '". $_POST['desc'] ."')");
    }
  }

?>
于 2013-09-28T06:32:53.677 回答