在我的网站上,有一个名为" My Ideal plank。"
这是一个表单,用户将输入他们的数据,并且会有一个返回消息,其中包含该人的理想董事会类型。
第一步,制作表格:完成;
现在我需要连接并注册数据库中的数据。我在Wordpress数据库中创建了一个名为" minhaprancha"的表。但是我无法在其上记录数据:confused:
连接代码和注册如下:
container.RegisterType<IUnitOfWorkAsync, UnitOfWork>(
"test",
new TransientLifetimeManager(),
new InjectionConstructor(container.Resolve<IDataContextAsync>("test")));
container.RegisterType<ICodeRepository, CodeRepository>();
container.RegisterType<CodesController, CodesController>();
首先,如何在数据库中连接和注册数据?
-----------------------更新---------------------
我管理了它!我直接建立了联系:
JSON
exceptionMessage=An error occurred when trying to create a controller of type 'CodesController'. Make sure that the controller has a parameterless public constructor.
exceptionType=System.InvalidOperationException
innerException
exceptionMessage=Type '<Namespace>.Api.Controllers.CodesController' does not have a default constructor
stackTrace= at System.Linq.Expressions.Expression.New(Type type)
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
数据库中插入的代码是一样的。但是,我为任何变量更改了global $wpdb;
$nome = "";
$email = "";
$estilo = "";
$experiencia = "";
$altura = "";
$peso = "";
//VÁRIÁVEIS
if(!empty($_POST)){
$nome = $_POST['nome'];
$email = $_POST['email'];
$estilo = $_POST['estilo'];
$experiencia = $_POST['experiencia'];
$altura = $_POST['altura'];
$peso = $_POST['peso'];
cadastrar($nome,$email,$estilo,$experiencia,$altura,$peso);
calcularIMC($estilo,$experiencia,$altura,$peso);
}
function cadastrar($nome,$email,$estilo,$experiencia,$altura,$peso){ //INSERE OS DADOS NO BANCO
try{
$wpdb->insert( "INSERT INTO aa_minhaprancha("."nome, email, estilo, experiencia, altura, peso) VALUES (". ":nome', ':email', ':estilo', ':experiencia', ':altura', ':peso')");
if($wpdb->rowCount() > 0)
return true;
else
return false;
}catch(PDOException $e){
echo "Erro ao incluir na tabela categoria ".$e->getMessage();
}
}
。
答案 0 :(得分:0)
在wordpress中,引用是写的:
<?php $wpdb->insert( $table, $data, $format ); ?>
其中$ data和$ format是数组。
答案 1 :(得分:0)
首先,您应该阅读有关如何正确使用$wpdb->insert()
的文档 $nome = "";
$email = "";
$estilo = "";
$experiencia = "";
$altura = "";
$peso = "";
//VÁRIÁVEIS
if(!empty($_POST)){
$nome = $_POST['nome'];
$email = $_POST['email'];
$estilo = $_POST['estilo'];
$experiencia = $_POST['experiencia'];
$altura = $_POST['altura'];
$peso = $_POST['peso'];
cadastrar($nome,$email,$estilo,$experiencia,$altura,$peso);
calcularIMC($estilo,$experiencia,$altura,$peso);
}
function cadastrar($nome,$email,$estilo,$experiencia,$altura,$peso){ //INSERE OS DADOS NO BANCO
// the WPDB class is already setup for us let's make it accessible
global $wpdb;
// the name of our table
$table = 'aa_minhaprancha';
// data is an array with the format column_name => data
$data = array(
'nome' => $nome,
'email' => $email,
'estilo' => $estilo,
'experiencia' => $experiencia,
'altura' => $altura,
'peso' => $peso,
);
// run the insert
$inserted = $wpdb->insert( $table, $data );
// if it didn't update get the error
if ( ! $inserted ) {
$wpbb->print_error();
}
}
根据表格的设置,您可能需要在插入时添加额外的$format
参数。这是要映射到$data