I have this code which ironically WORKS on another page i have but for some reason will not work with my new page even though LITERALLY its the exact same function and should work regardless but with my luck it wont so i need help, maybe i have a small error but i have compared and compared the code time and time again and theres literally no difference from what i can tell.

here is the code:

<?php

$con = mysqli_connect("localhost", "root", "", "numbers") or die(mysqli_error($con));   

//submission code, inserting data into mysql database   

$username = NULL;
$password = NULL;
$mySqlDate = date('Y-m-d');
$mySqlTime = date('g:i a');

if(isset($_GET['add']))
{
    $username = $_GET['username'];
    $password = $_GET['password'];
}

//filter out non numeric and special characters
$username = mysqli_real_escape_string($con, $_GET['username']);
$password = mysqli_real_escape_string($con, $_GET['password']);


$check = mysqli_query($con, "SELECT count(*) FROM admin WHERE username = '".$username."'") or die();
$row = mysqli_fetch_row($check);

if ($row[0] == 0)
    {
        $sql="INSERT INTO admin (username, password, date, time) VALUES ('".$username."','".$password."','".$mySqlDate."','".$mySqlTime."')";

        if (!mysqli_query($con,$sql)) 
        {
            die('Error: ' . mysqli_error($con));
        }
        $length = "Remember to use lower case letters and do not use spaces";
    echo "<font size='5' color='blue'>".$length."</font> ";
    }
else
{
    $length = "Remember to use lower case letters and do not use spaces";
    echo "<font size='5' color='blue'>".$length."</font> ";
}

?>

<html>


<form method="post" action="index.php">
<table  width="400" border="0" cellspacing="1" cellpadding="2">


<div style = "margin-bottom: 100px" >
<tr width="200">
<font size="6">
Add a New User with Administrative Privileges
</tr>
</font>
</div>


<tr>
<td width="100">Username:</td>
<td>
<input name="username" type="text" id="username">
</td>
</tr>

<tr>
<td style="width:100%;" width="200">Password:</td>
<td>
<input name="password" type="text" id="password">
</td>
</tr>

<tr>
<td>
<input name="add" type="submit" id="add" value="Submit">
</td>
</tr>

</table>
</form>


</html>

this is the adduser.php file
any and all help is greatly appreciated!

Dani AI

Generated

Short diagnosis (why you see a blank row): the form in your add page is being submitted with POST while your processing code reads from $_GET (and runs DB code even when there was no submission). That combination will insert an empty username/password (date/time still set), then the next submit is blocked because that blank username already exists. Also note you changed from a COUNT check to SELECT / fetch_row which doesn't give the same numeric result — use COUNT() or check num_rows.

Concrete fixes

  • Make the form post to the same processing file (or change the processing file to the action target).
  • In PHP, read from $_POST, not $_GET. Only run the insert when the request method is POST (or when isset($_POST['add'])).
  • Use parameterized queries (prepared statements) instead of string concatenation.
  • Be consistent: do not mix mysql* and mysqli* in the same app.
  • Store passwords safely (use password_hash) and consider using a DATETIME column with NOW() instead of separate date/time strings.

Example processing pattern (put this at the top of adduser.php and make the form action point to adduser.php):

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add'])) {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if ($username === '' || $password === '') { /* show error */ }
    else {
        $mysqli = new mysqli('localhost','root','','numbers');
        $stmt = $mysqli->prepare('SELECT COUNT(*) FROM admin WHERE username = ?');
        $stmt->bind_param('s',$username);
        $stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch();
        $stmt->close();

        if ($count == 0) {
            $hash = password_hash($password, PASSWORD_DEFAULT);
            $stmt = $mysqli->prepare('INSERT INTO admin (username,password,date,time) VALUES (?, ?, CURDATE(), CURTIME())');
            $stmt->bind_param('ss',$username,$hash);
            $stmt->execute();
            $stmt->close();
        }
        $mysqli->close();
    }
}
?>

Quick debugging tips: enable error reporting during development, var_dump($_POST) to see what arrives, and confirm the submit button is named exactly what your PHP checks. This will resolve the blank-entry behavior you described in your posts, .

OH forgot to note that it will add one entry but will only correctly add the time and date to the table with username and password being blank, then after that it will add nothing unless i delete the one added entry.

here is something else to add, i have been testing and found that it will actually go ahead and enter the date and time info into the database but not the username and password, it does this without even needing any information entered into the username or password fields, when u hit back it will show a new entry in the table but with no username and password but it will have date and time.. weird...

ok here is some more info if it helps, i have a basic setup so its very plain i just need it to work, this is my code so far but i have no idea whats wrong with it:

<html>


<form method="post" action="index.php">
<table  width="400" border="0" cellspacing="1" cellpadding="2">


<div style = "margin-bottom: 100px" >
<tr width="200">
<font size="6">
Add a New User with Administrative Privileges
<br>
<br>
</tr>
</font>
</div>


<tr>
<td width="100">Username:</td>
<td>
<input name="username" type="text" id="username">
</td>
</tr>

<tr>
<td style="width:100%;" width="200">Password:</td>
<td>
<input name="password" type="text" id="password">
</td>
</tr>

<tr>
<td>
<input name="new" type="submit" id="new" value="Add User">
</td>
</tr>

</table>
</form>

</html>


<?php include('header.php');

$con = mysqli_connect("localhost", "root", "", "numbers") or die(mysqli_error($con));   

//submission code, inserting data into mysql database   

$username = "";
$password = "";
$mySqlDate = date('Y-m-d');
$mySqlTime = date('g:i a');

if(isset($_GET['new']))
{
    $username = $_GET['username'];
    $password = $_GET['password'];
}

//filter out non numeric and special characters
$username = mysqli_real_escape_string($con, $_GET['username']);
$password = mysqli_real_escape_string($con, $_GET['password']);


$check = mysqli_query($con, "SELECT * FROM admin WHERE username = '".$username."'") or die();
$row = mysqli_fetch_row($check);

if ($row[0] == 0)
{
    $sql="INSERT INTO admin (username, password, date, time) VALUES ('".$username."','".$password."','".$mySqlDate."','".$mySqlTime."')";

    if (!mysqli_query($con,$sql)) 
    {
        die('Error: ' . mysqli_error($con));
    }
        echo "false";
    //$length = "Remember to use lower case letters and do not use spaces";
    //echo "<font size='5' color='blue'>".$length."</font> ";
}
else
{
    echo "true";
}

?>

thats the adduser.php

and this:

<?php 

/*
session_start();
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == false){
    header('location: index.php');
    exit();
}
*/
include('header.php');
//include('index.php');

?>
<body>

    <div class="row-fluid">
        <div class="span12">




            <div class="container">

<br><br>
                            <form method="post" action="delete.php" >
                        <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
                            <div class="alert alert-info">

                                <strong><i class="icon-user icon-large"></i>&nbsp;Data From User Submitted Data</strong>
                                &nbsp;&nbsp;Check the CheckBox then click the Delete button to Delete selected Data 
                            </div>
                            <thead>

                                <tr>
                                    <th></th>
                                    <th>User Name</th>
                                    <th>Password</th>
                                    <th>Date Added</th>
                                    <th>Time Added</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php 
                            $query=mysql_query("select * from admin")or die(mysql_error());
                            while($row=mysql_fetch_array($query)){
                            $id=$row['id'];
                            ?>

                                        <tr>
                                        <td>
                                        <input name="selector[]" type="checkbox" value="<?php echo $id; ?>">
                                        </td>
                                         <td><?php echo $row['username'] ?></td>
                                         <td><?php echo $row['password'] ?></td>
                                         <td><?php echo $row['date'] ?></td>
                                         <td><?php echo $row['time'] ?></td>
                                </tr>

                                  <?php } ?>
                            </tbody>
                        </table>
                        <input type="submit" class="btn btn-danger" value="Delete" name="delete">

</form>

<form method="post" action="adduser.php" >

<br>

<div class = "exportbtn"> 
<input type="submit" class="btn btn-info" value="Add New User" name="export">
</div>

</form>
        </div>
        </div>
        </div>

</body>
</html>

is my index.php why is this not working, am i missing something??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.