Error trying to register data in the database (mysql) via PHP

Asked

Viewed 554 times

1

I’m trying to insert some data in the database via php, however this returning me error, apparently this connecting, I can not find an error actually not know much of php, I’ve been following tutorial on the internet, follows below the codes.

$(function(){

		 		var atual_fs, next_fs
		 		var formulario = $('form[name=formulario]');

		 		$('.btn-next1').on('click',function (evento) {

		 			if ($('select , .range').valid()) {
			 			atual_fs = $(this).parent().parent().parent();
			 			next_fs = $(this).parent().parent().parent().next();

			 			atual_fs.hide(500);
			 			next_fs.show(500);				
		 			}else {
		 			}
		 			evento.preventDefault();

		 		});

		 		$('.btn-next').on('click',function (evento){

		 			var array = formulario.serializeArray();

		 			$.each(array, function(i, val){
		 				console.log($.param(array));
		 			});

		 			if ($('#name , #email , #telefone').valid()) {
		 				$.ajax({
		 					method: 'post',
		 					url: 'cadastrar.php',
		 					data: {cadastrar: 'sim', campos: array},
		 					dataType: 'json',
		 					beforeSend: function(){
		 						$('#formulario').html('<div class="aguarde"><p> Aguarde estamos requisitando os planos!</p></div>')
		 					},
		 					success: function(valor){
		 						if(valor.erro == 'sim'){
		 							$('#formulario').html('<div class="erros"><p>'+valor.getErro+'</p></div>');
		 						}else{
		 							$('#formulario').html('<div class="ok"><p>'+valor.msg+'</p></div>');
		 						}
		 					}
		 				});

		 			}else {
		 				
		 			}
		 			evento.preventDefault();

		 		});

		});
<?php 

	sleep(2);
	include_once 'config.php';

	if(isset($_POST['cadastrar']) && $_POST['cadastrar'] == 'sim'):

		$novos_campos = array();
		$campos_post = $_POST['campos'];
		foreach($campos_post as $indice => $valor){
			$novos_campos[$valor['name']] = $valor['value'];
		}

		if(!strstr($novos_campos['email'], '@')){
			$respostas['erro'] = 'sim';
			$respostas['getErro'] = 'E-mail Invalido.';		
		}else{
			$respostas['erro'] = 'nao';
			$inserir_banco = $pdo->prepare("INSERT INTO `lead` SET nome = ?, email = ?, telefone = ?, desejo = ?, valor = ?, valores = ?");
			$inserir_sql = array(
				$novos_campos['name'],
				$novos_campos['email'],
				$novos_campos['telefone'],
				$novos_campos['desejo'],
				$novos_campos['valor'],
				$novos_campos['valores']
			);
			if($inserir_banco->execute($array_sql)){
				$respostas['erro'] = 'nao';
				$respostas['msg'] = 'Cliente Cadastrado';
			}else{
				$respostas['erro'] = 'sim';
				$respostas['getErro'] = 'Erro ao cadastrar.';	
			}
		}

		echo json_encode($respostas);
	endif;
 ?>

config.php

<?php $pdo = new PDO('mysql:host=localhost;dbname=simulador','usuario','senha'); ?>
  • 1

    @GOKUSSJ4 then, is not registering in the bank and returns me $replies['getErro'] = 'Error when registering. '; ... I don’t know if I can consider this a mistake, thank you.

  • 1

    Returns the bank error, do so $respostas['getErro'] = $inserir_banco->errorInfo(); after the one print_r() in error.

1 answer

1


Change:

$inserir_banco->execute($array_sql);

For:

$inserir_banco->execute($inserir_sql);

I don’t know if you’ve noticed but $array_sql has not been assigned anywhere in your code snippet.

  • Vlwww worked out registered, thank you very much.

Browser other questions tagged

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