无法连接sql [php]

时间:2018-01-06 08:54:12

标签: php html sql phpmyadmin web

您可以帮我修复我的代码吗? 该网站只是添加数据,编辑和删除。但是,当我尝试填写字段并单击“添加”时,它将转到空白页面。这是我的代码:

<?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");


?>

单击“保存”按钮时,它完全空白。对不起我的英文不好。请帮我解决一下这个。提前谢谢。

3 个答案:

答案 0 :(得分: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>  

答案 1 :(得分:0)

您需要在if(isset($_POST['ans'])){ $id = $_POST['id']; $rsp = $_POST['response']; $correctCount = 0; } 标记中添加value属性,否则 button将失败:

if (isset($_POST['save']))

答案 2 :(得分:-1)

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