简体   繁体   中英

Add Edit Delete form not adding into MySQL no errors

I have this Add Edit Delete form, the problem is: when I put everything and I click on ADD it says "Data added successfully." but the data isn't in my table of phpAdmin and it not shows in the page... Or is simply because my hoster doens't work with MySQLi but with MySQL? Without talking about SQL Injections because Im not so expert and dont know how protect from that, this pages will be protected with login area so only restricted members will access to it.

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

?>

It not doesn't says or shows any errors or any other problems, it says only data added successfully and nothing else. I don't understand why it doesn't add any data in my tables, i checked everything again and again, maybe because i'm tired but i tried to rename tables names but nothing change, is the same...

Spotted three errors,

add.php : Column names should be without ''. Check the following

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

edit.php : '' missing from $id . Check the following

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

delete.php : '' missing from $id . Check the following

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

If the connection with DB is successful, it must work (and this answer deserves a green tick from you :D).

Or is simply because my hoster doens't work with MySQLi but with MySQL?

Wherever I faced issues, I got some error or a blank page.

Check your dB connection. Turn to mysqli, declair it with $sql with (errno), but call your param before $sql. Use if condition to check your connection. On your add please use prepared with $stmnt and execute it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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