PHP MYSQL->执行失败:(1048)列'标题'不能为空错误 [英] PHP MYSQL->execute failed: (1048) Column 'title' cannot be null ERROR

查看:235
本文介绍了PHP MYSQL->执行失败:(1048)列'标题'不能为空错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



执行失败:(1048)'title'列不能成为'title' null



我的表单的工作方式如下:

提交后,我的HTML表单将输入的值发送给一个JavaScript函数processIdea()。这个函数然后检查错误,如果没有找到,表单将被发送到ideaSubmission.php。这段代码的目的是获取发布的值,检查它们是否存在,然后将它们插入到我的Mysql数据库中。

这里是我的3个文件; *

* HTML FORM:

 < form id =submit_ideaaction =submitIdea.phpmethod =POST > 
< fieldset>
<图例>意见提交< / legend>
< label for =title>标题< / label>
< input type =textname =title/>
< br>
< label for =brief>简介< / label>
< input type =textname =brief/>
< br>
< label for =problem>问题< / label>
< input type =textname =problem/>
< br>
< label for =solution>解决方案< / label>
< input type =textname =solution/>
< br>
< label for =audience>受众群体< / label>
< input type =textname =audience/>
< br>
< label for =prediction>预测< / label>
< input type =textname =prediction/>
< br>
< label for =constraints>约束< / label>
< input type =textname =constraints/>
< br>
<! - < label for =categories>请从下面的列表中选择适用的类别< / label>
< input type =checkboxid =categoriesnamecategories []valueApp/>& nbsp; App< br />>
< input type =checkboxnamecategories []valuebusiness_venture/>& nbsp; Business venture< br />>>
< input type =checkboxnamecategories []valuehome-ware/>& nbsp; Home-ware< br />>>
< input type =checkboxnamecategories []valuetechnology/>& nbsp; Technology< br />>>
< input type =checkboxnamecategories []valuefurniture/>& nbsp; Furniture< br />>>
< input type =checkboxnamecategories []valueart/>& nbsp; Art< br />>>
>>
< input type =checkboxnamecategories []valuefashion/>& nbsp; Clothing&Fashion< br />>>
< input type =checkboxnamecategories []valueculinary/>& nbsp; Culinary< br />>>
< input type =checkboxnamecategories []valuemisc/>& nbsp; Misc< br /> - >
< button type =submitonclick =processIdea();>提交< / button>
< div style =clear:both;>< / div>
< / fieldset>
< / form>

JAVASCRIPT功能:

  $(document)。ready(function(){
$(#submit_idea)。submit(function(){
processIdea();
return假;
});
});

函数processIdea(){

var errors ='';

//验证标题
var title = $(#submit_idea [name ='title'])。val();
if(!title){
errors + =' - 请输入标题'\\ n';
}
// Validate brief
var brief = $(#submit_idea [name ='brief'])。val();
if(!brief){
errors + =' - 请输入一个简短的想法brief \\\
';
}
//验证问题
var problem = $(#submit_idea [name ='problem'])。val();
if(!problem){
errors + =' - 请描述你想解决的问题';';
}
//验证解决方案
var solution = $(#submit_idea [name ='solution'])。val();
if(!solution){
errors + =' - 请描述您对上述问题的解答\';
}
//验证受众
var audience = $(#submit_idea [name ='audience'])。val();
if(!audience){
errors + =' - 请将您的解决方案的目标描述为'\\ n';
}
//验证预测
var prediction = $(#submit_idea [name ='prediction'])。val();
if(!prediction){
errors + =' - 请描述你想解决的预测问题'';
}
//验证约束
var constraints = $(#submit_idea [name ='constraints'])。val();
if(!constraints){
errors + =' - 请描述您的解决方案的约束条件\';
}
//验证类别
// var categories = $(#submit_idea [name ='categories []']:checked)。
// if(!categories){
// errors + =' - 请选择您的解决方案属于\ n的类别;
//}

if(errors){
errors ='发生以下错误:\ n'+ errors;
警报(错误);
返回false;
} else {

//通过Ajax提交表单,然后重置表单
$(#submit_idea)。ajaxSubmit({success:showResult,type:'post });
返回false;



函数showResult(data){
if(data =='save_failed'){
alert('Form save failed,please联系您的管理员');
返回false;
} else {
$(#submit_idea)。clearForm()。clearFields()。resetForm();
alert('表格保存成功');
返回false;





$ b

PHP:

 <?php 
//开始会话
include_once'/includes/db_connect.php';
include_once'/includes/functions.php';
sec_session_start();
$ b $ if(login_check($ mysqli)== true){

//检索表单数据
if(empty($ _ POST)){
回声空!; }
$ b $ if(isset($ _ POST ['submit_idea'])){
if(isset($ _ POST ['title'])){$ title = $ _POST ['title' ]。 }
if(isset($ _ POST ['brief'])){$ brief = $ _POST ['brief']; }
if(isset($ _ POST ['problem'])){$ problem = $ _POST ['problem']; }
if(isset($ _ POST ['solution'])){$ solution = $ _POST ['solution']; }
if(isset($ _ POST ['audience'])){$ audience = $ _POST ['audience']; }
if(isset($ _ POST ['prediction'])){$ prediction = $ _POST ['prediction']; }
if(isset($ _ POST ['constraints'])){$ constraints = $ _POST ['constraints']; }
// if(isset($ _ POST ['categories'])){$ categories = $ _POST ['categories']; }


if(!$ title ||!$ brief ||!$ problem ||!$ solution ||!$ audience ||!$ prediction ||!$ constraints){
echosave_failed;
return;

//


//将类别数组转换为序列化的字符串
// $ categories_list = serialize($ categories);

// if(!$ link){
// echosave_failed;
// return;
//}
// mysql_select_db($ db ['idea']);

//在执行插入操作之前清除变量
$ clean_title = $ mysqli-> real_escape_string($ title);
$ clean_brief = $ mysqli-> real_escape_string($ brief);
$ clean_problem = $ mysqli-> real_escape_string($ problem);
$ clean_solution = $ mysqli-> real_escape_string($ solution);
$ clean_audience = $ mysqli-> real_escape_string($ audience);
$ clean_prediction = $ mysqli-> real_escape_string($ prediction);
$ clean_constraints = $ mysqli-> real_escape_string($ constraints);
// $ clean_categories_list = mysql_real_escape_string($ categories_list);
}
else {
//执行插入
$ now = time();

$ user_id = $ _SESSION ['user_id']; $($!

)if(!$ stmt = $ mysqli-> prepare(INSERT INTO idea_thread(user_id,time,title,Brief,problem,solution,audience,prediction,constraints)VALUES(?,?如果(!$ stmt-> bind_param('issssssss',$ user_id());} ,$ now,$ clean_title,$ clean_brief,$ clean_problem,$ clean_solution,$ clean_audience,$ clean_prediction,$ clean_constraints)){echo绑定参数失败:;}

if(!$ stmt- > execute()){echo执行失败:(。$ stmt-> errno。)。 $ stdt->错误;}

// if(@mysql_query($ sql,$ link)){
echosuccess;
}
// return;
//} else {
// echosave_failed;
// return;
//}
} else {
echo你是怎么到这里来的?请先登录!
header(Location:../signup.php);
出口;
}
?>

所有帮助表示感谢!



编辑:ajaxSubmit是一个jquery表单插件,是从这个来源;

http://malsup.github。 com / jquery.form.js

解决方案

正如你可以在你的php代码和你的 var_dump($ _ POST) submit_idea 永远不会被提交,因此永远不会进入 if isset ($ _POST ['submit_idea'])将其他发布值设置为适当的变量。

在表单中提交该值或在php中更改条件


I've run into a curious error today while coding a HTML / JS / PHP webform.

"Execute failed: (1048) Column 'title' cannot be null"

The way my form works is as follows;

Upon submit, my HTML form takes entered values, and sends them to a javascript function "processIdea()". This function then checks for errors and if none are found, the form is POSTed to ideaSubmission.php. The purpose of this code is to take posted values, check that they exist and then insert them into my Mysql database.

Here are my 3 files; *note one field is disabled while I work on a fix for it - I'm aware of this :)*

HTML FORM:

<form id="submit_idea" action ="submitIdea.php" method="POST">
                            <fieldset> 
                                <legend>Idea submission</legend>
                                <label for="title">Title</label>
                                <input type="text" name="title"/>
                                <br>
                                <label for="brief">Brief</label>
                                <input type="text" name="brief"/>
                                <br>
                                <label for="problem">Problem</label>
                                <input type="text" name="problem"/>
                                <br>
                                <label for="solution">solution</label>
                                <input type="text" name="solution"/>
                                <br>
                                <label for="audience">audience</label>
                                <input type="text" name="audience"/>
                                <br>
                                <label for="prediction">prediction</label>
                                <input type="text" name="prediction"/>
                                <br>
                                <label for="constraints">constraints</label>
                                <input type="text" name="constraints"/>
                                <br>
                            <!--<label for="categories">Please select applicable categories / from the list below</label>
                                <input type="checkbox" id="categories" name"categories[]" value"App" />&nbsp;App<br />>
                                <input type="checkbox" name"categories[]" value"business_venture" />&nbsp;Business venture<br />>
                                <input type="checkbox" name"categories[]" value"home-ware" />&nbsp;Home-ware<br />>
                                <input type="checkbox" name"categories[]" value"technology" />&nbsp;Technology<br />>
                                <input type="checkbox" name"categories[]" value"furniture" />&nbsp;Furniture<br />>
                                <input type="checkbox" name"categories[]" value"art" />&nbsp;Art<br />>
                                <input type="checkbox" name"categories[]" value"jewellery" />&nbsp;Jewellery<br />>
                                <input type="checkbox" name"categories[]" value"fashion" />&nbsp;Clothing / Fashion<br />>
                                <input type="checkbox" name"categories[]" value"culinary" />&nbsp;Culinary<br />>
                                <input type="checkbox" name"categories[]" value"misc" />&nbsp;Misc<br /> -->
                                <button type="submit" onclick="processIdea();">Submit</button>
                                <div style="clear:both;"></div>
                            </fieldset>
                        </form>

JAVASCRIPT FUNCTION:

$("document").ready(function() {
 $("#submit_idea").submit(function() {
  processIdea();
  return false;
 });
});

function processIdea() {

 var errors = '';

 // Validate title
 var title = $("#submit_idea [name='title']").val();
 if (!title) {
  errors += ' - Please enter a title\n';
 }
 // Validate brief
 var brief = $("#submit_idea [name='brief']").val();
 if (!brief) {
  errors += ' - Please enter a short idea brief\n';
 }
 // Validate Problem
 var problem = $("#submit_idea [name='problem']").val();
 if (!problem) {
  errors += ' - Please discribe the problem you want to solve\n';
 }
 //Validate Solution
  var solution = $("#submit_idea [name='solution']").val();
 if (!solution) {
  errors += ' - Please discribe your solution to the above problem\n';
 }
 //Validate Audience
  var audience = $("#submit_idea [name='audience']").val();
 if (!audience) {
  errors += ' - Please discribe the audience your solution targets\n';
 }
  //Validate Prediction
  var prediction = $("#submit_idea [name='prediction']").val();
 if (!prediction) {
  errors += ' - Please discribe the prediction you want to solve\n';
 }
  //Validate constraints
  var constraints = $("#submit_idea [name='constraints']").val();
 if (!constraints) {
  errors += ' - Please discribe the constraints of your solution\n';
 }
 //Validate Categories
 // var categories = $("#submit_idea  [name='categories[]']:checked").length;
 //if (!categories) {
 // errors += ' - Please select the category your solution falls within\n';
 //}

 if (errors){
  errors = 'The following errors occurred:\n' + errors;
  alert(errors);
  return false;
 } else {

  // Submit our form via Ajax and then reset the form
  $("#submit_idea").ajaxSubmit({success:showResult, type: 'post'});
  return false;
 } 
}

function showResult(data) {
 if (data == 'save_failed') {
  alert('Form save failed, please contact your administrator');
  return false;
 } else {
  $("#submit_idea").clearForm().clearFields().resetForm();
  alert('Form save success');
  return false;
 }
}

PHP:

<?php
//Starts session
include_once '/includes/db_connect.php';
include_once '/includes/functions.php';
sec_session_start(); 

if(login_check($mysqli) == true) {

// Retrieve form data
if (empty($_POST)){
 echo "empty!"; }

if(isset($_POST['submit_idea'])){
  if(isset($_POST['title'])){ $title = $_POST['title']; } 
  if(isset($_POST['brief'])){ $brief = $_POST['brief']; } 
  if(isset($_POST['problem'])){ $problem = $_POST['problem']; } 
  if(isset($_POST['solution'])){ $solution = $_POST['solution']; } 
  if(isset($_POST['audience'])){ $audience = $_POST['audience']; } 
  if(isset($_POST['prediction'])){ $prediction = $_POST['prediction']; } 
  if(isset($_POST['constraints'])){ $constraints = $_POST['constraints']; } 
  // if(isset($_POST['categories'])){ $categories = $_POST['categories']; } 


  if (!$title || !$brief || !$problem || !$solution || !$audience || !$prediction || !$constraints) {
    echo "save_failed";
    return;
  }
  //


  //Convert categories array to a serialized string
  // $categories_list = serialize($categories);

  //if (!$link) {
  // echo "save_failed";
  // return; 
  //}
  //mysql_select_db($db['idea']);

  // Clean variables before performing insert
  $clean_title = $mysqli->real_escape_string($title);
  $clean_brief = $mysqli->real_escape_string($brief);
  $clean_problem = $mysqli->real_escape_string($problem);
  $clean_solution = $mysqli->real_escape_string($solution);
  $clean_audience = $mysqli->real_escape_string($audience);
  $clean_prediction = $mysqli->real_escape_string($prediction);
  $clean_constraints = $mysqli->real_escape_string($constraints);
  // $clean_categories_list = mysql_real_escape_string($categories_list);
} 
else {
  // Perform insert
  $now = time();

  $user_id = $_SESSION['user_id'];

  if(!$stmt = $mysqli->prepare("INSERT INTO idea_thread (user_id, time, title, Brief, problem, solution, audience, prediction, constraints) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")) {echo "Prepare failed!";}

  if(!$stmt->bind_param('issssssss', $user_id, $now, $clean_title, $clean_brief, $clean_problem, $clean_solution, $clean_audience, $clean_prediction, $clean_constraints)){echo "Binding parameters failed:";}

  if(!$stmt->execute()){echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;}

  // if (@mysql_query($sql, $link)) {
    echo "success";
  }
  // return;
  //} else {
  // echo "save_failed";
  // return;
  //}
  } else { 
    echo "How did you get here? Please log in first!";
    header("Location: ../signup.php");
    exit;
  }
?>

All help appreciated, thanks!

EDIT: ajaxSubmit is a jquery form plugin that is sourced from this;

http://malsup.github.com/jquery.form.js

解决方案

As you can see in your php code and the result of your var_dump($_POST) the submit_idea is never submitted therefore, it never goes to the if is_set($_POST['submit_idea']) to set other posted values to proper variables.
Submit that value in your form or change your condition in php

这篇关于PHP MYSQL-&gt;执行失败:(1048)列'标题'不能为空错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆