I need help adding new users to a table i have made for an admin that can add or delete users from said table freely. What i have so far is an entry that can add date and time but for some reason it doesnt add the username and password. I dont know if its skipping the username and password and simply adding them the second you go to that page but something is wrong and i can figure this out, i have been stuck for days and what makes little sense to me is that i have this same code somewhere else but it woks there, allbeit a little different but its the same concept so then why isnt it working here??

here is the code for the adduser.php

<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";
}

?>

and here is the index.php code

<?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>

Recommended Answers

All 5 Replies

Okay so first things first, any SQL/PHP error messages at all?

Have you checked that your databases are set up exactly the same, there are no differences in structures etc so they perform exactly the same.

If they are identical, is it because this new location doesn't support or is having troubles with the mysqli_real_escape_string()?

To me, if you have to copies of the same code (in different locations) and one fails to work, it means the location itself is causing the error or something that has to be setup for each location (databases). These are really the only things that change between the two, so are the probable cause of error. (Assuming no other error messages)

i think this issue is in that i have split the original functionality of the code into 2 pages instead of haveing it all on one, u see beofre i had the entries and the execution all on one page and it worked perfectly but now i have it set when the user hits the add new user button on the index page it takes them to the page with the new user prompts and i believe that is causing my issue, its executing before the data is even being passed, but how woul di set this up to work? could i use links to move the user to the page where this new code is and THEN execute it then have another link so the user can go back? or is there a simpler way? also i have tried everything and tested everything, the logic is working just fine i just feel there is something up with the execution. Also im not getting any errors thats my real issue.

okay i figured out whats kinda of wrong but i have no idea how to go about fixing it.

this part of my code:

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

is returning false so im not actually using anything from the user, it works on another page i have but wont work here? i dont understand why this is or how i even fix it??

try changing the form method attribute to what is expected by the form processor.

for example, if you want to process this form with get method,

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

then it should be like this

<form method="get" action="index.php">

u my friend... have solved all my issues in like 5 lines... THANK YOU SO MUCH! MUCH LIKE, VERY HELP, MUCH AWESOME!!!

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.