I cannot enter user through/php database

Asked

Viewed 53 times

1

Good morning, you guys. I’m facing a somewhat embarrassing problem. I can update the user via the panel I built, but I can’t add a new user to it.

Follows the code.

<?   
		if (isset($_POST['submitted'])) { 
			$matricula	= $_POST['matricula'];
			$nome 		= $_POST['nome'];
			$senha		= md5($_POST['senha']);
			$email 		= $_POST['email'];
			$nivel 		= $_POST['nivel'];
			$funcao		= $_POST['funcao'];
			$menus		= implode(",", $_POST['menus']);
			$acesso		= $_POST['acesso'];

			$sql = "
				INSERT INTO tbl_usuarios(
					nome_usuario, 
					login_usuario, 
					senha_usuario,
					email_usuario, 
					nivel_usuario,
					funcao_usuario,
					menus_usuario,
					acesso_usuario,
				) 
				VALUES(
					'".$nome."',
					'".$matricula."',					
					'".$senha."',
					'".$email."',
					'".$nivel."',
					'".$funcao."',
					'".$menus."',
					'".$acesso."',
				)"; 
			$sucesso = mysql_query($sql) or die(mysql_error()); 
			$id = mysql_insert_id();

			if ($sucesso){
				echo "<p class='sucesso'>Dados inseridos corretamente - CÓDIGO USUÁRIO: ".$id."</p>";
			}else 
				die(mysql_error());
		}	
	?>
	<br />	
<form id="frmUsuarios" name="frmUsuarios" action='#' method='POST'>
<table border="0" id="tabela_incluir">
    <tr class="cor2">
    	<td class="dir">Matrícula*:</td>
        <td><input type="text" size="6" id="matricula" name="matricula" class="input validate[required,custom[onlyNumberSp]]" onblur="validaUser()" maxlength="6" />&nbsp;<span id="valid"></span>
        </td>
	</tr>
    <tr class="cor2">
    	<td class="dir">Nome*:</td>
        <td><input type="text" size="66" name="nome" class="input validate[required]" /></td>
	</tr>
    <tr class="cor2">
    	<td class="dir">Senha*:</td>
        <td><input type="password" size="40" name="senha" class="input-normal validate[required]" /></td>
	</tr>
    <tr class="cor2">
    	<td class="dir">E-mail*:</td>
        <td><input type="email" size="40" name="email" class="input-normal validate[required,custom[email]]" /></td>
	</tr>
    <tr class="cor2">
    	<td class="dir">Nível*:</td>
        <td><select name="nivel" class="select validate[required]">
    		<?
			$result = mysql_query("SELECT * 
				FROM tbl_nivel_acesso 
				ORDER BY 3"
			);
			print ("<option value='' selected='selected'></option>");
			while ($rows = mysql_fetch_array($result)){
				print("<option value='{$rows['niv_nivel']}'");
					print(">$rows[niv_descricao_nivel]");
				print("</option>");
			}
			?>
			</select></td>
	</tr>
    <tr class="cor2">
    	<td class="dir">Função*:</td>
        <td><select name="funcao" class="select validate[required]">
    		<?
			$result = mysql_query("SELECT * 
				FROM tbl_funcao 
				ORDER BY 2"
			);
			print ("<option value='' selected='selected'></option>");
			while ($rows = mysql_fetch_array($result)){
				print("<option value='{$rows['id_funcao']}'");
					print(">$rows[descricao_funcao]");
				print("</option>");
			}
			?>
			</select></td>
	</tr>
    <tr class="cor2">
    	<td class="dir">Menus*:</td>
        <td>
        	<input type="checkbox" name="menus[]" value="1" class="validate[required]" /> Cadastros
            <br />
            <input type="checkbox" name="menus[]" value="2" class="validate[required]" /> Clientes
            <br />
            <input type="checkbox" name="menus[]" value="3" class="validate[required]" /> Projetos
            <br />
            <input type="checkbox" name="menus[]" value="4" class="validate[required]" /> Logs
            <br />
            <input type="checkbox" name="menus[]" value="5" class="validate[required]" /> Boletos
            <br />
            <input type="checkbox" name="menus[]" value="6" class="validate[required]" /> Pagamentos
            <br />
            <input type="checkbox" name="menus[]" value="7" class="validate[required]" /> Contas
            <br />
            <input type="checkbox" name="menus[]" value="8" class="validate[required]" /> Contato
        </td>
	</tr>
    <tr class="cor2">
    	<td class="dir">Acesso*:</td>
        <td>
        	<select name="acesso" class="select validate[required]">
            	<option value="" selected="selected"></option>
                <option value="1">SIM</option>
                <option value="0">NÃO</option>
            </select>
        </td>
	</tr>
    <tr>
	    <td></td>
        <td align="right">
			<input type="button" class="button" value="Gravar" />
            <input type="button" class="button" value="Voltar" name="voltar" onclick="location.href='?acessando=usuarios-listar&titulo=USUÁRIOS';">
            <input type='hidden' value='1' name='submitted' />
		</td>
	</tr> 
</table>
</form>

The code works perfectly if I want to edit the user level, the function, but if I create the registration of one, and I click on "record", it does nothing, it does not insert the user into the database with the information, and even shows an error message. Could someone check for me if it contains any errors?

  • No error gets harder. But looking at your code, if the same script works for update, but not to insert, is that your BD user is without privilege to enter data?

1 answer

0

<?php   
$acao = mysql_escape_string($_GET['matricular']);

if($acao == 'sim'):
            $matricula  = htmlspecialchars($_POST['matricula'], ENT_QUOTES);
            $nome       = htmlspecialchars($_POST['nome'], ENT_QUOTES);
            $senha      = md5($_POST['senha']);
            $email      = htmlspecialchars($_POST['email'], ENT_QUOTES);
            $nivel      = $_POST['nivel'];
            $funcao     = $_POST['funcao'];
            $menus      = implode(",", $_POST['menus']);
            $acesso     = $_POST['acesso'];

            $sql = mysql_query("
                INSERT INTO tbl_usuarios(nome_usuario, login_usuario, senha_usuario,email_usuario, nivel_usuario,funcao_usuario,menus_usuario,acesso_usuario) VALUES('$nome','$matricula','$senha','$email','$nivel','$funcao','$menus','$acesso')"); 

            if ($sucesso){
                echo "<p class='sucesso'>Dados inseridos corretamente - CÓDIGO USUÁRIO: ".$id."</p>";
            }else {
                echo ('Ops.. Ocorreu um erro... Informe o suporte.');
            }

    endif;
    ?>
    <br />  
<form id="frmUsuarios" name="frmUsuarios" action='?matricular=sim' method='POST'>
<table border="0" id="tabela_incluir">
    <tr class="cor2">
        <td class="dir">Matrícula*:</td>
        <td><input type="text" size="6" id="matricula" name="matricula" class="input validate[required,custom[onlyNumberSp]]" onblur="validaUser()" maxlength="6" />&nbsp;<span id="valid"></span>
        </td>
    </tr>
    <tr class="cor2">
        <td class="dir">Nome*:</td>
        <td><input type="text" size="66" name="nome" class="input validate[required]" /></td>
    </tr>
    <tr class="cor2">
        <td class="dir">Senha*:</td>
        <td><input type="password" size="40" name="senha" class="input-normal validate[required]" /></td>
    </tr>
    <tr class="cor2">
        <td class="dir">E-mail*:</td>
        <td><input type="email" size="40" name="email" class="input-normal validate[required,custom[email]]" /></td>
    </tr>
    <tr class="cor2">
        <td class="dir">Nível*:</td>
        <td><select name="nivel" class="select validate[required]">
            <?php
            $result = mysql_query("SELECT * 
                FROM tbl_nivel_acesso 
                ORDER BY 3"
            );
            print ("<option value='' selected='selected'></option>");
            while ($rows = mysql_fetch_array($result)){
                print("<option value='{$rows['niv_nivel']}'");
                    print(">$rows[niv_descricao_nivel]");
                print("</option>");
            }
            ?>
            </select></td>
    </tr>
    <tr class="cor2">
        <td class="dir">Função*:</td>
        <td><select name="funcao" class="select validate[required]">
            <?php
            $result = mysql_query("SELECT * 
                FROM tbl_funcao 
                ORDER BY 2"
            );
            print ("<option value='' selected='selected'></option>");
            while ($rows = mysql_fetch_array($result)){
                print("<option value='{$rows['id_funcao']}'");
                    print(">$rows[descricao_funcao]");
                print("</option>");
            }
            ?>
            </select></td>
    </tr>
    <tr class="cor2">
        <td class="dir">Menus*:</td>
        <td>
            <input type="checkbox" name="menus[]" value="1" class="validate[required]" /> Cadastros
            <br />
            <input type="checkbox" name="menus[]" value="2" class="validate[required]" /> Clientes
            <br />
            <input type="checkbox" name="menus[]" value="3" class="validate[required]" /> Projetos
            <br />
            <input type="checkbox" name="menus[]" value="4" class="validate[required]" /> Logs
            <br />
            <input type="checkbox" name="menus[]" value="5" class="validate[required]" /> Boletos
            <br />
            <input type="checkbox" name="menus[]" value="6" class="validate[required]" /> Pagamentos
            <br />
            <input type="checkbox" name="menus[]" value="7" class="validate[required]" /> Contas
            <br />
            <input type="checkbox" name="menus[]" value="8" class="validate[required]" /> Contato
        </td>
    </tr>
    <tr class="cor2">
        <td class="dir">Acesso*:</td>
        <td>
            <select name="acesso" class="select validate[required]">
                <option value="" selected="selected"></option>
                <option value="1">SIM</option>
                <option value="0">NÃO</option>
            </select>
        </td>
    </tr>
    <tr>
        <td></td>
        <td align="right">
            <input type="button" class="button" value="Gravar" />
            <input type="button" class="button" value="Voltar" name="voltar" onclick="location.href='?acessando=usuarios-listar&titulo=USUÁRIOS';">
            <input type='hidden' value='1' name='submitted' />
        </td>
    </tr> 
</table>
</form>
  • Be careful because <? is no longer being used look to change to <?php Copy the code and test this is probably right

  • I tested it and the same thing. I saw that you put a code in case of an error. Even with this, it does not return me the error that you added and does not add inside the database. Any more suggestions?

  • see your connection or your db configuration because the code is right for me. Hey I have not edited only php edited the tmb form.!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.