user3849771
user3849771

Reputation: 57

Why is it not inserting to the database?

<?php
                    if(!isset($_GET['nick']))
                    {
                        echo '<form action="ban.php" method="get">
                              <input placeholder="Username" type="text" name="nick"/>
                              <input placeholder="Reason" type="text" name="reason"/>
                              <input type="submit" value"Ban!"/>';
                    } else
                    {
                        $nick = mysql_real_escape_string($_GET['nick']);
                        $reason = mysql_real_escape_string($_GET['reason']);

                        $query = mysql_query("SELECT Name FROM users WHERE Name='".$nick."'");
                        if (mysql_num_rows($query) != 0)
                        {
                            while($rows = mysql_fetch_assoc($query))
                            {
                                $IP = $rows['IP'];
                            }
                        }
                        else
                        {
                            echo "No user found!";
                        }
                        $today = date("d/m/y"); 
                        $sql = "INSERT INTO bans (`Name`, `IP`, `BReason`, `BAdmin`, `BDate`) VALUES ('$nick', '$IP', '$reason', '$name', '$today')";
                        if ($connect->query($sql) === TRUE)
                        {
                            echo "May his account rest in peace.";
                        } 
                        else 
                        {
                            echo "Error: " . $sql . "<br>" . $conn->error;
                        }
                    }
                    ?>

For some reason, it doesn't insert into the database. It doesn't show any message. I tried a lot but I can't seem to find a fix for it.

Help please? Thank you.

Upvotes: 0

Views: 63

Answers (2)

Martin
Martin

Reputation: 22760

You have several issues, firstly really, really replace all MySQL with MySQLi and read a bit about how to edit the SQL to make it MySQLi - it doesn't take much time or effort to change the MySQL for the better.

as for the code:

$query = mysql_query("SELECT Name FROM users WHERE Name='".$nick."'");
                 if (mysql_num_rows($query) != 0)
                    {
                        while($rows = mysql_fetch_assoc($query))
                        {
                            $IP = $rows['IP'];
                        }
                    } 

You are running $query as returning all the values of Name where Name = '"$nick"'.

It may not be an issue but I seems for me to work better to just contain the strings straight into the MySQL -

Name = '$nick' 

But you're only returning the Name from searching for the name so the output of the $Query will only ever be $rows['Name'] = $nick. If you want IP set Query to:

$Query = mysql_query("SELECT IP FROM users WHERE Name='$nick' "); 

So that will return in the $row the key value pair of $row['IP'] = The IP value you want.

$connet->query() may well work but it looks like a different form to the above MySql code, and may not work with old MysqL (rather than MySQLi). So try:

if(mysql_query("INSERT INTO bans (`Name`, `IP`, `BReason`, `BAdmin`, `BDate`) VALUES  ('$nick', '$IP', '$reason', '$name', '$today')")){
    print "Inserted ok!";
} 
else {
 print mysql_error(); <== this will output any error if the previous query fails.
}

Try the above and see what you discover .

EDIT: http://www.pontikis.net/blog/how-to-use-php-improved-mysqli-extension-and-why-you-should Gives you a decent guide on how to turn MySQL into MySQLi . Also check what version of PHP you run as MySQL is deprecated and doesn't work with more recent PHP. Also check your PHP error log file for background info as to why you don't get the responses you expect.

Upvotes: 2

Kickstart
Kickstart

Reputation: 21513

Your current query relies on a select that returns the name field, but then stores the IP field (which the query does not return). You need to return and store both

However I would do the return of the values and the insert as just a single piece of SQL:-

INSERT INTO bans (`Name`, `IP`, `BReason`, `BAdmin`, `BDate`)
SELECT Name, IP, '$reason', '$name', '$today' 
FROM users 
WHERE Name= '$nick'

Upvotes: 0

Related Questions