【问题标题】:cant connect sql [php]无法连接 sql [php]
【发布时间】:2018-06-15 23:35:58
【问题描述】:

你能帮我修复我的代码吗? 该网站只需添加数据、编辑和删除。但是,当我尝试填写字段并单击添加时,它会转到一个空白页面。这是我的代码:

<?php  include('server.php'); ?>

<!DOCTYPE html>
<html>
<head>
    <title>CRUD: CReate, Update, Delete PHP MySQL</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

    <form method="post" action="server.php" >
        <div class="input-group">
            <label>Name</label>
            <input type="text" name="name" value="">
        </div>
        <div class="input-group">
            <label>Address</label>
            <input type="text" name="address" value="">
        </div>
        <div class="input-group">
            <button class="btn" type="submit" name="save" >Save</button>
        </div>
    </form>


<?php $results = mysqli_query($db, "SELECT * FROM info"); ?>
    <table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Address</th>
            <th colspan="2">Action</th>
        </tr>
    </thead>

    <?php while ($row = mysqli_fetch_array($results)) { ?>
        <tr>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['address']; ?></td>
            <td>
                <a href="index.php?edit=<?php echo $row['id']; ?>" class="edit_btn" >Edit</a>
            </td>
            <td>
                <a href="server.php?del=<?php echo $row['id']; ?>" class="del_btn">Delete</a>
            </td>
        </tr>
    <?php } ?>
</table>    
</body>
</html>

这是 php 代码。

 <?php 

    $name = "";
    $address = "";
    $id = "";
    $update = false;

    mysql_connect("localhost", "user", "password");
    $db = mysql_select_db("crud");

    if (isset($_POST['save'])) {
        $name = $_POST['name'];
        $address = $_POST['address'];

        mysqli_query($db, "INSERT INTO info (name, address) VALUES ('$name', '$address')"); 
        $_SESSION['message'] = "Address saved"; 
        header('location: index.php');
    }

    $results = mysqli_query($db, "SELECT * FROM info");


?>

当我点击保存按钮时,它完全是空白的。对不起我的英语不好。请帮我解决一下这个。提前谢谢你。

【问题讨论】:

  • 我不明白,一旦你使用mysql_connect 和其他你使用mysqli_query
  • 您的表格状态是否发生了变化?任何行添加/删除?
  • 我已经尝试使用 $db = mysqli_connect('localhost', 'user', password'', 'crud');但仍然得到相同的结果:(
  • 嗨 Shahlin,没有添加任何行。

标签: php html sql phpmyadmin web


【解决方案1】:

检查以下代码并更改您的 server.php 文件。我希望它会起作用。

 <?php 
        $connect=mysqli_connect("localhost", "root", "", "dbname");
        if(!$connect){
            die('Can not connect:'.mysqli_error());
        }
        else{
            echo "Connect";
        }
        $name = "";
        $address = "";
        $id = "";
        $update = false;

        if (isset($_POST['save'])) {
            $name = $_POST['name'];
            $sql=mysqli_query($connect, "INSERT INTO tablename (name) VALUES ('$name')"); 
            $_SESSION['message'] = "Address saved"; 
            header('location: index.php');
        }
    ?>

更改 index.php 文件

<form method="post" action="server.php" >
        <div class="input-group">
            <label>Name</label>
            <input type="text" name="name" value="">
        </div>
        <div class="input-group">
            <label>Address</label>
            <input type="text" name="address" value="">
        </div>
        <div class="input-group">
            <button class="btn" type="submit" name="save" >Save</button>
        </div>
    </form>


    <table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Address</th>
            <th colspan="2">Action</th>
        </tr>
    </thead>

    <?php while($row = mysqli_fetch_array($results)) { ?>
        <tr>
            <td><?php echo $row['columnname1']; ?></td>
            <td><?php echo $row['columnname2']; ?></td>
            <td>
                <a href="index.php?edit=<?php echo $row['yourprimarykeyid']; ?>" class="edit_btn" >Edit</a>
            </td>
            <td>
                <a href="server.php?del=<?php echo $row['yourprimarykeyid']; ?>" class="del_btn">Delete</a>
            </td>
        </tr>
    <?php } ?>
</table>  

【讨论】:

  • 你能把改变的部分粘贴一下吗?
  • 还是一样:(
【解决方案2】:

您需要在button 标签中添加value 属性,否则 if (isset($_POST['save'])) 会失败:

<button class="btn" type="submit" name="save" value="1">Save</button>

【讨论】:

  • 还是一样的:(
  • 尝试在 PHP ini_set('display_errors',1); error_reporting(E_ALL); 中启用错误报告您也可以尝试使用 mysql_connectmysql_query 而不是 mysqli 函数。
【解决方案3】:

如果您的代码包含php代码,则应将其保存为php格式,然后使用wampserver之类的localhost提供程序软件。将文件复制到 c:/wamp64/www/ 目录并运行浏览器并在地址栏中输入 https://localhost/.... 即 ... 是子目录和文件名。 如果双击打开php文件,里面的php代码不会执行。

【讨论】:

  • 欢迎来到 StackOverflow。我不认为这回答了这个问题。您的意思是发表评论吗?
  • 能否请您扩展您的答案,以便更详细地回答 OP 的问题。
猜你喜欢
  • 2016-07-02
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 2017-01-27
  • 2016-11-08
  • 2017-08-03
  • 2013-09-10
  • 2018-09-27
相关资源
最近更新 更多