繁体   English   中英

添加编辑删除表单不添加到MySQL中没有错误

[英]Add Edit Delete form not adding into MySQL no errors

我有此“添加编辑删除”表单,问题是:当我放置所有内容并单击“添加”时,它显示“数据添加成功”。 但是数据不在我的phpAdmin表中,并且也未显示在页面中……或者仅仅是因为我的托管服务商不支持MySQLi,而是支持MySQL? 不用说SQL注入是因为我不是那么熟练,也不知道如何保护它,所以该页面将使用登录区域进行保护,因此只有受限制的成员才能访问它。

index.php

<?php
//including the database connection file
include_once("config.php");

//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT * FROM `user` ORDER BY id DESC"); // using mysqli_query instead
?>

<html>
<head>  
    <title>Homepage</title>
</head>

<body>
<a href="add.html">Add New Data</a><br/><br/>

    <table width='80%' border=0>

    <tr bgcolor='#CCCCCC'>
        <td>Steam Username</td>
        <td>Steam Password</td>
        <td>Steam Guard Code</td>
        <td>Update</td>
    </tr>
    <?php 
    //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array 
    while($res = mysqli_fetch_array($result)) {         
        echo "<tr>";
        echo "<td>".$res['steamUE']."</td>";
        echo "<td>".$res['steamPW']."</td>";
        echo "<td>".$res['steamGC']."</td>";    
        echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";       
    }
    ?>
    </table>
</body>
</html>

add.html

<html>
<head>
    <title>Add Data</title>
</head>

<body>
    <a href="index.php">Home</a>
    <br/><br/>

    <form action="add.php" method="post" name="form1">
        <table width="25%" border="0">
            <tr> 
                <td>Steam Username</td>
                <td><input type="text" name="steamUE"></td>
            </tr>
            <tr> 
                <td>Steam Password</td>
                <td><input type="text" name="steamPW"></td>
            </tr>
            <tr> 
                <td>Steam Guard Code</td>
                <td><input type="text" name="steamGC"></td>
            </tr>
            <tr> 
                <td></td>
                <td><input type="submit" name="Submit" value="Add"></td>
            </tr>
        </table>
    </form>
</body>
</html>

edit.php

<?php
// including the database connection file
include_once("config.php");

if(isset($_POST['update']))
{   

    $id = mysqli_real_escape_string($mysqli, $_POST['id']);

    $steamUE = mysqli_real_escape_string($mysqli, $_POST['steamUE']);
    $steamPW = mysqli_real_escape_string($mysqli, $_POST['steamPW']);
    $steamGC = mysqli_real_escape_string($mysqli, $_POST['steamGC']);   

    // checking empty fields
    if(empty($steamUE) || empty($steamPW) || empty($steamGC)) { 

        if(empty($steamUE)) {
            echo "<font color='red'>Steam Username field is empty.</font><br/>";
        }

        if(empty($steamPW)) {
            echo "<font color='red'>Steam Password field is empty.</font><br/>";
        }

        if(empty($steamGC)) {
            echo "<font color='red'>Steam Guard Code field is empty.</font><br/>";
        }       
    } else {    
        //updating the table
        $result = mysqli_query($mysqli, "UPDATE `user` SET steamUE='$steamUE',steamPW='$steamPW',steamGC='$steamGC' WHERE id='$id'");

        //redirectig to the display page. In our case, it is index.php
        header("Location: index.php");
    }
}
?>
<?php
//getting id from url
$id = $_GET['id'];

//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM `user` WHERE id='$id'");

while($res = mysqli_fetch_array($result))
{
    $steamUE = $res['steamUE'];
    $steamPW = $res['steamPW'];
    $steamGC = $res['steamGC'];
}
?>
<html>
<head>  
    <title>Edit Data</title>
</head>

<body>
    <a href="index.php">Home</a>
    <br/><br/>

    <form name="form1" method="post" action="edit.php">
        <table border="0">
            <tr> 
                <td>Steam Username</td>
                <td><input type="text" name="steamUE" value="<?php echo $steamUE;?>"></td>
            </tr>
            <tr> 
                <td>Steam Username</td>
                <td><input type="text" name="steamPW" value="<?php echo $steamPW;?>"></td>
            </tr>
            <tr> 
                <td>Steam Guard Code</td>
                <td><input type="text" name="steamGC" value="<?php echo $steamGC;?>"></td>
            </tr>
            <tr>
                <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
                <td><input type="submit" name="update" value="Update"></td>
            </tr>
        </table>
    </form>
</body>
</html>

delete.php

<?php
//including the database connection file
include("config.php");

//getting id of the data from url
$id = $_GET['id'];

//deleting the row from table
$result = mysqli_query($mysqli, "DELETE * FROM `user` WHERE id='$id'");

//redirecting to the display page (index.php in our case)
header("Location: index.php");
?>

add.php

<html>
<head>
    <title>Add Data</title>
</head>

<body>
<?php
//including the database connection file
include_once("config.php");

if(isset($_POST['Submit'])) {   
    $steamUE = mysqli_real_escape_string($mysqli, $_POST['steamUE']);
    $steamPW = mysqli_real_escape_string($mysqli, $_POST['steamPW']);
    $steamGC = mysqli_real_escape_string($mysqli, $_POST['steamGC']);

    // checking empty fields
    if(empty($steamUE) || empty($steamPW) || empty($steamGC)) {

        if(empty($steamUE)) {
            echo "<font color='red'>Steam Username field is empty.</font><br/>";
        }

        if(empty($steamPW)) {
            echo "<font color='red'>Steam Password field is empty.</font><br/>";
        }

        if(empty($steamGC)) {
            echo "<font color='red'>Steam Guard Code field is empty.</font><br/>";
        }

        //link to the previous page
        echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
    } else { 
        // if all the fields are filled (not empty) 

        //insert data to database   
        $result = mysqli_query($mysqli, "INSERT INTO `user` (steamUE,steamPW,steamGC) VALUES ('$steamUE','$steamPW','$steamGC')");

        //display success message
        echo "<font color='green'>Data added successfully.";
        echo "<br/><a href='index.php'>View Result</a>";
    }
}
?>
</body>
</html>

config.php

<?php
/*
// mysql_connect("database-host", "username", "password")
$conn = mysql_connect("localhost","root","root") 
            or die("cannot connected");

// mysql_select_db("database-name", "connection-link-identifier")
@mysql_select_db("test",$conn);
*/

/**
 * mysql_connect is deprecated
 * using mysqli_connect instead
 */

$databaseHost = 'sql.website.com';
$databaseName = '';
$databaseUsername = '';
$databasePassword = '';

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName); 

?>

它不是没有说或没有显示任何错误或任何其他问题,而是说仅成功添加了数据,没有其他内容。 我不明白为什么它不向表中添加任何数据,我一次又一次地检查了所有内容,也许是因为我很累,但是我试图重命名表名但没有任何变化,是一样的...

发现了三个错误

add.php :列名不应包含“”。 检查以下内容

$result = mysqli_query($mysqli, "INSERT INTO user (steamUE,steamPW,steam_GC) VALUES ('$steamUE','$steamPW','$steamGC')");

edit.php :“ $id缺少”。 检查以下内容

$result = mysqli_query($mysqli, "UPDATE user SET steamUE='$steamUE',steamPW='$steamPW',steamGC='$steamGC' WHERE id='$id'");

delete.php :“ $id缺少”。 检查以下内容

$result = mysqli_query($mysqli, "DELETE * FROM user WHERE id='$id'");

如果与数据库的连接成功,则它必须正常工作(这个答案值得您:D指点绿色)。

还是仅仅是因为我的托管服务商无法使用MySQLi而是使用MySQL?

无论遇到什么问题,都会出现错误或空白页。

检查您的dB连接。 转到mysqli,用(errno)用$ sql声明它,但在$ sql之前调用您的参数。 使用if条件检查您的连接。 在添加时,请使用带有$ stmnt的prepare并执行它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM